Docker (Working with container)
Working with container
As described in the docker documentation, we install it by following the instructions on the following link https://docs.docker.com/install/linux/docker-ce/ubuntu/
We start by downloading the ubuntu image (e.g. version 16.04) with the command
docker pull ubuntu:16.04
To create a container use the Docker CLI run command.
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
The syntax is therefore the run command with one or more arguments. We notice when we have something in square brackets, e.g. [OPTIONS] is optional.
Let’s take an example, with the command:
docker run ubuntu:16.04 ps aux
This command tells the docker daemon to create a container based on the image of ubuntu 16.04 and to execute a command inside it that is “ps aux” (where “aux” are the parameters of “ps”).
We note that we can start as many containers as we want and with different versions of Linux, for example we can start a container based on ubuntu and one based on CentOS, all of which can coexist on the same host Docker. The images don’t have “all” the OS but the view they give you is that of the ubuntu OS.
Obviously if we launch ubuntu inside we have the package manager apt-get, if we launch a container based on an image of CentOS we have yum.
Interact with console
The client Docker (our terminal) can interact with the Daemon Docker in different ways.
- -i option (interactive mode) tells Docker to connect to STDIN on the container;
- -t option (TTY mode) specifies to get a pseudo-terminal;
- -d flag tells Docker to run the container as a daemon (prints the id of the container created)
The -d option allows you to run a container as if it were a detached process, as if it were a daemon, and so we are not attached to the standard container input as we would with -i, so the container is run in the background. We can connect to this container at any time and we can see its output using the docker logs command passing the container name or ID.
$ docker logs <container id/name>
Ping Example
We ping in interactive and detachetd mode.
The “ping” command is not included in the ubuntu base image, so we must install it:
sudo docker run -it ubuntu:16.04 bash
apt-get update && apt-get install iputils-ping
To save the changes introduced to the container and then the utility iputils-ping we run commit:
sudo docker commit fdf0ce0a95ed ubuntu-ping
Now if we launch the ping command in interactive mode we will have the following output:
sudo docker run -it ubuntu-ping bash
ping www.google.com
Let’s try to launch ping in detached mode:
sudo docker run -d ubuntu-ping ping www.google.it
An alphanumeric code is printed which is the ID (Long Version) of the container just created.
The ping command has been executed, but we do not see the output. The execution has been launched in the background. To statically see the container logs, and thus the execution of the ping command, we can use the command:
$ docker logs <container id/name>sudo docker logs ff3809241355
To follow the output in real time, add the -f option:
sudo docker logs -f ff3809241355
Other important commands
For the list of running containers in our Docker Engine:
docker ps
A container that ends its run is no more viewable with the docker ps. Run:
docker ps -a
to list all containers (includes containers that are stopped).
The output is composed as follows:
- Container id;
- The basic image from which the container is constructed;
- The command currently running in the container (ping www.google.it)
when created. - Status (that can be up)
- Names: referred to as dockers from a random name, but we when we launch docker run we can give the --name option, to give it a custom name.
In docker there are two variants of the ID of the containers that are:
- the short version;
- and the long.
They are equivalent to each other and docker makes sure that two containers never have the same figures at the beginning.
When we run a docker command we can specify either of the two versions.
Terminate a container
To finish the execution of a container we can use the stop or kill command, the difference is that:
- docker kill allows us to send a special signal to the parent process of the container that we are stopping;
- while docker stop sends control+C.
$ docker stop [OPTIONS] CONTAINER [CONTAINER...]
or
$ docker kill [OPTIONS] CONTAINER [CONTAINER...]
In any case the container goes to the stopped state and will no more be viewable in the ps docker without parameters, but we can see it with the -a option.
To restart a stopped container, I can run the command:
docker start [OPTIONS] CONTAINER [CONTAINER...]
We can open a shell in a container that is already running with -t mandatory and -i optional and with /bin/sh.
sudo docker exec -it ff3809241355 /bin/sh
By launching the “ls” command we see the file system of the container and not of the host machine. We can quit with the “exit” command.
We can delete a (stopped) container:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Display containers’ stats with command:
docker stats [OPTIONS] [CONTAINER...]
sudo docker stats ff3809241355