How to Install and Use Docker on Ubuntu

Over the last few years, Docker has gained popularity as a containerization application. Containers provide the benefits of virtual machines without the associated overhead.

A virtual machine allows users to start a self-contained operating system on top of another machine. It means users can run another Linux or Windows machine on top of their current operating system. However, it is a resource-intensive process.

Each virtual machine starts its own OS kernel, binaries, libraries and applications on top of the host machine. Containers solve this problem through sharing the host machine’s kernel. It separates on the application level only. Binaries and libraries are separated only when necessary. As a result, containers have faster execution times and smaller footprints. Docker is currently the most popular container technology.

Installation on Ubuntu

Prerequisite

Docker has a free community version called Docker CE and a paid enterprise version called Docker EE. In this article, we will concentrate on the community version.
Docker CE requires a 64-bit version of Ubuntu in any of the following flavors:

  • Zesty 17.04
  • Xenial 16.04 (LTS)
  • Trusty 14.04 (LTS)

You can run a lot of the commands as a non-root user. However, you’ll need root (sudo) access for some of the commands.

Installing Docker

There are multiple ways to install Docker. In this tutorial, we will set up Docker repository and install Docker CE from the repository. Also, we are assuming that you have a clean machine without any previous versions of Docker. If you have a previous version of Docker, please uninstall the version before starting.

Repository Setup

1. Update your apt package to make sure your OS is up-to-date:

$ sudo apt-get update

2. Install the following packages:

$ sudo apt-get install 
    apt-transport-https 
    ca-certificates 
    curl 
    software-properties-common

3. Add the official GPG key

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Verify the key fingerprint to be 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88. You can use the following command:

$ sudo apt-key fingerprint 0EBFCD88
pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <[email protected]>
sub   4096R/F273FCD8 2017-02-22      

4. Set up a stable repository using the following:

$ sudo add-apt-repository 
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu 
   $(lsb_release -cs) 
   stable"

The “arch” defines the architecture. For x86_64 use “arch=amd64”, for armhf use “arch=armhf” and for s390x use “arch=x390x”.

Docker CE Setup

1. Update your apt packages:

$ sudo apt-get update

2. Install latest Docker CE:

$ sudo apt-get install -y docker-ce

3. The above command should install docker and start the daemon. Check if the daemon is running using the following command:
$ sudo systemctl status docker
The result should look like below. The status “active (running)” means everything is okay.

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2017-08-30 06:53:52 PDT; 2 hours ago
     Docs: https://docs.docker.com
 Main PID: 827 (docker)

Using Docker

To use Docker, you need to understand 3 concepts:

    • Docker Hub: Docker Hub is the cloud-based repository where you store all public Docker images. You can create a login on Docker Hub here: https://hub.docker.com/
    • Docker Images: Docker images are the blue prints of different containers. It is a snapshot of the container.
    • Docker Container: Running a Docker image you get a container. The container is the working machine that you will use for your tasks.

Let’s get started on using Docker.

Docker commands

You can view all docker commands with the following:

$ docker

You can find all the Docker commands here.
If you want to find out about a specific command, you can use:
$ docker target-command –help
where “target-command” is the command you want to investigate.

Downloading Docker Images

Docker images are downloaded from Docker Hub. You don’t need to login to download and use images, but you need a login for uploading images.

Download your first docker image using the following command:

$ docker run hello-world

The output should look like this:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete 
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

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

In the above lines, Docker first looked for the image “hello-world” in your local repository. When it was “Unable to find image”, it pulled it from the Docker Hub. Then it ran the hello-world container.

The following command will show you available images on Docker Hub:

$ docker search nginx

We were looking for “nginx” related images. The partial output looks like this:

NAME                           DESCRIPTION                             STARS     OFFICIAL  AUTOMATED
nginx                    Official build of Nginx.                      6737       [OK]       
jwilder/nginx-proxy      Automated Nginx reverse proxy for docker c..  1099       [OK]
richarvey/nginx-php-fpm  Container running Nginx + PHP-FPM capable …   429        [OK]                                                                               
...

Alternatively, you can go to https://hub.docker.com/ and search for images. “Official” denotes versions that were checked by the Docker team.

You can also use the “pull” command to download images:

$ docker pull nginx

