Docker containers

Published: Thursday, 13 August 2020

A docker container is process than runs an instance of a docker image, with customised settings, in an isolated environment.

It is possible to start multiple containers running the same image. One reason for this is clustering.

The docker daemon, dockerd, manages docker containers on the docker host system.

Container creation

When creating a container, various options can be used to configure and isolate the container. For a full list of options see:

$ docker container create --help

Having a naming convention helps with debugging and managing containers. Give each docker container a unique name using the --name option

$ docker container create --name web1 httpd:2.4
Unable to find image 'httpd:2.4' locally
2.4: Pulling from library/httpd
bf5952930446: Pull complete
3d3fecf6569b: Pull complete
b5fc3125d912: Pull complete
679d69c01e90: Pull complete
76291586768e: Pull complete
Digest: sha256:3cbdff4bc16681541885ccf1524a532afa28d2a6578ab7c2d5154a7abc182379
Status: Downloaded newer image for httpd:2.4
0963ab77c1cde9a69c5a07149df3c1053da30179ce46eca1a0741c1d3543f3c4

The above container is

  • running an httpd:2.4 image
  • has a container ID of 0963ab77c1cde9a69c5a07149df3c1053da30179ce46eca1a0741c1d3543f3c4
  • and has a user-friendly container name of web1

To see running containers on the docker host, use docker container ls

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Note that the newly created container does not appear as it’s status is not running.

Instead, use the -a or --all option to see it.

$ docker container ls --all
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
0963ab77c1cd        httpd:2.4           "httpd-foreground"       About a minute ago   Created                                 web1

Starting a container

To start it, run docker container start

$ docker container start web1
web1

Now it will appear in the list

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0963ab77c1cd        httpd:2.4           "httpd-foreground"       3 minutes ago       Up 31 seconds       80/tcp              web1

To run a container, i.e. create and start at the same time, use docker container run

$ docker container run --name web2 --detach httpd:2.4
3cf63678cf607f620e9dc2f822c98eee4e7f760ac26b569718f077f754970b2f

The above will start another container that is

  • running the httpd:2.4 image
  • has been given a container ID of 3cf63678cf607f620e9dc2f822c98eee4e7f760ac26b569718f077f754970b2f
  • has a user-friendly container name of web2

The -d or --detach option is required to run the container in the background.

$ docker container ls
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
3cf63678cf60        httpd:2.4           "httpd-foreground"   4 minutes ago       Up 4 minutes        80/tcp              web2
0963ab77c1cd        httpd:2.4           "httpd-foreground"   9 minutes ago       Up 6 minutes        80/tcp              web1

Stopping a container

Use docker container stop

$ docker container stop web1
web1

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                     PORTS               NAMES
3cf63678cf60        httpd:2.4           "httpd-foreground"   6 minutes ago       Up 6 minutes               80/tcp              web2
0963ab77c1cd        httpd:2.4           "httpd-foreground"   11 minutes ago      Exited (0) 3 seconds ago                       web1

web1 has been stopped.

Removing a container

$ docker container rm web1
web1