Manage Linux Containers with Docker on Ubuntu

Version: 1.0
Authors: Srijan Kishore
Last edited: 02/Jul/2014

This guide explains how to install Docker and manage virtual machines effectively. Docker is a Open Source Linux virtualistaion platform for developers and system administrators to build, deploy, and execute distributed applications easily. Docker consists of the Docker Engine, a light runtime and virtualisation layer, tools to package and version virtual machines similar to a sourcecode repository and Docker Hub, a service in the cloud to share applications and automate workflow. Docker allows applications quickly assembled from components and eliminates friction between development environments, quality control and production.

1 Preliminary Note

In my case I have Ubuntu 14.04 on which I will be installing docker. In the virtualisation software market we have several full hardware virtualization technologies available like KVM, Xen or Hyper-V. But the full visualizations are too heavy to virtualize single applications on Linux. We can overcome this situation by using Linux Containers which is a good alternate for the operating system level visualization. Linux Containers is very useful in means to have development/test environment in secured bunch of containers. Docker provides Linux Container environment for this purpose.

2 Installation

As I state above I have Ubuntu on which I am going to install docker. In this chapter I will show you 2 options to install docher, in 2a I use the docker version from Ubuntu repository, this version is maintained by Ubuntu for the full 5 years of LTS support, but its not the latest version. In 2b I will use the latest version from Ubuntu ppa repository. As docker is under heavy development, the latest Docker version from 2b) will most likely suit most users. Please use either method 2a or 2b but not noth at the same time!

2a Install from official Ubuntu repository

To install use :

sudo apt-get update
sudo apt-get install docker.io

and create a symlink to make the use on the shell easier.

sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

2b Install latest docker version

To install the latest docker version from docker.io repository, run this command

curl -s https://get.docker.io/ubuntu/ | sudo sh

3 Prepare the shell enviroment

Now I will add my user=srijan to to the docker group

sudo usermod -a -G docker srijan

Or Use:

sudo usermod -a -G docker $USER

Here I will logout my present session & relogin again. Now I will add the docker configuration file to get the system notified for its location.

sudo vi /etc/default/docker.io

DOCKER="/usr/bin/docker.io"


Now restart the service as follows:

sudo service docker.io restart

4 Container Management

I am going to start container with the Ubuntu operating system. I will download the docker image as follows:

 docker pull ubuntu
 Note: pull is used to pull an image or a repository from the registry   

Now I will login into the bash shell of the ubuntu container by using:

 docker run -i -t ubuntu /bin/bash

Just for confirmation I will check my container IP as follows:

root@fd98ee950252:/# ifconfig 
eth0      Link encap:Ethernet  HWaddr 5a:a6:c6:88:f2:48  
          inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::58a6:c6ff:fe88:f248/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:7 errors:0 dropped:2 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:558 (558.0 B)  TX bytes:648 (648.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

root@fd98ee950252:/#

I have an IP of 172.17.0.3 on my container. Similarly I can use other containers also. To exit the container just type:

exit

Similarly you can have other OS containers, e.g.

I want to use Debian container I will use the code:

docker run -i -t debian /bin/bash

If you want to have particular distribution then use:

docker run -i -t ubuntu:12.04 /bin/bash

It will create the ubuntu12.04 container. I will crosscheck it as follows:

root@44b56100fd1f:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
root@44b56100fd1f:/#

4 Building our own images

There are two ways to create a Docker image:

  • Via the docker commit command
  • Via the docker build command with a Dockerfile

The docker commit method is not currently recommended, as building with a Dockerfile is far more flexible and powerful, but we'll demonstrate it to you for the sake of completeness. After that, I will focus on the recommended method of building Docker images: writing a Dockerfile and using the docker build command.

4.1 Using Docker commit to create images

I will create a container, make changes to that container as you would change code, and then commit those changes to a new image.

Let's start by creating a container from the ubuntu image we've used in the past.

docker run -i -t ubuntu /bin/bash
root@73527b8b4261:/#
Note: Please see above root is showing the hostname 73527b8b4261, it is the container name created & it will differ in your case.

Further I will install apache in it:

apt-get install apache2

I have launched the container and then installed Apache within it. Now I am going to use this container as a web server, so I will save it in its current state.
That will save us from having to rebuild it with Apache every time I create a new container. To do this I will exit from the container, using the exit command,
and use the docker commit command.

exit
docker commit 73527b8b4261 srijan/apache2 8ce0ea7a1528

Note: here 73527b8b4261 is my container name & I am using  8ce0ea7a1528 Tag for the container, you can give any tag name or use the same one.

Suppose you forgot the id of the last created container you can use:

docker ps -l -q

It will yield 73527b8b4261

Note: 73527b8b4261 is your last created container name, it will differ in your case.

Let's look at our new image. It can be achieved as follows:

docker images srijan/apache2
srijan@vboxtest:~$ docker images srijan/apache2
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
srijan/apache2      8ce0ea7a1528        741155868ac8        6 minutes ago       207.2 MB
srijan@vboxtest:~$
Note: Here 8ce0ea7a1528 was the tag name which I used at the time of saving the container, 
Note: 741155868ac8 is the IMAGE ID of that container
Note: All these values will difer in your case as at the time of saving the container docker generates random name to them

Now I will save the customized image as follows:

docker commit -m="A new custom image" --author="Srijan Kishore" 73527b8b4261 srijan/apache2:webserver 

It will give the result as:

srijan@vboxtest:~$ docker commit -m="A new custom image" --author="Srijan Kishore" 73527b8b4261 srijan/apache2:webserver
f0367362eb405c513ac002b5cf172a2c0bc6c8212eab91c613f9ee611cf92fec

To run the a container from our new image, we can do so using the docker run command.

docker run -t -i srijan/apache2:webserver /bin/bash

We can crosscheck our commited image as follows:

docker inspect srijan/apache2:webserver
srijan@vboxtest:~$ docker inspect srijan/apache2:webserver
[{
    "Architecture": "amd64",
    "Author": "Srijan Kishore",
    "Comment": "A new custom image",
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/bin/bash"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "HOME=/",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "ExposedPorts": null,
        "Hostname": "",
        "Image": "",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
    "Container": "73527b8b42614f6ecd83fb5f9822d6086988d3b68fd5e32b4afbc7cd415402fd",
    "ContainerConfig": {
        "AttachStderr": true,
        "AttachStdin": true,
        "AttachStdout": true,
        "Cmd": [
            "/bin/bash"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "HOME=/",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "ExposedPorts": null,
        "Hostname": "73527b8b4261",
        "Image": "ubuntu",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": true,
        "PortSpecs": null,
        "StdinOnce": true,
        "Tty": true,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
    "Created": "2014-06-30T12:58:04.973349049Z",
    "DockerVersion": "1.0.1",
    "Id": "f0367362eb405c513ac002b5cf172a2c0bc6c8212eab91c613f9ee611cf92fec",
    "Os": "linux",
    "Parent": "ef83896b7fb99b00b9e0e6ac943826386e7edcef11a3a2f58b42011ab4a4e683",
    "Size": 14463026
}
]srijan@vboxtest:~$

To run the a container from our new image, we can do so using the docker run command.

docker run -t -i srijan/apache2:webserver /bin/bash

Share this page:

0 Comment(s)