Knowledge Base - How To Install Docker and Use Containers in Ubuntu

Virtualization is a really a hot topic and something that has made the cloud possible. Without virtualization in place, we would have never reached the point where we stand right now.  If you have ordered for a VMCentral VPS, then you are using virtualization.

 

We will be discussing about Container virtualization in this tutorial, that is now being adopted by many developers as a method to build applications, ship applications and run it anywhere.

 

Container virtualization is completely different compared to hypervisor based virtualization technologies. In hypervisor based virtualization technologies (like KVM, Xen Etc), an entirely new operating system is installed as a guest machine, and hypervisor will be controlling the resource allocations(A guest vm is seen as a complete separate host by the host system.).

 

However, in container based virtualization technologies, the host kernel is used by the containers and a complete different namespace is created for network, processes etc. As container based virtualization uses the host kernel, you cannot install another operating system which is not compatible with the host kernel. Basically, you cannot have a windows container running on top of a Linux host machine.

 

Docker is nothing but an engine for managing containers on the host system. It adds features such as automated deployment of containers, automated building of container images, which can be shipped to a central registry. Users can download their required images from the central registry and start the container using that image with their own required variables and parameters.

In this tutorial, we will be walking through the steps that needs to be taken to install Docker containers inside Ubuntu distribution.

 

Docker has three different important building blocks. They are mentioned below.

 

  • Docker Daemon (This is the daemon that manages the containers running on the system)
  • Docker CLI (This is used as an interface to communicate and send commands to Docker Daemon)
  • Docker Images (Docker images can be viewed as the source code of the container. From which a container can be started)

 

Docker uses LXC (Linux Containers), which isolates each containers with the help of kernel level features such as namespace, chroot, cgroups etc.

 

Although Docker uses LXC, it offers much more than just LXC. The main feature that gives an edge to Docker is its portability.

 

ie: Build your container image anywhere, and simply pull it and start it from anywhere. And if it worked on your test environment, it will work in any environment. This is because an image contains all application components and its dependencies.

 

Docker was mainly built to solve the problem of “Was working in Dev environment, but why is it not working in production”

 

Please note the fact that you need to have a 64 bit operating system to install and run docker containers.

 

 

Step 1:  AUFS support

 

The very first step before we install Docker is to confirm you have AUFS support on your machine. This can be done by installing the below package.

 

sudo apt-get install linux-image-extra-`uname -r`

The above command will install the required dependencies for AUFS support.

 

Now the next step is to add Docker official apt repository to your system. This can be done as shown below.

 

sudo sh -c “wget -qO- https://get.docker.io/gpg | apt-key add -”

Lets now add the URL’s required to fetch docker packages. This can be done as shown below.

 

sudo sh -c “echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list”

apt-get update

Lets now install Docker on our system as shown below.

 

apt-get install lxc-docker

The next thing that we need to is to enable forwarding of packets inside Ubuntu UFW as shown below. To do this, you need to edit the file /etc/default/ufw, so that it has the below line.

 

DEFAULT_FORWARD_POLICY=”ACCEPT”

You can now reload UFW as shown below, so that this forward policy change takes effect.

sudo ufw reload

You should now have Docker service running on your system. You can check the status as shown below.

 

sudo docker info

If docker is not running, you can start it using the below command.

 

sudo service docker start

Now as we discussed earlier, docker images are the building block of the containers. Docker images are the source code for a container. There are many default images available from docker hub (which is entirely made and maintained by docker hub.)

 

The idea is to first pull the docker images to your local system, and then start a container using that image. Let’s see if we have any images already on the system.

 

sudo docker images

As our system is a fresh install of Docker, we do not have an image with us. You can pull any of the publicly available image, using the below command.

 

sudo docker pull ubuntu

You can now recheck the images on your system using the same command

 

sudo docker images

You should see different versions of Ubuntu images as an output of the above command. Things to note, is each image has its own image id and  an image tag, and an image name.

 

While starting a container using an image, we can either call its name, or image id with the tag.

How to run a docker container?

 

As we now have docker installed in our system, also we have pulled Ubuntu images, lets now get started with running our first docker container. This can be done with the below command.

 

sudo docker help

The above command can give you a bit of help with all command line options and parameters.

 

Lets now run our first container as shown below. Running containers is a single docker run command away as shown below.

 

sudo docker run -i -t ubuntu /bin/bash

once the above command completes, you will be placed inside the container bash shell. As soon as you type exit the container will be stopped.

 

The main thing to note about the above docker run command is the fact that the final command /bin/bash is the cmd. The container will exist till that /bin/bash process will exist. So that is the reason when you exit from the container, the container gets killed.

You can get the list of running docker containers with the below command.

 

sudo docker ps

If you want to get the list of all containers(including the ones that are not in running state), then you can use the below command.

 

sudo docker ps -a

We have seen the method where the container got excited as soon as the bash shell (well the final CMD process passed to docker run) was stopped.

 

There is another method where you can run a container in background mode, so that it does not get stopped like we saw earlier.

 

sudo docker run –name test -d ubuntu /bin/sh

In the above method, the container will exist in running condition, even after you exit the terminal. Also we have named the container as test (if you do not name your container, docker daemon will assign a random name to it.)

 

Lets say you do not have the Ubuntu image in your local system. In that case, the above command will pull Ubuntu image from docker hub.

 

You can stop a running container using the below command.

 

sudo docker stop <container-id>

Container-id can be found from the output of docker ps command.  You can start a stopped container as shown below.

 

sudo docker start container-id

You can also use the container name instead of container-id. If you want to remove a container altogether from the system, docker rm command can be used.

 

sudo docker rm <container-id>