Starting A Container

Once we have the images, we can use them to start containers. In this recipe, we will start a container with the ubuntu:latest image and see what happens behind the scenes.

Getting ready

Ensure that the Docker daemon is running on the host and can be connected through the Docker client.

How to do it…

We can start a container using either of the following syntaxes:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

NOTE

The docker container run command is recommended over docker run because, in version 1.13, Docker logically grouped container operations under the docker container management command, and so docker run might be obsolete in the future.

Here is an example of using the docker container run command:

$ docker container run -i -t --name mycontainer ubuntu /bin/bash

By default, Docker picks the image with the latest tag:

  • The -interactive or -i option starts the container in interactive mode by keeping the STDIN open

  • The --tty or -t option allocates a pseudo-tty and attaches it to the standard input

So, with the preceding command, we can start a container from the ubuntu:latest image, attach pseudo-tty , name it mycontainer , and run the /bin/bash command. If the name is not specified, then a random string will be assigned as the name.

Also, if the image is not available locally, then it will be downloaded from the registry first and then run.

How it works…

Under the hood, Docker will:

  • Merge all the layers, that makeup that image using UnionFS.

  • Allocate a unique ID to a container, which is referred to as the Container ID.

  • Allocate a filesystem and mount a read/write layer for the container. Any changes on this layer will be temporary and will be discarded if they are not committed.

  • Allocate a bridge network interface.

  • Assign an IP address to the container.

  • Execute the process specified by the user.

Also, with the default Docker configuration, it creates a directory (with the container’s ID inside /var/lib/docker/containers ), which has the container’s specific information such as hostname, configuration details, logs, and /etc/hosts .

There’s more…

To exit from the container, press Ctrl + D or type exit . It is similar to exiting from a shell, but this will stop the container. Alternatively, to detach from the container, press Ctrl + P + Q . The detached container will detach itself from the terminal, give control back to the Docker host shell, and wait for the docker container attach command to attach back to the container.

The run command creates and starts the container. With Docker 1.3 or later, it is possible to just create the container using the create command and run it later using the start command, as shown in the following example:

$ ID=$(docker container create -t -i ubuntu /bin/bash)
$ docker container start -a -i $ID

The container can be started in the background, and then we can attach to it whenever needed. We need to use the -d option to start the container in the background:

$ docker container run -d -i -t ubuntu /bin/bash 
0df95cc49e258b74be713c31d5a28b9d590906ed9d6e1a2dc75672aa48f28c4f

The preceding command returns the container ID of the container, which we can attach to later, as follows:

$ ID=$(docker container run -d -t -i ubuntu /bin/bash)`
$ docker attach $ID

In the preceding case, we chose /bin/bash to run inside the container. If we attach to the container, we will get an interactive shell. We can run a non-interactive process and run it in the background to make a deamonized container, like the following:

$ docker container run -d  ubuntu \
           /bin/bash -c  \
           "while [ true ]; do date; sleep 1; done"

To remove the container after it exits, start the container with the --rm option, as follows:

$ docker run --rm ubuntu date

As soon as the date command exits, the container will be removed.

The --read-only option of the run command will mount the root filesystem in read-only mode:

$ docker container run --read-only --rm \
          ubuntu touch file
touch: cannot touch 'file': Read-only file system

You can also set custom labels for containers, which can be used to group containers based on labels. Take a look at the Labelling and filtering containers recipe in this chapter for more details.

TIP

A container can be referred to in three ways: by name, by its short container ID ( 0df95cc49e25 ), and by its container ID ( 0df95cc49e258b74be713c31d5a28b9d590906ed9d6e1a2dc75672aa48f28c4f ).

See also

Look at the help option of docker run :

$ docker container run --help

The documentation on the Docker website can be found here: https://docs.docker.com/engine/reference/commandline/container_run/.