Docker - create a container on your computer

8 minute read

In this post I will describe how to install docker on a Linux computer and how to use it. Before I will start I will give a short introduction to Docker.

Docker, written in the Go language, is a set of platforms to develop, ship and run applications that delivers software in containers. Docker separates applications from the infrastructure faciliating the quick delivery of software. Containers are standardized, encapsulated units and package their own software, configuration files and libraries. Via well-defined channels these containers can communicate with each other. All containers are lightweight since they run directly within the host machine’s kernel. Docker provides flexibility and portability enabling software to be run and tested on on-premises (local computer), or in a private or public cloud.

Centrally to the Docker engine are three components:

  1. the HTTP server. It is a type of long-running program called a daemon process (the dockerd command). The daemon listens for Docker API requests and manages Docker objects. It handles client requests, checks with the registry and manages the containers.
  2. REST API. The server forms the backend and provides a REST API for integration of applications into the engine. The REST API specifies the interfaces that programs can use to talk to the daemon and provides further instructions on what the daemon needs to do.
  3. the Docker CLI. The CLI is a command line tool (the docker command), which uses the API to offer all functions. The CLI or API manages the container.

The main classes of Docker images are:

  1. Docker container, the isolated environment that runs applications.
  2. Docker image, a read-only template used to build containers that are used to store and ship applications. Often, an image is based on another image with further customization. If he have a collection of images that can be searched and deployed we are speaking of a Docker Registry. An example is Docker hub available at https://www.docker.com/products/docker-hub.
  3. Docker service, that allows containers to be scaled across multiple Docker daemons, resulting in a swarm, a set of cooperating daemons that communicate through the Docker API.

Install Docker on Ubuntu

I will follow here the official instructions of Docker docs that can be found here.

First, we will uninstall older versions of Docker (docker, docker.io or docker-engine). This step is to assure that we will not run into any conflicts with older versions of Docker. In case, there are no previous versions of Docker on your system, running the command will not do any harm. To remove older versions, we enter the following to the console:

sudo apt-get remove docker docker-engine docker.io containerd runc

The help page of docker lists three different ways to install Docker. Here, we will install the Docker engine using the repository, that allows for facilicated installation and upgrading. This is also the recommended approach.

We set up the Docker repository, afterwards we install and update Docker from the repository.

Set up the repository

  1. First, we update the apt package index and install the relevant packages over HTTPS:
    sudo apt-get update
    sudo apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \
     gnupg-agent \
     software-properties-common
    
  2. We add Docker’s official GPG key and verify that we have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88:
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
  1. In the last step here, we set up the stable repository using the option for AMD64. The architecture has to be adjusted depending on the specifications of the computer you are working on. Also, we need to specify the name of the Ubuntu distribution, this can be realized by the subcommand lsb_release -cs which returns the name of the Ubuntu distribution (e.g. xenial, bionic).
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

Install Docker Engine - Community

  1. We update the apt package index
    sudo apt-get update
    
  2. We then install the latest version of Docker Engine and containerd by entering
    sudo apt-get install docker-ce docker-ce-cli containerd.io
    
  3. Optionally, we can also install a specific version of Docker engine - community. Using the command apt-cache madison docker-ce, we list all available versions in the repository. To install a specific version, we need to specify the version using the correct version string from the second column of the output, e.g. 5:18.09.1~3-0~ubuntu-xenial:
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

Manage Docker as a non-root user

The Docker daemon binds to a Unix socket instead of a TCP port. This Unix socket is owned by default by root and other users can only access it when putting sudo in front of every command. The Docker daemon runs always as the root user. Since we do not want to do put every time sudo in front of the docker command, we create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group. The docker group grants privileges equivalent to the root.

  1. We create the docker group
    sudo groupadd docker
    
  2. We then add our user to the docker group
    sudo usermod -aG docker $USER
    

Test for succesful installation

To test the succesful installation, run the following in the console

docker run hello-world

This should yield the following output to your console:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Uninstall Docker Engine - Community

For reasons of completeness, I will also give here the commands to remove Docker Engine - Community package from the local computer. Removing is done via the apt package managing system using the command

sudo apt-get purge docker-ce

This step however, does not remove images, containers, volumes or customized configuration files on your host. To delete these files, we enter

sudo rm -rf /var/lib/docker

Edited configuration files need to be deleted manually.

Images for Python and R

Many images exist that relate to bioinformatics applications. Often these images deploy the programming languages Python and R. The Rocker project, available at https://www.rocker-project.org/, offers Docker images for the R environment. The image discovery is implemented by employing the built-in search capability using the search command to locate images of interest. The rocker/r-base image provides the image for the R console. Using the rocker/rstudio image, the user can access the R language together with the RStudio IDE (in this case a web-based service accessible locally via a browser). Some options need to be supplied, for instance options to map the RStudio port (normally 8787) to a local system port (also normally 8787). The RStudio IDE requires the specification of a password at run time. The username is rstudio by default. The -e argument will set the password within the container.

docker run -e PASSWORD="testpass" --name=local_rstudio -p 8787:8787 rocker/rstudio

This will give the following output when run for the first time:

Unable to find image 'rocker/rstudio:latest' locally
latest: Pulling from rocker/rstudio
16ea0e8c8879: Pull complete 
7ce39da2c1e2: Pull complete 
e7408bd3a47d: Pull complete 
4145697f9637: Pull complete 
b87f12b4d765: Pull complete 
4466b5634cf1: Pull complete 
e69ed3842245: Pull complete 
Digest: sha256:6b96a83a2fa6bade836a2f9de13ba11acafafa03f434a8b21354d6508300787a
Status: Downloaded newer image for rocker/rstudio:latest
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] add: executing... 
Nothing additional to add
[cont-init.d] add: exited 0.
[cont-init.d] userconf: executing... 
[cont-init.d] userconf: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.

Given the command above, the container will run in attached mode (terminal window will be blocked until the container is terminated). To allow for a detached container, include the argument -d in above’s statement.

To start the RStudio IDE, type http://localhost:8787 in your favourite’s internet browser. Type in the username (rstudio) and the password (testpass). The container will run under the supervision of the Docker process and this containerized R environment will not impact any local versions of R.

List available containers

With time we downloaded many containers from repositories. We can use ps to display all currently available Docker containers. To do this, we enter the following to our terminal

docker ps -a

which will give the following tabular output

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d797347893f1        rocker/rstudio      "/init"                  9 minutes ago       Exited (0) 13 seconds ago                       local_rstudio
76070ca5a178        hello-world         "/hello"                 32 minutes ago      Exited (0) 32 minutes ago                       awesome_mayer

The argument -a shows all containers (without this flag, ps will only show the running containers).

To stop running Docker containers, we enter the following to the console

docker stop <container name> 

To remove the container, we enter

docker rm <container name> 

To sum up, this blog post showed how to install Docker on a Ubuntu machine. We downloaded an image for the RStudio IDE and started it in the browser. Furthermore, using the ps, stop and rm command, we got to learn a simple way to manage containers on our system.

References

https://docs.docker.com/engine/docker-overview/ https://www.docker.com/products/docker-hub https://www.rocker-project.org/


Comments

    No comments found for this article.

    Join the discussion for this article on this ticket. Comments appear on this page instantly.