Docker is the most revolutionized technology in virtualization world now a days. Docker is actually an open source project which provides container technology. A container is a light weight VM(virtual machine) or a process which allows us to install linux based applications inside it. Container don’t have its own Kernel, RAM, CPU and Disk but it uses the under lying OS kernel, RAM, CPU cores and Disk.
Container provides process base isolation where virtual machines provides resource based isolation. The main benefit of containers is that we can provision a container in less than a second because launching a 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 :
64-bit OS Kernel version 3.10 or higherStep:1 Update the Package database using below command
Let’s first update the packages database using ‘ apt update ‘ command
linuxtechi@docker:~$ sudo apt updateStep:2 Add GPG Key for Docker Official Repository
Docker engine 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.
linuxtechi@docker:~$ sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609DNow add the docker repository using ‘ apt-add-repository ‘ command
linuxtechi@docker:~$ sudo apt-add-repository "deb https://apt.dockerproject.org/repo ubuntu-xenial main"linuxtechi@docker:~$
Refresh the package index again as we have added docker repository
linuxtechi@docker:~$ sudo apt updateStep:3 Install docker engine package using apt command.
Following command will install latest version docker-engine. At the time of writing this article docker version ‘ 1.12.5 ‘ is available.
linuxtechi@docker:~$ sudo apt install docker-engineOnce the docker-engine is installed, start and enable docker service using following commands
linuxtechi@docker:~$ sudo systemctl start dockerlinuxtechi@docker:~$ sudo systemctl enable docker
Now this server will work as Docker Engine or Container Engine. A Bridge is also created which will acts as L2 switch and will provide IP address to the containers from its own DHCP server.
Verify the Docker version and other key parameters of Docker using ‘ docker info ‘ command
linuxtechi@docker:~$ sudo docker info
Add your user name to docker group using ‘ usermod ‘ command, in my case user name is ‘ linuxtechi ‘
linuxtechi@docker:~$ sudo usermod -aG docker linuxtechiDocker installation part is completed now, let’s get familiar with some basic commands of Docker with examples.
Syntax of Docker command :
# docker {options} command {arguments…}
To list the options of docker command, type ‘ docker ‘ on the terminal
Whenever docker engine is installed, default Registry Server is updated in docker command. When we run the docker command to download and search images then it will go the registry server to fetch the mentioned docker image. Though we can change this registry address as per our setup.
Search Docker images using ‘docker search’ commandLet’s assume we want search latest centos docker image.
linuxtechi@docker:~$ sudo docker search centos
Download Docker images using ‘docker pull’ command
Let’s suppose we want to download Ubuntu 16.04 docker image.
linuxtechi@docker:~$ sudo docker pull ubuntu:16.04
Similarly we can download the other Linux OS images as per our requirements
Once the image is download then it is stored locally in docker host image repository. We can list the available images in our local repository using ‘ docker images ‘ command.
linuxtechi@docker:~$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 104bec311bcd 9 days ago 129 MB Provision or launch a container using docker run command
Let’s suppose we want provision a Ubuntu 16:04 container.
linuxtechi@docker:~$ sudo docker run -it --name=mycontainer1 ubuntu:16.04root@b5cdf552b56c:/#
In above Command ‘ i ‘ stands for interactive and ‘ t ‘ stands for terminal and name of the container is ‘ mycontainer1 ‘ and Container image is ‘ubuntu:16.04’
Note: In case if we don’t mentioned OS version then it will try to provision latest one.
To stop the container type ‘ exit ’ in container console. If you don’t want to stop the container but want go back to docker engine console, the type ‘ ctrl+p+q ‘ in container console.
Verify how many containers are currently runningUsing ‘ docker ps ‘ command we can list running containers and to list all containers either they are running or stopped use ‘ docker ps -a ‘
linuxtechi@docker:~$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5cdf552b56c ubuntu:16.04 "/bin/bash" 9 minutes ago Up 9 minutes mycontainer1
linuxtechi@docker:~$ Stopping a Container using docker stop command
Let’s stop my recently provisioned container “mycontainer1”
linuxtechi@docker:~$ sudo docker stop mycontainer1mycontainer1
linuxtechi@docker:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5cdf552b56c ubuntu:16.04 "/bin/bash" 15 minutes ago Exited (0) About a minute ago mycontainer1 Start and attach to the container ‘mycontainer1’
use ‘ docker start {container_name} ’ command to start a container and to get the console of container use the command ‘ docker attach {container_name} ‘
linuxtechi@docker:~$ sudo docker start mycontainer1mycontainer1
linuxtechi@docker:~$ sudo docker attach mycontainer1
root@b5cdf552b56c:/# Starting a Container in detach mode.
Let’s suppose we want provision one more container with the name ‘mycontainer2’ from centos7 docker image in detach mode( i.e container will be launched in the background and will not get console), to get the console use docker attach command
linuxtechi@docker:~$ sudo docker run -dit --name=mycontainer2 centos:75efb063260c8d328cf685effa05a610dfbf55ef602c7b14296d27668d9ff004d
linuxtechi@docker:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5efb063260c8 centos:7 "/bin/bash" 28 seconds ago Up 28 seconds mycontainer2
b5cdf552b56c ubuntu:16.04 "/bin/bash"