How to Install and Use Docker on Ubuntu 16.04

DockerUbuntuGo Programming

Docker is an open-source project that automates the deployment of application inside the software container. The container allows the developer to package up all project resources such as libraries, dependencies, assets etc. Docker is written in Go Programming language and is developed by Dotcloud. It is basically a container engine which uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system and automates the application deployment on the container.

Installing Docker

Before install Docker, it should required updated packages. To update the packages, use the following command –

$ sudo apt-get update

Use the following command to add the GPG key for the official Docker repository to the system-

$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

The sample output should be like this –

Executing: /tmp/tmp.akuLSrhQz8/gpg.1.sh --keyserver
hkp://p80.pool.sks-keyservers.net:80
--recv-keys
58118E89F3A912897C070ADBF76221572C52609D
gpg: requesting key 2C52609D from hkp server p80.pool.sks-keyservers.net
gpg: key 2C52609D: public key "Docker Release Tool (releasedocker) <docker@docker.com>" imported
gpg: Total number processed: 1
gpg:             imported: 1 (RSA: 1)

To add the Docker repository to APT sources, use the following command –

$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list

To update the package database with the Docker packages from the newly added repository, use the following command –

$ sudo apt-get update

Make sure you are about to install from the Docker repository instead of the default Ubuntu repository. To verify it, use the following command –

$ apt-cache policy docker-engine

The sample output should be like this –

docker-engine:
   Installed: (none)
   Candidate: 1.11.1-0~xenial
   Version table:
   1.11.1-0~xenial 500
      500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
   1.11.0-0~xenial 500
      500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages

Notice that, docker-engine is not installed,to install Docker-engine, use the following command –

$ sudo apt-get install -y docker-engine

The sample output should be like this –

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
   apport-hooks-elementary contractor libgda-5.0-4 libgda-5.0-common
   libgranite-common libgranite3 libgsignon-glib1 libindicate5 libnoise-core0
   libtagc0 mysql-server-5.7 mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
   aufs-tools cgroupfs-mount
The following NEW packages will be installed:
   aufs-tools cgroupfs-mount docker-engine
0 upgraded, 3 newly installed, 0 to remove and 212 not upgraded.
Need to get 14.6 MB of archives.
After this operation, 73.7 MB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/universe amd64 aufs-tools amd64 1:3.2+20130722-1.1ubuntu1 [92.9 kB]
..............................................................................................

To check whether docker is started or not, use the following command –

$ sudo systemctl status docker

The sample output should be like this –

docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
   Active: active (running) since Mon 2016-05-23 10:36:08 IST; 5min ago
   Docs: https://docs.docker.com
   Main PID: 14186 (docker)
   Tasks: 20 (limit: 512)
   Memory: 21.5M
   CPU: 385ms
   CGroup: /system.slice/docker.service
            ├─14186 /usr/bin/docker daemon -H fd://
            └─14204 docker-containerd -l /var/run/docker/libcontainerd/docker-con

May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.169666150+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.261017824+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.469630404+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.829495384+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.830931516+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.831009718+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.831055091+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.831113464+05:30"
May 23 10:36:08 linux systemd[1]: Started Docker Application Container Engine.
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.855330148+05:30

To start the Docker service, use the following command –

$ sudo service docker start

To view all the available subcommands of Docker, use the following command –

$ docker

The sample output should be like this –

Usage: docker [OPTIONS] COMMAND [arg...]
   docker daemon [ --help | ... ]
   docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

Options:
   --config=~/.docker Location of client config files
   -D, --debug Enable debug mode
   -H, --host=[] Daemon socket(s) to connect to
   -h, --help Print usage
   -l, --log-level=info Set the logging level
   --tls Use TLS; implied by --tlsverify
   --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
   --tlscert=~/.docker/cert.pem Path to TLS certificate file
   --tlskey=~/.docker/key.pem Path to TLS key file
   --tlsverify Use TLS and verify the remote
   -v, --version Print version information and quit

