Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

How To Install Docker on Ubuntu 16.04 (Quick Start)

SelvakumarSelvakumar

Introduction

Docker is becoming a trend nowadays because of two major reasons.

  1. Requires Less resource
  2. Saves a lot of time.

Docker is easy to use application which creates a container for holding applications and their required dependencies.

This container will run on the Operating system. And the Applications will be running inside the container.

The container will be running on the operating system.

The main advantage of Docker is its support for the distributed system.

Once you set the applications and its dependencies, you can easily deploy the containers in remote servers and start using those applications.

If you need to install some specific applications in many servers, you can just deploy the Docker instead of doing the same thing again and again.

Even if you want to upgrade the software, Upgrading them on the host container itself enough.

install docker on ubuntu 16.04

Prerequisites

  • To install the Docker, we are going to use the Ubuntu 16.04.

  • The non-root user with sudo privileges to execute the administrative commands. Refer this article to create the sudo non-root user.

Installing Docker

You can install Docker from the Ubuntu 16.04 repository.

But:

If you like to install the updated version of Docker, you have to install it from the official repository.

If you have an older version of the docker, you can remove that using uninstall docker ubuntu command.

Follow these commands to install the latest version of Docker from the official repository.

You have to add the GPG key to the system for the Official Docker repository.

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

Now:

You have to add the Docker repository to the APT package system.

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

You will see the following lines running on your Ubuntu terminal.

