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 higher

Step:1 Update the Package database using below command

Let’s first update the packages database using ‘apt update‘ command

Step: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.

Now add the docker repository using ‘apt-add-repository‘ command

Refresh the package index again as we have added docker repository

Step: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.

Once the docker-engine is installed, start and enable docker service using following commands

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

Add your user name to docker group using ‘usermod‘ command, in my case user name is ‘linuxtechi‘

Docker 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’ command

Let’s assume we want search latest centos docker image.

Download Docker images using ‘docker pull’ command

Let’s suppose we want to download Ubuntu 16.04 docker image.

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.

Provision or launch a container using docker run command

Let’s suppose we want provision a Ubuntu 16:04 container.

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 running

Using ‘docker ps‘ command we can list  running containers and to list all containers either they are running or stopped use ‘docker ps -a‘

Stopping a Container using docker stop command

Let’s stop my recently provisioned container “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}‘

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

Binding Container ports to Docker engine Host

By Default Containers can reach to outside world and each outgoing connection will appear as if request is coming form docker’s host IP address but from outside world no one reach to containers.

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

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

In above command -p option is used for pating (port  address translation), From the outside world if any one try to ssh my docker’s host ip address on port 2000 , then its request will be forwarded to 22 port on container “myserver1” and similarly request on 8000 port will be forwarded to 80 port on the container (myserver1)

Commit changes of a container to a Docker Image

In docker command we have commit option to save the changes of container to a docker image. Let’s assume in above container we have installed web server and want save these changes to a docker image so that in future we can launch web server container from docker image.

Terminate / delete containers 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.

To delete a running container

Removing Docker images from Host’s Local image repository

‘docker rmi‘ command is used to delete or remove docker images from host’s local image repository

Note: We can also upload our customize docker images to the docker hub using docker push command but first we need to create our account on docker hub and run below commands from the terminal

That’s all for this article. Hope you got an idea how to work with containers.Please share your valuable feedback and comments.

Show more