Install Docker on Ubuntu 16.04 LTS

Docker is an open source project to pack, ship and run any application as a lightweight container.

Docker containers wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in.

Installation

The docker installation package is available in official Ubuntu repository. Let’s use the docker repository in order to get the latest version of docker.

krishna@ubuntu:~$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Executing: /tmp/tmp.MwudvrXVEs/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)
krishna@ubuntu:~$ 
krishna@ubuntu:~$ sudo apt-add-repository 'deb https://apt.dockerproject.org/repo ubuntu-xenial main'

Update the package database with docker packages for newly added repository.

krishna@ubuntu:~$ sudo apt-get update
krishna@ubuntu:~$ apt-cache policy docker-engine
docker-engine:
 Installed: (none)
 Candidate: 17.04.0~ce-0~ubuntu-xenial
 Version table:
 17.04.0~ce-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 17.03.1~ce-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 17.03.0~ce-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.13.1-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.13.0-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.12.6-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.12.5-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.12.4-0~ubuntu-xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.12.3-0~xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.12.2-0~xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.12.1-0~xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.12.0-0~xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 1.11.2-0~xenial 500
 500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
 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
krishna@ubuntu:~$

Install Docker

krishna@ubuntu:~$ sudo apt-get install docker-engine
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
 aufs-tools cgroupfs-mount libltdl7
Suggested packages:
 mountall
The following NEW packages will be installed:
 aufs-tools cgroupfs-mount docker-engine libltdl7
0 upgraded, 4 newly installed, 0 to remove and 46 not upgraded.
Need to get 18.1 MB of archives.
After this operation, 85.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 aufs-tools amd64 1:3.2+20130722-1.1ubuntu1 [92.9 kB]
Get:2 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 docker-engine amd64 17.04.0~ce-0~ubuntu-xenial [18.0 MB]
Get:3 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 cgroupfs-mount all 1.2 [4,970 B]
Get:4 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libltdl7 amd64 2.4.6-0.1 [38.3 kB]
Fetched 18.1 MB in 4s (3,987 kB/s) 
Selecting previously unselected package aufs-tools.
(Reading database ... 59647 files and directories currently installed.)
Preparing to unpack .../aufs-tools_1%3a3.2+20130722-1.1ubuntu1_amd64.deb ...
Unpacking aufs-tools (1:3.2+20130722-1.1ubuntu1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../cgroupfs-mount_1.2_all.deb ...
Unpacking cgroupfs-mount (1.2) ...
Selecting previously unselected package libltdl7:amd64.
Preparing to unpack .../libltdl7_2.4.6-0.1_amd64.deb ...
Unpacking libltdl7:amd64 (2.4.6-0.1) ...
Selecting previously unselected package docker-engine.
Preparing to unpack .../docker-engine_17.04.0~ce-0~ubuntu-xenial_amd64.deb ...
Unpacking docker-engine (17.04.0~ce-0~ubuntu-xenial) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu16) ...
Setting up aufs-tools (1:3.2+20130722-1.1ubuntu1) ...
Setting up cgroupfs-mount (1.2) ...
Setting up libltdl7:amd64 (2.4.6-0.1) ...
Setting up docker-engine (17.04.0~ce-0~ubuntu-xenial) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for systemd (229-4ubuntu16) ...
Processing triggers for ureadahead (0.100.0-19) ...
krishna@ubuntu:~$

Docker is installed now, you can verify the docker status

krishna@ubuntu:~$ systemctl status docker.service
● docker.service - Docker Application Container Engine
 Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
 Active: active (running) since Wed 2017-04-19 10:15:57 IST; 2min 51s ago
 Docs: https://docs.docker.com
 Main PID: 5246 (dockerd)
 CGroup: /system.slice/docker.service
 ├─5246 /usr/bin/dockerd -H fd://
 └─5251 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --stat

Installing docker also provide us the docker client (Docker command line utility).

Docker Command and Usage

Docker command requires root privilege to run by default. It can also run by a docker group, which is automatically created during the installation of docker. Let’s add username to docker group.

krishna@ubuntu:~$ sudo usermod -aG docker krishna

Here krishna is the username added to docker group.

Docker command output

kkrishna@ubuntu:~$ docker

Usage: docker COMMAND

A self-sufficient runtime for containers

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

Management Commands:
 container Manage containers
 image Manage images
 network Manage networks
 node Manage Swarm nodes
 plugin Manage plugins
 secret Manage Docker secrets
 service Manage services
 stack Manage Docker stacks
 swarm Manage Swarm
 system Manage Docker
 volume Manage volumes

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 to files or directories 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 Docker objects
 kill Kill one or more running containers
 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
 pause Pause all processes within one or more containers
 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 one or more containers
 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 (streamed to STDOUT by default)
 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 one or more running containers
 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
 top Display the running processes of a container
 unpause Unpause all processes within one or more containers
 update Update configuration of one or more containers
 version Show the Docker version information
 wait Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.
krishna@ubuntu:~$

Docker info is available with command ‘sudo docker info’

krishna@ubuntu:~$ sudo docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.04.0-ce
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 0
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: 
containerd version: 422e31ce907fd9c3833a38d7b8fdd023e5a76e73
runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228
init version: 949e6fa
Security Options:
 apparmor
 seccomp
 Profile: default
Kernel Version: 4.4.0-62-generic
Operating System: Ubuntu 16.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.953GiB
Name: ubuntu
ID: LJ33:RNLE:46UH:TCUB:SNBJ:5OM4:F4ZA:L33K:LGTE:JOP7:R3AD:QXDF
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support
krishna@ubuntu:~$

Docker Images

Docker containers are run from docker images. We can pull these images from docker hub. Let’s step simple image for test.

krishna@ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pull complete 
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
Status: Downloaded newer image for hello-world:latest

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 ID:
 https://cloud.docker.com/

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

krishna@ubuntu:~$

Conclusion

Docker is pretty easy to install. There is lot more to learn.

9 Comments

  1. Pingback: Setup MySQL Docker Container - Exabig

  2. Pingback: Setup MongoDB Docker Container - Exabig

  3. Pingback: Setup Keycloak Docker Container - Exabig

  4. Krishna Prajapati

    Recommended method for docker install

    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
    sudo apt-key fingerprint 0EBFCD88
    sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
    sudo apt-get update
    sudo apt-get install docker-ce
    sudo usermod -aG docker krishna

  5. Pingback: Setup Keycloak-mysql Docker Container - Exabig

  6. Pingback: MongoDB Backup & Restore in Docker Container - Exabig

  7. Krishna Prajapati

    docker cp can be used to copy file from docker to host

    docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
    docker cp hwer:./sfrehke.sql.gz /tmp

  8. Krishna Prajapati

    Quick Docker Install

    curl -fsSL https://get.docker.com/ | sh

  9. Krishna Prajapati

    DOCKER
    ——————————-
    sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
    sudo apt-key fingerprint 0EBFCD88
    sudo add-apt-repository \
    “deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable”
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io
    apt-cache madison docker-ce

    DOCKER COMPOSE
    ————————————–
    sudo curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    docker-compose –version

Leave a Reply

Your email address will not be published. Required fields are marked *