How to manage Linux containers with Docker on Ubuntu

While full hardware virtualization such as KVM, Xen or Hyper-V is great at running fully isolated instances of multiple operating systems on a physical host, it comes with various overheads in terms of performance, resource, and provisioning time. Depending on your use cases, full machine virtualization may actually not be necessary.

An alternative lightweight virtualization approach is so-called Linux Containers (LXC), which provides operating system level virtualization. Without the overhead of running virtual machines, LXC allows you to run multiple instances of full Linux operating system within lightweight container sandbox. Containers can be very useful when you set up a reproducible development/test environment or deploy applications within secure sandboxes.

Docker is an open-source tool which was developed to facilitate the deployment of Linux containers. Docker is fast becoming a de-facto standard for container technologies, being embraced in major Linux distros such as Ubuntu and Red Hat.

In this tutorial, I am going to demonstrate how to manage Linux containers with Docker on Ubuntu 14.04. Note that instructions may be slightly different for earlier versions of Ubuntu. If you want to try out Docker on Fedora or CentOS, refer to this tutorial.

At this time, the Docker package available on Ubuntu only supports 64-bit systems. To run it on 32-bit machine, you will need to build 32-bit version of Docker from source.

Install Docker

Installing Docker is easy with apt-get command.

$ sudo apt-get install docker.io

To allow non-root user to run Docker, add yourself to docker group. The command below will allow the current user to run Docker without root permission.

$ sudo usermod -a -G docker $USER

Log out and then re-login to activate group membership change.

Next, edit the Docker configuration file to update the location of the Docker binary.

$ sudo vi /etc/default/docker.io
DOCKER="/usr/bin/docker.io"

Restart Docker service.

$ sudo service docker.io restart

Manage Docker Containers

If you want to start a new Docker container of Ubuntu operating system, first pull Ubuntu Docker image first. The command below will download Docker image over a network.

$ docker pull ubuntu

You can start a Ubuntu Docker in an interactive mode as follows. The last argument "/bin/bash" is the command that will be executed inside a container once it is launched, in this case, a simple bash shell.

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

The above command will launch a Ubuntu container immediately (which is the beauty of containers!), and give you a shell prompt inside the container. At this point, you should be able to access a full Ubuntu operating system inside a sandboxed environment.

To exit a Docker container, type "exit" at the prompt inside the container.

You can launch containers in different flavors. For example, to start a Fedora container:

$ docker.io run -i -t fedora /bin/bash

If a Fedora Docker image is not available locally, the command will automatically download the image first, and then launch a Docker.

If you want to launch a container with a particular distro release, you can also do that. For example, to start a Ubuntu 13.04 Docker:

$ docker.io run -i -t ubuntu:13.04 /bin/bash

Container Networking

Docker uses Linux bridge to interconnect containers with each other, and to connect them to external networks. After installing Docker, you should see docker0 Linux bridge created automatically by default. Every container you create will be connected to docker0 bridge interface.

Custom Linux Bridge

If you want, you can use a custom Linux bridge to interconnect containers. For that, you can create a custom bridge and configure it as follows. You can assign a separate subnet to the bridge, and have Dockers assigned IP addresses from the subnet. I am going to use 10.0.0.0/24 as a Docker subnet.

$ sudo apt-get install bridge-utils
$ sudo brctl addbr br0
$ sudo ifconfig br0 10.0.0.1 netmask 255.255.255.0

To make the custom bridge used by Docker, add "-b=br0" to DOCKER_OPTS variable in /etc/default/docker.io, and restart Docker service.

$ sudo service docker.io restart

At this point, any new container will be connected to br0, and its IP address will automatically be assigned from 10.0.0.0/24.

Other Customizations

There are several other ways to customize the default network settings of Docker, mostly by tweaking DOCKER_OPTS variable in /etc/default/docker.io.

  • "-dns 8.8.8.8 -dns 8.8.4.4": specify the DNS servers used by a container.
  • "-icc=false": make containers isolated from each other.

Troubleshooting

1. You encounter the following error when running docker.io command.

dial unix /var/run/docker.sock: no such file or directory

The error may be because Docker daemon is not running. Check the status of Docker daemon, and make sure to start it first.

$ sudo service docker.io status
$ sudo service docker.io start

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

6 thoughts on “How to manage Linux containers with Docker on Ubuntu

  1. I ran this under VMWare which might account for the second issue, but I doubt the first. What did I do wrong? After installing the docker.io package and giving the first few commands (above) to initialize things I get:

    $ docker pull
    The program 'docker' is currently not installed. You can install it by typing:
    sudo apt-get install docker
    $ docker.io pull

    Usage: docker pull NAME

    Pull an image or a repository from the registry

    -t, --tag="": Download tagged image in repository
    $ docker.io pull ubuntu
    2014/05/02 14:02:04 dial unix /var/run/docker.sock: no such file or directory
    $ uname -a
    Linux guava.example.com 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:08:14 UTC 2014 i686 i686 i686 GNU/Linux

    $ sudo apt-get install docker
    ...
    $ docker pull
    Segmentation Fault or Critical Error encountered. Dumping core and aborting.
    Aborted (core dumped)

    • In Ubuntu/Debian, the command "docker" is taken by something else. So do not install "apt-get install docker" nor run "docker" command. You are supposed to run "docker.io" command.

      As I mentioned in the article, to solve the "dial unix /var/run/docker.sock: no such file or directory" error, make sure that docker daemon is running in the background:

      $ ps aux|grep docker
      root       966  0.0  0.1 361500  9312 ?        Sl   09:09   0:00 /usr/bin/docker.io -d -dns 8.8.8.8 -dns 8.8.4.4
      

      To start a docker daemon:

      $ sudo service docker.io start
      • Here's the problem:

        $ sudo tail -4 /var/log/upstart/docker.io.log
        2014/05/02 14:36:55 The docker runtime currently only supports amd64 (not 386). This will change in the future. Aborting.
        2014/05/02 14:36:55 The docker runtime currently only supports amd64 (not 386). This will change in the future. Aborting.
        2014/05/02 14:36:55 The docker runtime currently only supports amd64 (not 386). This will change in the future. Aborting.
        2014/05/02 14:36:55 The docker runtime currently only supports amd64 (not 386). This will change in the future. Aborting.

  2. Great tutorial, I have my first container up and running in less than 5 minutes.

    I wish to see more about docker soon, how to run multiple containers in the cloud, integration with virtual switches, fine tuning, etc.

    Thanks.

Leave a comment

Your email address will not be published. Required fields are marked *