Blog

Running containers with Docker

Running containers with Docker
Bookmark and Share

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments. As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs, and any cloud.

This post describes how to run Docker machines with the help of Boot2Docker.

Boot2Docker

Boot2Docker is a lightweight Linux distribution made specifically to run Docker containers. It is currently designed and tuned for development. Using it for any kind of production workloads at this time is highly discouraged.

After boot2docker installation, we can download the boot2docker-vm by typing this command:

this will download and install into VirtualBox a VM.

The boot2docker-vm VM is switched off. To urn it on type the command:

Boot2Docker is now up and running.

The Next step is to set up a Docker machine. Docker Hub hosts a collection of docker machines.

Boot2Docker sets up two network adaptors, one using NAT to allow the VM to download images and files from the internet, and a host only network that Docker container’s ports will be exposed on.

To expose a port you should use a command like this one

To start practicing with Docker let’s ssh-ing into the running boot2docker vm.

Docker

The Docker Engine consists of two parts: a daemon, a server process that manages all the containers, and a client, which acts as a remote control for the daemon.

If you’re loggeg into the boot2docker machine, you can check if the docker daemon is running.

This will verify that the daemon is running and that you can connect to it. If you can see the version number you know you are all set.

I found very useful the [interactive tutorial(https://www.docker.com/tryit/) because the best way to understand Docker is to try it!

Searching for images

The easiest way to get started is to use a container image from someone else. Container images are available on the Docker Hub Registry, a cloud-based collection of applications. You can find them online at Docker Hub as well through the Docker Engine client command line.

To search for a container, you can use the command docker search. For example:

Searched for a “tutorial” container.

Downloading container images

Container images can be downloaded easily using docker pull. For images in the Docker Hub Registry, the name you specify is constructed as /.

To download learn/tutorial container:

With a container Docker can download several layers because a docker images can consists of several layers.

Run a container

You can think of containers as a process in a box. The box contains everything the process might need, so it has the filesystem, system libraries, shell and such, but by default none of these are running. You start a container by running a process in it.

The command docker run takes a minimum of two arguments:

  1. an image name, and
  2. the command you want to execute within that image.

So, for the learn/tutorial container, it is:

With this you have just started a container and executed a program inside of it, when the program stopped, so did the container.

Installing things

Next we are going to install a simple utility, ping, in the container. The image is based upon ubuntu, so you can run the command apt-get install -y ping in the container.

Note that even though the container stops right after a command completes, the changes are not forgotten.

That worked! You have installed a program on top of a base image. Your changes to the filesystem have been kept, but are not yet saved.

Save your changes

After you make changes (by running a command inside a container), you probably want to save those changes. This will enable you to start from this point later. With Docker, the process of saving the state is called committing. Commit basically saves the difference between the old image and the new state.

To do that there are several steps. First use docker ps -l to find the ID of the container you created by installing ping.

The id is 3cba51f3bedc.

The second step is to actually commit the changes:

That worked! Please take note that Docker has returned a new ID. This ID is the image ID.

Run your new image

You have built a complete, self-contained image with the ‘ping’ utility installed named learn/ping. Your image can now run on any host that runs Docker.

Let’s try it now with a ping to the host google.com:

That worked! Note that normally you can use one of Ctrl-C, Ctrl-P, or Ctrl-Q to disconnect (I don’t know why on my Mac none of those three worked). The container will keep running until it will disconnect automatically.

Your image is now a running container. Using docker ps we can see a list of all running containers and using docker inspect. We can see useful information about this container.

Push your image to the Docker Hub Registry

Now that you have verified that your image works, you can share it with others. Remember that you pulled (downloaded) the learn/tutorial image from the Registry? By pushing (uploading) images that you build, you can easily retrieve them to use on other hosts as well as share them with other users.

To do that the command docker push is used:

Ops… it requires to be registered to Docker Hub so that’s all for now.

Leave a Reply

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