Interacting with containers
- Use the exec of the docker container command
- We can execute interactive commands by using shell
- Recommended Practice:
- Run the container in a detached mode and use exec/logs for experimentation.
Containerization using Dockerfile
- Dockerfile is a set of instructions which help in building the docker image.
- To build a docker image (which is oci compliant) we need to
- choose a base image
- provide additional instructions to containerize the application
docker image build
command does the image building process by creating a container, executing instructions and creating the docker image- The base image is required to set necessary stuff, if you want to build a docker image from scratch we can use base image
scratch
- The instructions in docker file
INSTRUCTION <value>
- Refer Here for whole set of instructions
FROM: set base image
- Refer Here for official docs
LABEL: set metadata
- Refer Here for official docs
RUN: execute instructions for configuring your application
- Refer Here for official docs
CMD: run the command during startup
- Refer Here for official docs
Expose: expose container ports
- Refer Here for official docs
Image building ways of working
- Create a newfolder or choose an existing folder. Create a file called as
Dockerfile
. - Generally
Dockerfile
is present with the source code.
Building springpetclinic
- Refer Here for the first set of changes (https://github.com/asquarezone/DockerZone/commit/1629a1f639be4e756b021107d1dfa1ef812a4145?diff=split)
- Now build the image with tag
1.0.0
and repospc
- The command is
docker image build -t spc:1.0 .
- Now add labels Refer Here for changeset and below for image building, lets use the same tag and repo
- When we repeatedly use same repo and tags there is a chance that some earlier builded images will get a repo name and tag name as
<none>
- Refer Here for the changes done to download the spring petclinic into the docker image.
- Now lets build the image with same repo
spc
and tag1.0.1
- the Failure wget not found represents the
wget
not present in the base image, So we have two options- find an alternative command
- install wget
- We have used the alternative to use
curl
Refer Here for changes - Now lets build the image with same repo
spc
and tag1.0.1
- Refer Here for adding the expose information.
- Now build the image with same repo
spc
and tag1.0.2
- Now lets create the container from spc:1.0.2 and we observed the container went into exited state
- Now add the CMD to start the application, build the image with repo
spc
and tag1.0.3
Refer Here for the changes in Dockerfile
- Note: 1.0.3 failed due to wrong argument Refer Here for fix
- Now create the container using spc:1.0.4
- Generally slim or alpine base images will reduce image size Refer Here for the changes to use alpine jdk 11 as a base image
- Build the image with repo
spc:1.0.0-slim
- run the contianer