Hit:1 http://ppa.launchpad.net/certbot/certbot/ubuntu xenial InRelease
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Hit:3 http://us.archive.ubuntu.com/ubuntu xenial InRelease
Get:4 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:5 https://download.docker.com/linux/ubuntu xenial InRelease [29.5 kB]
Get:6 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages [1,966 B]
Get:7 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [573 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [554 kB]
Get:10 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [504 kB]
Get:11 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [487 kB]
Fetched 2,456 kB in 1s (1,787 kB/s)
Reading package lists... Done

After that, you have to update the apt repository.

$ sudo apt-get update

Then, you have to install the Docker from the Docker repository instead of Ubuntu repository (Since it is not the recent version).

Docker Ubuntu image can be used to install docker on Ubuntu.

$ apt-cache policy docker-ce

Once you execute the command, you will get the following output.

docker-ce:
Installed: (none)
Candidate: 17.06.0~ce-0~ubuntu
Version table:
 17.06.0~ce-0~ubuntu 500
    500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 17.03.2~ce-0~ubuntu-xenial 500
    500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 17.03.1~ce-0~ubuntu-xenial 500
    500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 17.03.0~ce-0~ubuntu-xenial 500
    500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages

The docker-ce is not yet installed on the Ubuntu machine.

We are going to install it using the candidate for the Ubuntu 16.04.

Execute the below command to install the Docker.

$ sudo apt-get install -y docker-ce

The Docker will be installed on your machine shortly and you can check the status using the below command.

$ sudo systemctl status docker

You might get an output like below.

docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
Active: active (running) since Thu 2017-07-13 02:36:25 PDT; 20s ago
 Docs: https://docs.docker.com
Main PID: 24530 (dockerd)
CGroup: /system.slice/docker.service
       ├─24530 /usr/bin/dockerd -H fd://
       └─24536 docker-containerd -l unix:///var/run/docker/libcontainerd/doc

Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.271396054-07
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.271691097-07
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.271886165-07
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.272510406-07
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.577302502-07
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.689910678-07
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.739344366-07
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.739724141-07
Jul 13 02:36:25 vps.selva systemd[1]: Started Docker Application Container Engin
Jul 13 02:36:25 vps.selva dockerd[24530]: time="2017-07-13T02:36:25.771313142-07

Executing the Docker command without Administrator Privileges

The next step is to execute the Docker commands without sudo. For that, you have to execute the commands as the user in the docker group.

If you don't, you will get the following output.

Output
docker: Cannot connect to the Docker daemon. Is the docker

daemon running on this host?.
See 'docker run --help'.

After that, you have to add the Docker to the sudo user group.

To do that, use the following command.

$ sudo usermod -aG docker ${USER}

To apply the new group membership, you have to logout and login to the server, or you have to just execute the below command.

$ su - ${USER}

You have to enter the user password. After that, you can check whether the user has been added to the docker group by issuing the following command.

$ id -nG

The output will be like

Output
sammy sudo docker

If you want to add one more user to the Docker user group, use the below format and replace the name with your name.

$ sudo usermod -aG docker username

If you don't use the user from docker group, use the sudo in front of each command.

Docker commands

Docker has many comments and to use them you have to pass a lot of arguments after the command.

Let us see the list of command and the structure of the command.

$ docker [option] [command] [arguments]

If you want to know the Docker sub commands, just use the following command.

$ docker

All of the available subcommands will be displayed.

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

To know more detail about the sub command, just use the below format.

$ docker docker-subcommand --help

For instance, to know about attach subcommand, I am using the above format.

$ docker attach --help

The output is

Usage:  docker attach [OPTIONS] CONTAINER

Attach local standard input, output, and error streams to a running container

Options:
  --detach-keys string   Override the key sequence for detaching a
                         container
  --help                 Print usage
  --no-stdin             Do not attach STDIN
  --sig-proxy            Proxy all received signals to the process
                         (default true)

To know the system information about the docker, use the below format.

$ docker info

Since it is freshly installed Docker, you will get the output like below.

Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.06.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
Log: awslogs fluentd gcplogs gelf journald json-file 
logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 
cfb82a876ecc11b5ca0977d1733adbe58599088a
runc version: 2d41c047c83e09a6d61d464906feb2a2f3c52aa4
init version: 949e6fa
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.4.0-21-generic
Operating System: Ubuntu 16.04 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 992.6MiB
Name: vps.selva
ID: 6BBR:3NUQ:VJ35:Y3BT:SSAL:FTIE:Y4FW:KVZV:BJI3:SZH3:VNGW:FVUV
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

Docker Images

Docker requires the docker images to run. It grabs the docker image from the Docker hub.

The Docker Hub is a docker registry maintained by the creator of the Docker.

If you need to use docker, you have to host your Docker images on the Docker registry.

The important thing is:

It can be accessed by anyone.

You Don't need to worry. We have a solution for that.

The solution is building your private Docker registry. Refer this article to know how to build private Docker registry.

To check your accessibility with the Docker registry, use the following command.

$ docker run hello-world

You will get the following output which indicates that you have made the proper installation.

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.

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/

To search the available docker images on the Docker Hub, just use the sub command 'search'.

$ docker search ubuntu

Here, I am searching for images related to WordPress using the above command structure.

The command will search for all the available images related to WordPress and list them in the output.

$ docker search wordpress

And here is the ouput for that.

NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
wordpress                          The WordPress rich content management syst...   1810      [OK]
bitnami/wordpress                  Bitnami Docker Image for WordPress              42                   [OK]
appcontainers/wordpress            Centos/Debian Based Customizable Wordpress...   34                   [OK]
tutum/wordpress                    Out-of-the-box     Wordpress docker image           25
centurylink/wordpress              Wordpress image with MySQL removed.             14                   [OK]
tutum/wordpress-stackable          Out-of-the-box Wordpress docker image whic...   9
trafex/wordpress                   Lightweight WordPress container with Nginx...   7                        [OK]
armhf/wordpress                    The WordPress rich     content management syst...   7
maximiliend/wordpress              A enhanced docker image of WordPress with ...   4                    [OK]
visiblevc/wordpress                Wordpress development environment               3
scjalliance/wordpress              WordPress with GD and FreeType                  3                    [OK]
deardooley/wordpress               Production-ready minimal Wordpress images.      2                    [OK]
withinboredom/scalable-wordpress   An easy to scale WordPress                      2                    [OK]
bakudankun/wordpress-ja            WordPress Japanese Edition forked from off...   1                    [OK]
owncloud/wordpress                 Simple WordPress image for our websites         1                    [OK]
tobkle/centos-wordpress            Wordpress Container based on CentOS 6.6         1                    [OK]
apsl/wordpress                     Wordpress on LAMP managed with circus. Wor...   1                    [OK]
dsteinkopf/wordpress               wordpress clone plus some php extensions        1                    [OK]
mattjonestechnology/wordpress      This is the standard wordpress image, with...   0                    [OK]
itherz/wordpress                   Wordpress                                           0                    [OK]
starkandwayne/wordpress                                                            0
erikzenker/wordpress               wordpress docker image                          0
clintsharp/wordpress               Wordpress Blog                                  0
derjudge/wordpress                 1:1 clone of docker-    library/wordpress with...   0                    [OK]
mattti/wordpress                   Wordpress adjusted     to my own needs              0                    [OK]

Here the OK at the official column indicates that the images are supported and maintained by the company.

Once you choose the specific images to use, use the 'pull' command to download the image and use the 'run' subcommand.

Here, I am going to use the docker pull ubuntu command to download the image.

$ docker pull wordpress

The download will start and images will be downloaded

Using default tag: latest
latest: Pulling from library/wordpress
9f0706ba7422: Pull complete
4c407763908f: Pull complete
82e2bc3a45c1: Pull complete
c84e1013aed1: Pull complete
a3b5e03d7e24: Pull complete
917f836a88be: Pull complete
b2dc54431819: Pull complete
a60b574790b8: Pull complete
b792b0df0041: Pull complete
7c222ab9588d: Pull complete
5697c60bd5fe: Pull complete
8dd7a4634930: Pull complete
a36dd3681195: Pull complete
f6c62f823f86: Pull complete
3cf65373ee2b: Pull complete
c5947a44dcd1: Pull complete
bd988c316a53: Pull complete
8fe391132a9d: Pull complete
Digest:     sha256:d0a44dc70e04a576e464d2b9063dde3080a5b23407f89e1a731a    5226aeb47a66
Status: Downloaded newer image for wordpress:latest

Once you downloaded the images, you can run them using the below command.

$ docker run wordpress

The command will run the mentioned image.

If the image is not downloaded at the time of the command execution, the 'run' command itself download the image and then run it.

Once you run the command, you will get the following output.

WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Jul 13 10:36:08.094297 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.31 configured -- resuming normal operations
................

If you want to see the available docker images which were downloaded to your system, execute the below command.

$ docker images

The available docker images will be displayed below.

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              fcb67315d99b        38 hours ago        406MB
hello-world         latest              1815c82652c0        4 weeks ago         1.84kB

You can modify the images and update them according to their new requirement.

This is how the downloaded images can be used to create new images and they can be uploaded to the Docker hub.

Run the Docker Container

Docker containers can be very interactive, unlike the previous one container that exits after displaying the text while executing.

We can make the docker ubuntu container interactive using the -i and -t. We will use both of them combined into -it.

Here, let us run the Ubuntu container on the Docker.

$ docker run -it ubuntu

You will get an output like the following.

root@0921b713fd13:/#

It means that you have entered into the shell command of Ubuntu from the docker container.

You can install software on the Docker Ubuntu using the apt-get method.

To install Apache on the Docker Ubuntu, follow the command.

$ apt-get install apache2

This command will install Apache in the Docker Ubuntu.

Pushing the updated container to Docker Image

After you have downloading the docker images, you can do whatever the changes you want.

For instance, We have installed the Apache in the Container Ubuntu.

Now:

You can push the current container to the docker image.

There are two ways that you can do.

  • Saving the current container in the local system as Docker image.

  • Pushing the current container to Docker image in the Docker hub.

In order to save the updated container to the Docker Image, just exit from the container.

$ exit

Next:

Use the below command to push the updated container to the docker image to the Docker repository.

$ docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name

-m: You have to mention what you have to do with images.

-a: The author name has to be added here. You can add your name if you make any changes and uploading a new docker images.

You would have had an ID while starting the container session.

You can create more than one repository in the Docker Hub. But, if you did not, the default repository name will be the docker hub username.

$ docker commit -m "added apach2" -a "Neel" d9b100f2f636 finid/ubuntu-apache2

When you save the docker image, it will be saved on your local system.

We will see, how to push the docker to the Docker repository.

Now, Just check the docker image list using the below command. You will see the old docker image which you used to create new Docker image.

You will also see the new docker images that you have created.

$ docker images

You will get the list which looks like below

finid/ubuntu-apache2       latest              62359544c9ba        45 seconds ago      206.6 MB
ubuntu              latest              c5f1cf30c96b        7 days ago          120.8 MB
hello-world         latest              94df4f0ce8a4        2 weeks ago         967 B

You can see both of the docker images in the list. One is the source Docker image which you have downloaded to create a new docker image.

Another one is the newly created Docker image from the source.

You can identify the difference when listening to the docker image size.

So, the next time when you run the docker image, run the updated docker image.

Docker containers

There will be a lot of Docker containers which will be either or inactive on your system.

To see the active containers, use the below command.

$ docker ps

The output will look like the following one.

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

To view all the containers regardless of whether they active or inactive.

$ docker ps -a

If you only want to see the latest container, replace -a with -l

$ docker ps -l

If you want to stop any of the running containers, you have to add the container id and execute the command.

$ docker stop "id of the container which you want to stop"

You can identify the container id using the docker ps command.

Copying the Docker Image to Docker repository

After creating a new docker images from the existing one, now, it's time to upload the new docker to the Docker repository.

You can upload the docker image to the Docker repository or any private repositories.

For that, you should have an account in the registry to access them to upload the newly created image.

You have to mention the repository name in the command to access it. After that, you will be prompted to enter the password.

Here is the command for that.

$ docker login -u docker-registry-username

After that, you can push the new image to the repository using the below command.

$ docker push docker-registry-username/docker-image-name

The uploading process will take some time based on your internet speed and after that, you will get the following output.

Output
The push refers to a repository 
[docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: Pushed
5f70bf18a086: Pushed
a3b5c80a4eba: Pushed
7f18b442972b: Pushed
3ce512daaf78: Pushed
7aae4540b42d: Pushed

...

You can login to your docker repository account after pushing and check the status.

If you are facing any error while pushing the docker image then it means that you have not logged in.

The push refers to a repository [docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: Preparing
5f70bf18a086: Preparing
a3b5c80a4eba: Preparing
7f18b442972b: Preparing
3ce512daaf78: Preparing
7aae4540b42d: Waiting
unauthorized: authentication required

You have to log in again and repeat the push attempt again.

Conclusion

What you have seen in the article is very basic steps and there are a lot more in the Docker to do.

We will see ubuntu install docker-compose in the upcoming article. As of now docker-compose latest version is 3.3.

If you still have any doubt or facing any issues while executing the command, just leave a comment here and we will help you to solve the issue.

Also, subscribe to the news letter to receive the upcoming tutorials.

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments