Skip to content

My first containers and docker commands

Execution of the first container

Run a basic hello-world container with:

docker pull hello-world
docker run hello-world

and then run the following commands to see what happened:

docker image ls
docker container ls
docker container ls -a

The first command (docker pull hello-world) was not necessary; please retry now with:

docker container rm <container-name>
docker image rm hello-world
docker run hello-world

Some commands to manage the system, clean up containers and images

Some usefull commands:

  • to manage docker:
docker system df # Show docker disk usage
docker system prune # Remove unused data; to use with cuation
  • to delete containers:
docker container rm <container-name> # delete a container
docker rm $(docker ps -a -q) # to delete all unused containers

Remark 1

The id of the container can be used instead of <container-name> to delete the container.

Remark 2

Use the option --force or -f to force the removal of a running container. For instansce the following command will delete ALL containers: docker rm -f $(docker ps -a -q)

  • to delete images:
docker image rm <image-id> # delete an image
docker image prune -a # delete all unused images

Remark

The name of the image can be used instead of <image-id> to delete the image. For example: docker image rm <image-name>:<tag> <tag> is optional if it is the default tag is 'latest'.

More generally, to get the list of docker commands, type:

docker help

or to have informations on how to run a command:

docker <command> --help

You can also refer to the official docker website (https://docs.docker.com/engine/reference/commandline/docker/) for more details.

Command alliases

Some aliases exist in docker. The following commands are equivalent:

docker container ls [OPTIONS] ...
docker ps [OPTIONS] ...
docker container rm [OPTIONS] ...
docker rm [OPTIONS] ...
docker container run [OPTIONS] ...
docker run [OPTIONS] ...
docker image ls [OPTIONS] ...
docker images [OPTIONS] ...
docker image rm [OPTIONS] ...
docker rmi [OPTIONS] ...

Execution of the second container

Run a container with the pre-installed Python shell by setting the version, i.e by chosing the image tag (cf. dockerhub website to find an image).

In two different terminal windows, run:

Terminal #1

user@host $ docker run -it --name mycontainer --rm python:3.10
Python 3.10.11
>>> print('Hello Python')
Hello Python
>>>

Terminal #2

user@host $ docker ps -a
CONTAINER ID   IMAGE         COMMAND     CREATED      STATUS       PORTS    NAMES
6a4f09072980   python:3.10   "python3" 3 minutes ago Up 3 minutes         mycontainer

Then exit from the python shell and rexecute the command of the terminal #2.

Remark

The flags:

-it allows to have an interactive session: -i keeps STDIN open and -t allocates a pseudo-TTY terminal;

--name NAME give a name to the container instead of a random name;

--rm remove container after use.

Re-run the same container by opening the bash shell:

user@host $ docker run -it --name mycontainer --rm python:3.10 /bin/bash
root@6d7702e04353:/# python
Python 3.10.11
>>>

To run a python program with a specific version of python, it is useful to be able to share a directory between the host machine and the container (bind mounts; click here for more details).

For instance, let us try execute the python codes inside the folder application/ by binding it into /var/code inside the container:

user@host $ docker run -it --name mycontainer --rm -v application:/var/code python:3.10 bash
root@98f84f1f6cc1:/# cd /var/code
root@7fb7d20ea16d:/var/code# ls -l
root@7fb7d20ea16d:/var/code# pip install matplotlib numpy
root@7fb7d20ea16d:/var/code# python pi.py
root@7fb7d20ea16d:/var/code# ls -l

Remark

Through this example, we can see the interest of customising our own Docker image.

Instead of opening the bash interperter of the container, it is possible to execute directly the python code:

docker run -it --name mycontainer --rm -v application:/var/code python:3.10 python /var/code/hello_world.py