Monday, August 27, 2018

How to install Docker in Ubuntu 16.04 LTS

Docker:- 

Docker is the most revolutionized technology in virtualization world nowadays. Docker is actually an open source project which provides container technology. A container is a lightweight VM(virtual machine) or a process which allows us to install Linux based applications inside it. The container doesn’t have its own Kernel, RAM, CPU and Disk but it uses the underlying OS kernel, RAM, CPU cores and Disk.

The container provides process base isolation where virtual machines provide resource-based isolation. The main benefit of containers is that we can provide a container in less than a second because launching containers is actually starting a process in Linux.



In this article, we will discuss how to setup Docker on Ubuntu server 16.04. Prerequisite of Docker is listed below :

Step: 1- Update the package database: 
Let's update the package database to start the installation of docker using apt-get update command.
amar@docker:~$ sudo apt update
Step: 2- Add GPG key for docker's official repository
Install packages to allow apt to use a repository over HTTPS:
amar@docker:~$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
Docker package is not available in the default Ubuntu 16.04 server’s repositories. Let’s add the GPG key for docker repository using below command.
amar@docker:~$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.
amar@docker:~$ 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) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22
Now add docker repository using 'apt-add-repository' command.
amar@docker:~$ sudo sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
Update the package index by updating the package database
amar@docker:~$ sudo apt update
Step: 3- Install docker ce package using the command below
Use the following command to install the latest version of Docker.
amar@docker:~$ sudo apt install docker-ce
Step: 4- Verify your Docker-ce installation by running the command below
amar@docker:~$ sudo docker run hello-world
If your Docker installation is correct you will see the message like below.

Search Docker image using docker search command.
Let's assume you would like to search Ubuntu image, run the command below.
amar@docker:~$ sudo docker search ubuntu
Your search result will be like this.
Download Docker image using "docker pull" command.
amar@docker:~$ sudo docker pull ubuntu:latest

latest: Pulling from library/ubuntu
124c757242f8: Pull complete
2ebc019eb4e2: Pull complete
dac0825f7ffb: Pull complete
82b0bb65d1bf: Pull complete
ef3b655c7f88: Pull complete
Digest: sha256:72f832c6184b55569be1cd9043e4a80055d55873417ea792d989441f207dd2c7
Status: Downloaded newer image for ubuntu:latest
The above command will download the latest Ubuntu image. To list all the locally available images run the command " sudo docker images".
amar@docker:~$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              16508e5c265d        4 days ago          84.1MB
hello-world         latest              2cb0d9787c4d        6 weeks ago         1.85kB
Launch a container using "docker run" command.
amar@docker:~$ sudo docker run -it --name container1 ubuntu
Where in the above command.

sudo docker run                          It is a command.

-it                                              "i" for interactive and "t" is terminal 

--name                                       To define container name

ubuntu                                        Docker image name

To stop the container type 'exit' in the container console. if you don't want to stop the container but want to go back docker main console press 'ctrl+p+q' in the container console.

Verify how many container running currently 
To verify how many containers currently running use the command ' docker ps'
amar@docker:~$ sudo docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7049cf602a4f        ubuntu              "/bin/bash"         5 minutes ago       Up 5 minutes                            container1If you want to verify all stop or running containers run the command 'docker ps -a'.
amar@docker:~$ sudo docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS               PORTS           NAMES
7049cf602a4f        ubuntu              "/bin/bash"         8 minutes ago       Up 8 minutes                                    container1
61ef22bee07c        ubuntu              "/bin/bash"         24 minutes ago      Exited (0) 24 minutes ago                       hardcore_chatterjee
6b032efe25fd        hello-world         "/hello"            43 minutes ago      Exited (0) 43 minutes ago                       competent_panini
Stop a container using 'docker stop' command.
Let's stop recently created container1
amar@docker:~$ sudo docker stop container1
container1
amar@docker:~$
Start a container in detach mode
Let's suppose you want to start a container but don't want to go into the console '-dit' will launch the container in the background. 
amar@docker:~$ sudo docker run -dit --name container2 ubuntu
50e05cefdae5759b8c9565f15f8a5b3525875ed5633da116294811a67c620f3c
amar@docker:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
50e05cefdae5        ubuntu              "/bin/bash"         11 seconds ago      Up 10 seconds                           container2
amar@docker:~$
Binding Container ports to Docker host
By Default Containers can reach to the outside world and each outgoing connection will appear as if the request is coming from docker’s host IP address but from outside world, no one reaches to containers.

Using port translation method we allow outsiders to reach our containers.

Let’s suppose I want to host a website inside a container and this container will be accessed by Web developers via ssh

amar@docker:~$ sudo docker run -dit -p 8080:80 -p 222:22 --name container1 ubuntu:apache2
352878d8f76a5c30e36680dc0ad178d5d4a1fb3fa84e428a055acfd2e4541dd5
amar@docker:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                       NAMES
352878d8f76a        ubuntu:apache2      "/bin/bash"         6 seconds ago       Up 6 seconds        0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   container1
amar@docker:~$
Delete container using 'docker rm' command 
Use ‘docker rm‘ command to delete containers either based on their names and ids, Before deleting a container make sure it is stopped.
amar@docker:~$ sudo docker rm container1 
Remove Docker Image using 'docker rmi' command
‘docker rmi‘ command is used to delete or remove docker images from host’s local image repository
amar@docker:~$ sudo docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34
Deleted: sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5
amar@docker:~$
Thanks for reading my blog. Please leave your comments and suggestion to make it better.

1 comment: