Running Containers From Images
Remember when we mentioned containers are built from images? The command docker run <image>
creates a container based on that image. One can say that a container is a running instance of an image. Another reminder is that this image could either be local or in the registry.
Go ahead and run the already created images docker run python-docker
and docker run js-docker:
What do you notice? The containers run outputs to the terminal's respective lines. Notice that the command preceded by CMD in the Dockerfile is the one that runs:
docker build -t python-docker:test . and docker build -t js-docker:test .
Then, run the following:
python-docker:test and docker run js-docker:test
Note
You will not see any output on the terminal.
This is not because we don't have a command CMD
to run as soon as the container is up. For both images built from Python and Node, there is a CMD
inherited from the base images.
Note
Images created always inherit from the base image.
The two containers we have run contain scripts that run once and exit. Examining the results of docker ps
, you'll have nothing listed from the two containers run earlier. However, running docker ps -a
reveals the containers and their state as exited.
There is a command column that shows the CMD of the image from which the container is built from.
When running a container, you can specify the name as follows:
docker run --name <container-name> <image-name>
(for example, docker run --name py-docker-container python-docker
):
We outlined earlier that you only want to have relevant Docker images and not the <none>
tagged Docker images.
As for containers, you need to be aware that you can have several containers from one image. docker rm <container-id>
is the command for removing containers. This works for exited containers (those that are not running).
Note
For the containers that are still running, you would have to either:
Stop the containers before removing them (docker stop <container-id>)
Remove the containers forcefully (docker rm <container-id> -f
)
No container will be listed if you run docker ps
,
but sure enough if we run docker ps -a
, you will notice that the containers are listed and their command columns will show the inherited CMD commands: python3
and node
:
Python
The CMD in Dockerfile for Python's image is python3
. This means that the python3
command is run in the container and the container exits.
Note
With this in mind, one gets to run Python without installing Python in one's machine.
Try running this: docker run -it python-docker:test
(with the image we created last).
We get into an interactive bash shell in the container. -it
instructs the Docker container to create this shell. The shell runs python3
, which is the CMD in the Python base image:
In the command docker run -it python-docker:test python3 run.py, python3 run.py
is run as you would in the terminal within the container. Note that run.py
is within the container and so runs. Running docker run -it python python3 run.py
would indicate the absence of the run.py
script:
The same applies to JavaScript, showing that the concept applies across the board.
docker run -it js-docker:test
(the image we created last) will have a shell running node (the CMD in the node base image):
docker run -it js-docker:test node run.js
will output Hello Docker - JS:
That proves the inheritance factor in Docker images.
Now, return the Dockerfiles to their original state with the CMD commands on the last line.