Introduction to Containers

Introduction to Containers

This short tutorial is about creating a Docker container from a Dockerfile. It explains the basics of Docker without going into detail.

Table Of Contents

What is a container?

Containers are a new kind of virtualization. A container is a collection of files that are needed by the application inside the container. The processes within a container are isolated from other processes on the host system using kernel features such as namespaces, control groups and capabilities.

The Dockerfile

The Dockerfile is the file from which a Docker image is built. It could be seen as a textual description of a Docker image, since the Dockerfile defines which applications and libraries are in the image after it is built. A Dockerfile is in most cases also called Dockerfile and is structured as follows.

1FROM ubuntu:latest
2
3RUN apt-get update -y
4RUN apt-get upgrade -y
5RUN apt-get install -y htop
6
7ENTRYPOINT htop

In line 1, the FROM command is used to specify the so-called Docker Base Image. On this image, in our case Ubuntu, our later container is based. In lines 3, 4 and 5 the RUN command executes commands in the container. In this example, in lines 3 and 4 the software of the container is updated, before in line 5 the program htop is installed into the container. Finally, we use the ENTRYPOINT command to specify what should be executed when the container is started. Since we have installed htop, we want to run it when we start the container. More information about the Dockerfile can be found here.

Building the Docker Image

To build a Docker image from the Dockerfile, we need the docker build command. So that our finished image also has a name, it is tagged. The whole command looks like this.

docker build -t mycontainer:latest .

With the argument -t mycontainer:latest the container image gets its tag. The dot at the end of the line indicates where to find the Dockerfile. In our case it is located in the current directory, so in Linux it is specified with .. When building you get the following output.

Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM ubuntu:latest
 ---> 9140108b62dc
