Docker 101 : Building images (Writting a Dockerfile)
Building an image:
To be able to automate the building of a docker image, we use a Dockerfile. The dockerfile contains all the necessary instruction to build an image.
- The base operating system image (Ubuntu, debian, ...).
- Instructions to install the applications -we want to run in the container- and their dependencies.
- All the files that the application might need.
- Networking information ( port of the application, ...).
- Set of commands that we want the container to run at "boot" time.
Below, is a dockerfile simple example:
FROM : describes the base operating system the image will be based on (Debian in our case).
ENTRYPOINT : describes the command that should run when the container starts (/bin/bash in our case).
We build the image using the below command:
To run the container, we get the ID of the image using the below command:
Then we run the image using the Debian IMAGE ID above :
The above command starts a container with the image IMAGE ID.
The ENTRYPOINT command "/bin/bash" is run at the start of the container.
When applications exit, the container that "houses" them exits too.
we can't override the ENTRYPOINT to run another command when the container is launched.
To be able to do that we would need to build another image with a different ENTRYPOINT.
CMD also allows us to run commands at the start of the container.
As opposed to "ENTRYPOINT", we can override the "CMD" command with another command.
We can mention the command we want to run at the start of the container using the "docker run" command as below:
If we don't mention any command, the "CMD" command will run at the start of the container.