Commands:
   attach             Attach to a running container
   build              Build an image from a Dockerfile
   commit             Create a new image from a container's changes
   cp                 Copy files/folders between a container and the local filesystem
   create             Create a new container
   diff               Inspect changes on a container's filesystem
   events             Get real time events from the server
   exec               Run a command in a running container
   export             Export a container's filesystem as a tar archive
   history            Show the history of an image
   images             List images
   import             Import the contents from a tarball to create a filesystem image
   info               Display system-wide information
   inspect            Return low-level information on a container or image
   kill               Kill a running container
   load               Load an image from a tar archive or STDIN
   login              Log in to a Docker registry
   logout             Log out from a Docker registry
   logs               Fetch the logs of a container
   network            Manage Docker networks
   pause              Pause all processes within a container
   port               List port mappings or a specific mapping for the CONTAINER
   ps                 List containers
   pull               Pull an image or a repository from a registry
   push               Push an image or a repository to a registry
   rename             Rename a container
   restart            Restart a container
   rm                 Remove one or more containers
   rmi                Remove one or more images
   run                Run a command in a new container
   save               Save one or more images to a tar archive
   search             Search the Docker Hub for images
   start              Start one or more stopped containers
   stats              Display a live stream of container(s) resource usage statistics
   stop               Stop a running container
   tag                Tag an image into a repository
   top                Display the running processes of a container
   unpause            Unpause all processes within a container
   update             Update configuration of one or more containers
   version            Show the Docker version information
   volume             Manage Docker volumes
   wait               Block until a container stops, then print its exit code

You can search for images available on Docker Hub by using the docker command with the search subcommand.

$ sudo docker search ubuntu

The sample output should be like this –

NAME
AUTOMATED                         DESCRIPTION                                     STARS    OFFICIAL          
ubuntu                            Ubuntu is a Debian-based Linux operating s...    3953       [OK]
ubuntu-upstart                   Upstart is an event-based replacement for ...       62       [OK]
rastasheep/ubuntu-sshd
[OK]                           Dockerized SSH service, built on top of of...          26    
torusware/speedus-ubuntu
[OK]                            Always updated official Ubuntu docker imag...       26
ubuntu-debootstrap
[OK]                           debootstrap --variant=minbase --components...       24          [OK]
nickistre/ubuntu-lamp
[OK]                              LAMP server on Ubuntu 6 [OK]
nickistre/ubuntu-lamp-wordpress
[OK]                               LAMP on Ubuntu with wp-cli installed 5 [OK]
nuagebec/ubuntu
[OK]                              Simple always updated Ubuntu docker images...       4
nimmis/ubuntu  
[OK]                               This is a docker images different LTS vers...       4
maxexcloo/ubuntu
[OK]                               Docker base image built on Ubuntu with Sup...       2
partlab/ubuntu
[OK]                              Simple Ubuntu docker images.                         1
darksheer/ubuntu
[OK]                               Base Ubuntu Image -- Updated hourly                   1
admiringworm/ubuntu
[OK]                               Base ubuntu images based on the official u...       1
jordi/ubuntu
[OK]                            UbuntuBase Image                                        1
webhippie/ubuntu
[OK]                              Docker images for ubuntu                            0
teamrock/ubuntu TeamRock's
[OK]                               Ubuntu image configured with AW...                0
esycat/ubuntu  
[OK]                                Ubuntu LTS                                        0
life360/ubuntu
[OK]                              Ubuntu is a Debian-based Linux operating s...       0
widerplan/ubuntu
[OK]                              Our basic Ubuntu images.                         0
konstruktoid/ubuntu
[OK]                              Ubuntu base image                                  0
lynxtp/ubuntu
[OK]                              https://github.com/lynxtp/docker-ubuntu             0
datenbetrieb/ubuntu  
[OK]                             custom flavor of the official ubuntu base ...       0
ustclug/ubuntu
[OK]                              ubuntu image for docker with USTC mirror             0
uvatbc/ubuntu  
[OK]                               Ubuntu images with unprivileged user             0
rallias/ubuntu  
[OK]                                  Ubuntu with the needful                         0

To see the images that have been downloaded to your computer, use the following command –

$ sudo docker images

The sample output should be like this-

REPOSITORY          TAG                IMAGE ID                CREATED          SIZE
hello-world       latest                94df4f0ce8a4          3 weeks ago       967 B

To run the Docker container, use the following command –

$ sudo docker run -it hello-world

Above command runs hello-word container. the sample output should be like this –

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.
   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 Hub account:
   https://hub.docker.com

For more examples and ideas, visit:
   https://docs.docker.com/engine/userguide/

To listing Docker Containers, use the following command –

$ sudo docker ps

The sample output should be like this –

CONTAINER ID PORTS         IMAGE        COMMAND                CREATED                         STATUS                             NAMES
f7c79cc556dd              ubuntu        "/bin/bash"          3 hours ago                     Up 3 hours                         silly_spence

After this article, you will be able to understand – How To Install and Use Docker on Ubuntu 16.04 we will come up with more Linux based tricks and tips. Keep reading!

raja
Published on 20-Jan-2020 11:36:54
Advertisements