Step 2/5 : RUN apt-get update -y
 ---> Running in ba6d88153d08
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [930 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:9 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [779 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [416 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1367 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1051 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [32.0 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [2668 B]
Get:16 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [6303 B]
Get:17 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [368 kB]
Get:18 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [27.6 kB]
Fetched 18.4 MB in 3s (5811 kB/s)
Reading package lists...
Removing intermediate container ba6d88153d08
 ---> 4d9ddbd0fde2
Step 3/5 : RUN apt-get upgrade -y
 ---> Running in 45c472a10bed
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
The following packages will be upgraded:
  apt base-files gcc-10-base gpgv libapt-pkg6.0 libc-bin libc6 libgcc-s1
  libhogweed5 liblz4-1 libnettle7 libp11-kit0 libpam-modules
  libpam-modules-bin libpam-runtime libpam0g libprocps8 libseccomp2 libstdc++6
  libsystemd0 libudev1 libzstd1 perl-base procps tar ubuntu-keyring zlib1g
27 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 9920 kB of archives.
After this operation, 74.8 kB disk space will be freed.
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 gcc-10-base amd64 10.3.0-1ubuntu1~20.04 [20.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libgcc-s1 amd64 10.3.0-1ubuntu1~20.04 [41.8 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libstdc++6 amd64 10.3.0-1ubuntu1~20.04 [501 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libc6 amd64 2.31-0ubuntu9.2 [2715 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 base-files amd64 11ubuntu5.3 [60.6 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 perl-base amd64 5.30.0-9ubuntu0.2 [1513 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 tar amd64 1.30+dfsg-7ubuntu0.20.04.1 [240 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libc-bin amd64 2.31-0ubuntu9.2 [639 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 liblz4-1 amd64 1.9.2-2ubuntu0.20.04.1 [56.1 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libsystemd0 amd64 245.4-4ubuntu3.7 [270 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libudev1 amd64 245.4-4ubuntu3.7 [77.6 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libzstd1 amd64 1.4.4+dfsg-3ubuntu0.1 [237 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 zlib1g amd64 1:1.2.11.dfsg-2ubuntu1.2 [53.6 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libapt-pkg6.0 amd64 2.0.6 [835 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 gpgv amd64 2.2.19-3ubuntu2.1 [199 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 ubuntu-keyring all 2020.02.11.4 [22.1 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libseccomp2 amd64 2.5.1-1ubuntu1~20.04.1 [42.9 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 apt amd64 2.0.6 [1296 kB]
Get:19 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpam0g amd64 1.3.1-5ubuntu4.2 [55.4 kB]
Get:20 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpam-modules-bin amd64 1.3.1-5ubuntu4.2 [41.2 kB]
Get:21 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpam-modules amd64 1.3.1-5ubuntu4.2 [260 kB]
Get:22 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpam-runtime all 1.3.1-5ubuntu4.2 [37.3 kB]
Get:23 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libnettle7 amd64 3.5.1+really3.5.1-2ubuntu0.2 [115 kB]
Get:24 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libhogweed5 amd64 3.5.1+really3.5.1-2ubuntu0.2 [138 kB]
Get:25 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libp11-kit0 amd64 0.23.20-1ubuntu0.1 [188 kB]
Get:26 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libprocps8 amd64 2:3.3.16-1ubuntu2.2 [33.0 kB]
Get:27 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 procps amd64 2:3.3.16-1ubuntu2.2 [232 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 9920 kB in 2s (5885 kB/s)
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../gcc-10-base_10.3.0-1ubuntu1~20.04_amd64.deb ...
Unpacking gcc-10-base:amd64 (10.3.0-1ubuntu1~20.04) over (10-20200411-0ubuntu1) ...
Setting up gcc-10-base:amd64 (10.3.0-1ubuntu1~20.04) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libgcc-s1_10.3.0-1ubuntu1~20.04_amd64.deb ...
Unpacking libgcc-s1:amd64 (10.3.0-1ubuntu1~20.04) over (10-20200411-0ubuntu1) ...
Setting up libgcc-s1:amd64 (10.3.0-1ubuntu1~20.04) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libstdc++6_10.3.0-1ubuntu1~20.04_amd64.deb ...
Unpacking libstdc++6:amd64 (10.3.0-1ubuntu1~20.04) over (10-20200411-0ubuntu1) ...
Setting up libstdc++6:amd64 (10.3.0-1ubuntu1~20.04) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libc6_2.31-0ubuntu9.2_amd64.deb ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Cant locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Unpacking libc6:amd64 (2.31-0ubuntu9.2) over (2.31-0ubuntu9.1) ...
Setting up libc6:amd64 (2.31-0ubuntu9.2) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Cant locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../base-files_11ubuntu5.3_amd64.deb ...
Unpacking base-files (11ubuntu5.3) over (11ubuntu5.2) ...
Setting up base-files (11ubuntu5.3) ...
Installing new version of config file /etc/issue ...
Installing new version of config file /etc/issue.net ...
Installing new version of config file /etc/lsb-release ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../perl-base_5.30.0-9ubuntu0.2_amd64.deb ...
Unpacking perl-base (5.30.0-9ubuntu0.2) over (5.30.0-9build1) ...
Setting up perl-base (5.30.0-9ubuntu0.2) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../tar_1.30+dfsg-7ubuntu0.20.04.1_amd64.deb ...
Unpacking tar (1.30+dfsg-7ubuntu0.20.04.1) over (1.30+dfsg-7) ...
Setting up tar (1.30+dfsg-7ubuntu0.20.04.1) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libc-bin_2.31-0ubuntu9.2_amd64.deb ...
Unpacking libc-bin (2.31-0ubuntu9.2) over (2.31-0ubuntu9.1) ...
Setting up libc-bin (2.31-0ubuntu9.2) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../liblz4-1_1.9.2-2ubuntu0.20.04.1_amd64.deb ...
Unpacking liblz4-1:amd64 (1.9.2-2ubuntu0.20.04.1) over (1.9.2-2) ...
Setting up liblz4-1:amd64 (1.9.2-2ubuntu0.20.04.1) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libsystemd0_245.4-4ubuntu3.7_amd64.deb ...
Unpacking libsystemd0:amd64 (245.4-4ubuntu3.7) over (245.4-4ubuntu3.2) ...
Setting up libsystemd0:amd64 (245.4-4ubuntu3.7) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libudev1_245.4-4ubuntu3.7_amd64.deb ...
Unpacking libudev1:amd64 (245.4-4ubuntu3.7) over (245.4-4ubuntu3.2) ...
Setting up libudev1:amd64 (245.4-4ubuntu3.7) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libzstd1_1.4.4+dfsg-3ubuntu0.1_amd64.deb ...
Unpacking libzstd1:amd64 (1.4.4+dfsg-3ubuntu0.1) over (1.4.4+dfsg-3) ...
Setting up libzstd1:amd64 (1.4.4+dfsg-3ubuntu0.1) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../zlib1g_1%3a1.2.11.dfsg-2ubuntu1.2_amd64.deb ...
Unpacking zlib1g:amd64 (1:1.2.11.dfsg-2ubuntu1.2) over (1:1.2.11.dfsg-2ubuntu1) ...
Setting up zlib1g:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libapt-pkg6.0_2.0.6_amd64.deb ...
Unpacking libapt-pkg6.0:amd64 (2.0.6) over (2.0.2ubuntu0.1) ...
Setting up libapt-pkg6.0:amd64 (2.0.6) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../gpgv_2.2.19-3ubuntu2.1_amd64.deb ...
Unpacking gpgv (2.2.19-3ubuntu2.1) over (2.2.19-3ubuntu2) ...
Setting up gpgv (2.2.19-3ubuntu2.1) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../ubuntu-keyring_2020.02.11.4_all.deb ...
Unpacking ubuntu-keyring (2020.02.11.4) over (2020.02.11.2) ...
Setting up ubuntu-keyring (2020.02.11.4) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libseccomp2_2.5.1-1ubuntu1~20.04.1_amd64.deb ...
Unpacking libseccomp2:amd64 (2.5.1-1ubuntu1~20.04.1) over (2.4.3-1ubuntu3.20.04.3) ...
Setting up libseccomp2:amd64 (2.5.1-1ubuntu1~20.04.1) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../archives/apt_2.0.6_amd64.deb ...
Unpacking apt (2.0.6) over (2.0.2ubuntu0.1) ...
Setting up apt (2.0.6) ...
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libpam0g_1.3.1-5ubuntu4.2_amd64.deb ...
Unpacking libpam0g:amd64 (1.3.1-5ubuntu4.2) over (1.3.1-5ubuntu4.1) ...
Setting up libpam0g:amd64 (1.3.1-5ubuntu4.2) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Cant locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../libpam-modules-bin_1.3.1-5ubuntu4.2_amd64.deb ...
Unpacking libpam-modules-bin (1.3.1-5ubuntu4.2) over (1.3.1-5ubuntu4.1) ...
Setting up libpam-modules-bin (1.3.1-5ubuntu4.2) ...
(Reading database ... 4123 files and directories currently installed.)
Preparing to unpack .../libpam-modules_1.3.1-5ubuntu4.2_amd64.deb ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Cant locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Unpacking libpam-modules:amd64 (1.3.1-5ubuntu4.2) over (1.3.1-5ubuntu4.1) ...
Setting up libpam-modules:amd64 (1.3.1-5ubuntu4.2) ...
(Reading database ... 4127 files and directories currently installed.)
Preparing to unpack .../libpam-runtime_1.3.1-5ubuntu4.2_all.deb ...
Unpacking libpam-runtime (1.3.1-5ubuntu4.2) over (1.3.1-5ubuntu4.1) ...
Setting up libpam-runtime (1.3.1-5ubuntu4.2) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Cant locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
(Reading database ... 4127 files and directories currently installed.)
Preparing to unpack .../libnettle7_3.5.1+really3.5.1-2ubuntu0.2_amd64.deb ...
Unpacking libnettle7:amd64 (3.5.1+really3.5.1-2ubuntu0.2) over (3.5.1+really3.5.1-2) ...
Setting up libnettle7:amd64 (3.5.1+really3.5.1-2ubuntu0.2) ...
(Reading database ... 4127 files and directories currently installed.)
Preparing to unpack .../libhogweed5_3.5.1+really3.5.1-2ubuntu0.2_amd64.deb ...
Unpacking libhogweed5:amd64 (3.5.1+really3.5.1-2ubuntu0.2) over (3.5.1+really3.5.1-2) ...
Setting up libhogweed5:amd64 (3.5.1+really3.5.1-2ubuntu0.2) ...
(Reading database ... 4127 files and directories currently installed.)
Preparing to unpack .../libp11-kit0_0.23.20-1ubuntu0.1_amd64.deb ...
Unpacking libp11-kit0:amd64 (0.23.20-1ubuntu0.1) over (0.23.20-1build1) ...
Setting up libp11-kit0:amd64 (0.23.20-1ubuntu0.1) ...
(Reading database ... 4127 files and directories currently installed.)
Preparing to unpack .../libprocps8_2%3a3.3.16-1ubuntu2.2_amd64.deb ...
Unpacking libprocps8:amd64 (2:3.3.16-1ubuntu2.2) over (2:3.3.16-1ubuntu2) ...
Preparing to unpack .../procps_2%3a3.3.16-1ubuntu2.2_amd64.deb ...
Unpacking procps (2:3.3.16-1ubuntu2.2) over (2:3.3.16-1ubuntu2) ...
Setting up libprocps8:amd64 (2:3.3.16-1ubuntu2.2) ...
Setting up procps (2:3.3.16-1ubuntu2.2) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
Removing intermediate container 45c472a10bed
 ---> 50fa11563e57
Step 4/5 : RUN apt-get install -y htop
 ---> Running in 63c55ab7e89b
Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
  lsof strace
The following NEW packages will be installed:
  htop
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 80.5 kB of archives.
After this operation, 225 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 htop amd64 2.2.0-2build1 [80.5 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 80.5 kB in 0s (417 kB/s)
Selecting previously unselected package htop.
(Reading database ... 4127 files and directories currently installed.)
Preparing to unpack .../htop_2.2.0-2build1_amd64.deb ...
Unpacking htop (2.2.0-2build1) ...
Setting up htop (2.2.0-2build1) ...
Removing intermediate container 63c55ab7e89b
 ---> b7935759352d
Step 5/5 : ENTRYPOINT htop
 ---> Running in 4db6c907f392
Removing intermediate container 4db6c907f392
 ---> ba9ab3981bc8
Successfully built ba9ab3981bc8
Successfully tagged mycontainer:latest

During the build process, you can see the individual steps that we previously defined in the Dockerfile.

List Docker Images

Before the container is started, we have the opportunity to look at our just built Docker image. An overview of all locally available Docker images can be obtained with the following command.

docker images

An image with the repository entry mycontainer and the tag latest should appear here.

Running the Docker Container

Since it has just been verified that our Docker image is local, we can start the Docker container. We do this with the docker run command.

docker run -it mycontainer:latest

After executing the above command, htop should appear in the terminal. For htop to work properly, the docker run command needs the arguments -i and -t, where the -i is for interactive, so that the user can also give input, and -t for reseving a pseudo tty, so that the container can display output in the terminal.

List Docker Containers

Docker, of course, provides a way to list all containers on the host. For this purpose Docker provides the docker ps command.

docker ps

The running container should be displayed, which should look like this.

CONTAINER ID   IMAGE                COMMAND             CREATED         STATUS          PORTS   NAMES
27b27861ea4a   mycontainer:latest   "/bin/sh -c htop"   4 seconds ago   Up 3 seconds            thirsty_euclid

Notice the name of the Docker container. This is randomly chosen because we did not specify it with the --name option when running the Docker container.

Stopping Docker Containers

A container can be stopped using the docker stop command. The Docker container then changes its stauts to Exited. For our container, the command would look like this.

docker stop thirsty_euclid

Removing Docker Containers

When it comes to removing Docker containers, Docker has the docker rm command. We need to specify the name of the container we want to remove as an additional argument. Only Docker Containers in the Exited state, can be removed, or you have to force delete it using the -f argument. In my case the command looks like this.

docker rm thirsty_euclid

If we now list the containers again, there should be no container left on the host.

Conclusion

In this short tutorial, we built a custom Docker image and Docker container from a Dockerfile and then deleted it.

The processes shown represent the standard procedure when working with containers. Containers are relatively easy to create and manage.


See also