You can view all the images on your local repository with the following command

$ docker images

The output should look like this:

REPOSITORY          TAG               IMAGE ID            CREATED           SIZE
ubuntu             latest           ccc7a11d65b1        3 weeks ago         120MB
nginx              latest           b8efb18f159b        5 weeks ago         107MB
hello-world        latest           1815c82652c0        2 months ago        1.84kB

With the images from your local repository, you can start a container. For example, you can start your nginx container from the above repository.

Running Containers

Let’s suppose, we want to create an Ubuntu container from the available Ubuntu image. You can use the following command:

$ docker run -it ubuntu

The -it option tells run command to run an interactive shell. You will see a command prompt like this:

root@6370a8b73050:/#

This means you are logged in as a root user to the container with the id 6370a8b73050. You can install anything you want into this container. Suppose you want to install python.

You can use the commands:

root@6370a8b73050:/# apt-get update

And

root@6370a8b73050:/# apt-get install -y python3.5

Once the installations are successful, you have a python-ready Ubuntu container.
You can always check the container status from your host’s command line ($ means host prompt and root@6370a8b73050 container prompt):

$ docker ps 
CONTAINER ID   IMAGE       COMMAND                      CREATED        STATUS          PORTS        NAMES
6370a8b73050   ubuntu      "/bin/bash"             13 minutes ago   Up 12 minutes               cranky_bassi

$ docker ps -a
CONTAINER ID   IMAGE    COMMAND                         CREATED        STATUS          PORTS        NAMES
6370a8b73050   ubuntu  "/bin/bash"                 13 minutes ago   Up 13 minutes               cranky_bassi
6a16d462dd28   ubuntu  "/bin/bash"                 16 minutes ago   Exited (0) 15 minutes ago   youthful_pasteur
889d3a9328f2   nginx   "nginx -g 'daemon ..."      18 minutes ago   Exited (0) 17 minutes ago   hardcore_ritchie             

In the above output, ps command is only showing the running container while ps command with the “-a” option is showing all containers.

Saving Containers as Images

Suppose you want to save your python container as an image to reuse it later.
First exit the container:

root@6370a8b73050:/# exit

You can save the container to your local repository using the following command:
$ docker commit -m “Description of changes” -a “Author name” containerID repository/imageName
The repository is generally your username from Docker Hub. So you can save your python container like this:

$ docker commit -m "Python3.5 Container" -a "Zak H"  6370a8b73050 zakh/python3.5-ready

If you check your images, you will have a python image in your repository:

$ docker images
REPOSITORY                 TAG               IMAGE ID            CREATED            SIZE
zakh/python3.5-ready      latest           6ddf31466a89        2 minutes ago       196MB
ubuntu                    latest           ccc7a11d65b1        3 weeks ago         120MB
nginx                     latest           b8efb18f159b        5 weeks ago         107MB
hello-world               latest           1815c82652c0        2 months ago        1.84kB

You can push this image to the Docker Hub using the following commands:
docker login -u username
docker push username/imageName

In the above case, it will be:

docker login -u zakh
docker push zakh/python3.5-ready

Once you push the container to the Docker Hub, others can download the image to their computers and use the python3.5 environment you set up.

Cleaning Up

The following command will remove containers:
docker rm containerID

The following command will remove images from your local repository:
docker rmi imageID

Final Words

The above guide gives you a step-by-step process to use Docker to simplify your development. You can create your personal configurations, save them as images and share it with other users or reuse the images yourself. It speeds up the whole development process.

Advanced Topics
  • You can use DockerFile to build images from written YAML files.
  • Docker Swarm can help you orchestrate and manage a large number of Docker nodes.
References:
  • https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/
  • https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04
  • https://docker-curriculum.com/
  • https://www.digitalocean.com/community/tutorials/the-docker-ecosystem-an-introduction-to-common-components
  • https://www.airpair.com/docker/posts/8-proven-real-world-ways-to-use-docker
  • http://www.zdnet.com/article/what-is-docker-and-why-is-it-so-darn-popular/
  • https://docs.docker.com/enterprise/
  • https://docs.docker.com/engine/swarm
  • https://docs.docker.com/engine/reference/builder/
  • https://docs.docker.com/engine/reference/commandline/docker/
Related Posts