Docker containers
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
Section titled “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 --helpHaving 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.4Unable to find image 'httpd:2.4' locally2.4: Pulling from library/httpdbf5952930446: Pull complete3d3fecf6569b: Pull completeb5fc3125d912: Pull complete679d69c01e90: Pull complete76291586768e: Pull completeDigest: sha256:3cbdff4bc16681541885ccf1524a532afa28d2a6578ab7c2d5154a7abc182379Status: Downloaded newer image for httpd:2.40963ab77c1cde9a69c5a07149df3c1053da30179ce46eca1a0741c1d3543f3c4The above container is
- running an
httpd:2.4image - 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 lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESNote 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 --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0963ab77c1cd httpd:2.4 "httpd-foreground" About a minute ago Created web1Starting a container
Section titled “Starting a container”To start it, run docker container start
$ docker container start web1web1Now it will appear in the list
$ docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0963ab77c1cd httpd:2.4 "httpd-foreground" 3 minutes ago Up 31 seconds 80/tcp web1To run a container, i.e. create and start at the same time, use docker container run
$ docker container run --name web2 --detach httpd:2.43cf63678cf607f620e9dc2f822c98eee4e7f760ac26b569718f077f754970b2fThe above will start another container that is
- running the
httpd:2.4image - 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 lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3cf63678cf60 httpd:2.4 "httpd-foreground" 4 minutes ago Up 4 minutes 80/tcp web20963ab77c1cd httpd:2.4 "httpd-foreground" 9 minutes ago Up 6 minutes 80/tcp web1Stopping a container
Section titled “Stopping a container”Use docker container stop
$ docker container stop web1web1
$ docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3cf63678cf60 httpd:2.4 "httpd-foreground" 6 minutes ago Up 6 minutes 80/tcp web20963ab77c1cd httpd:2.4 "httpd-foreground" 11 minutes ago Exited (0) 3 seconds ago web1web1 has been stopped.
Removing a container
Section titled “Removing a container”$ docker container rm web1web1