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.10. 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.

Note that on Ubuntu 14.04 and earlier, the Docker configuration file is loated at /etc/default/docker.io, and its binary is /usr/bin/docker.io (due to naming conflict with a system tray app called docker).

Restart Docker service.

$ sudo service docker restart
(or 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, and cache it locally. The first time you run this command, it will take a couple of minutes to finish.

$ docker pull ubuntu

You can start a Ubuntu Docker in an interactive mode as follows.

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

In the above command, 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. So it 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 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. So it will take a couple of minutes to start a new Docker. Subsequent launch of additional Dockers of the same image will be finished instantly.

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 run -i -t ubuntu:13.04 /bin/bash

To see a list of currently running Docker containers, use the following command.

$ docker ps

If you want to see a list of all (active or terminated) Docker containers, run this command instead.

$ docker ps -a

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.

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.

6 thoughts on “How to manage Docker containers on Ubuntu”

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

$ 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.

Unfortunately that article no longer works as the server.go file that he modifies to allow it to build is depreciated and the modified code no longer exists. I have been looking through the code trying to find somewhere that has similar code, but I'm not having any luck.