Tutorials > How to manage Docker containers

How to manage Docker containers

Published on: 20 January 2020

Containers Docker Ubuntu Virtualization

Docker is a software platform developed to facilitate the deployment of applications based on the concept of containers. The platform can be used to create, test and distribute applications by collecting them in standardized units, called 'containers'.

Docker containers can be seen as isolated environments where applications can be run with no interference by other processes. Similarly to what happens with virtual machines, specific computational resources are allocated to each container. Unlike Virtual Machines, Docker does not require any hardware emulation but uses the physical hardware of the host for each container.

Being resource-efficient and able to generate improved system performance, containerization can be seen as the natural evolution of virtualization. Moreover, as they feature whatever needed for the correct execution of the applications - including libraries, system tools and codes - containers simplify the deployment of an application with no need to worry about the configuration of the runtime environment. 

In this tutorial you will learn how to best manage Docker containers on Linux, download images from Docker Hub, create a new container, and save an image of it.

When the Docker platform is not present on your system, following our guide on How to install Docker on Ubuntu 18.04 is recommended. 

Downloading images

Containers are built from images. By default these images are downloaded from Docker Hub, a registry of official images provided by the company.

Anyone can host their images on Docker Hub. This allows the use of pre-packaged images as the basis for your applications.

Try to run the command:

$ docker run centos

Docker Run CentOS

Docker will search for the "centos" image in the local registry. Not finding it (as it has not been downloaded so far) Docker will download it from Docker Hub and build the container with this image.

The image used does not provide any visual output, but you can see the container running by the command:

$ docker ps

to see all existing containers, both running and stopped:

$ docker ps -a

Show Containers

To view the list of downloaded images, type:

$ docker images

Docker Show Images

Building containers

Containers are similar to virtual machines and are less expensive in terms of resources.

Try building one with the centos image downloaded previously:

$ docker run -it centos

The "-it" switch allows access to the shell inside the container. The shell displayed isn’t of your operating system but of the container: all the commands will be executed on the shell of the OS present inside the container (in this case Ubuntu)and not on the host’s shell.

To exit the container’s shell and return to the host’s shell, type:

$ exit

Exit from the shell of container

To start, stop or delete a container:

$ docker start ID_or_container_name
$ docker stop ID_or_container_name
$ docker rm ID_or_container_name

Before starting or deleting a container, it must be in a "stop" state.
Before blocking a container, it must be in a "run" state.

Containers are isolated environments and are therefore "closed" by definition. To interact with their "content", map a communication port.

$ docker run -p 8081:80 -d nginx
  • “docker run”: container start command.

  • “-p 8081:80”: exposes the container port 80 on port 8081 of the host.

    • If it is a container that hosts a WebServer, access it by "localhost:8081”.

  • “-d”: run the container in background.

  • “nginx”: image used to build the container.

    • If the image is not present locally, it is downloaded from Docker Hub.

NGINX Web Page

Building containers images

Once  made the container and the necessary "customizations" the creation of the relative image is recommended: everything that is inserted in the container (installation of additional modules or other customizations) remains in the container and not in the image used for the build.  If removed and recreated, the container will not show any of the changes made up to that point.

Assume you have created a container with the CentOS image and installed Nginx. If you remove the container and recreate it with the ubuntu image, you will not have Nginx in the container as the image used does not contain it.

To generate the image of a container (and have a backup to recreate the container with the same conditions) execute the command:

$ docker commit -m "List of changes made" -a "User name" container_id repository/image_name
  • "-m" is used to write the list of changes to get a log to refer to

  • "-a" is used to indicate the name of the user who generates the image

This command saves the image in the local registry (viewable via docker images).

The image may also be saved on Docker Hub.  This can be done by first authenticating yourself on Docker Hub, using Docker (from shell), by typing:

$ docker login -u username
  • You will be required to enter the password for authentication on Docker Hub

$ docker push username/image_name