Navigation Menu

Background

Why

Docker has quite an amount of buzz around it today because it makes so many things easy that were difficult with virtual machines.

Docker containers makes it easy for Developers, Systems Administrators, Architects, Consultants and others to quickly test a piece of software in a container; much quicker than a virtual machine, and using less resources. The average command in Docker takes under a second to complete.

 

What

Simple Use Cases

  • I need to see the man page from a specific version of RHEL, CentOS or Fedora
  • I need to quickly verify the command line options of a program
  • I need to test the functionality of a specific version of software
  • I need a scratch pad that is NOT my system
  • I need a single daemon running, and I don’t care what distribution of Linux it runs on (see registry below)

 

Complex Use Cases

Docker containers run a single process when started, but complex installations of software which require multiple daemons running simultaneously (RHEV-M, Satellite, etc) can be done. However, they require more engineering work using either Bash scripting or something like: Using Supervisor with Docker or SystemD.
 

Production vs. Development

At the time of this writing Docker has not yet reached version 1.0 and Red Hat Enterprise Linux 7 has not yet been released as generally available, so production workloads are not recommended. Also, take note that some of the examples in this tutorial make use of docker to test software from different versions of CentOS and Red Hat Enterprise Linux; while this will have limited support, it is quite useful for quick testing and reading man pages.

CentOS and Red Hat Enterprise Linux

This tutorial will focus on integration with Red Hat technologies including CentOS and Red Hat Enterprise Linux. As mentioned, at the time of this writing, Docker has not yet reached version 1.0 and Red Hat Enterprise Linux 7 has not yet been released as generally available, so the majority of the hands on portion of this tutorial will use CentOS. Many of these tutorials can also be completed using Fedora.
 

Architecture

One of the key advantages of using Docker is it’s centralized image management server, called a Registry Server. The Docker project maintains a public registry server which hosts images they maintain, as well as images created by the community. This registry service is free to use, as long as the images are public.

As one builds images, they are made up of layers. These layers are shared together in, what is called a repository. Users on the registry can share multiple repositories.

Docker has an official CentOS 6 repository which they support:

This tutorial will use several public CentOS repositories which I have created and shared on Docker’s public registry. I have created a separate repository for each major version of CentOS:

 

OS Virtualization vs. Application Virtualization

Why separate repositories for each major version of Red Hat Enterprise Linux or CentOS? This was a conscious decision because we are working with a full enterprise Linux distribution versus a single application. Historically, it has been best practice to install a fresh copy when upgrading major version of Red Hat Enterprise Linux, and CentOS. While it is possible to upgrade in place and share multiple versions in a single repository, this is not the preferred method with an enterprise operating system.

However, when virtualizing individual applications, it may be very appropriate to upgrade in place and share all versions within a single repository (See section: Set Up a Registry Server).
 

Known Caveats

After reading this tutorial, you may be excited to set up your own registry server. This is easy to do, but has some caveats, so think think through whether it is worth just using a hosted registry server.

  • As of version 0.6.9, the private registry server does not have any kind of authentication support without using an HTTP proxy such as Apache or NginX.
  • As of version 0.6.9, the private registry server has support for search, but the client can not search them by default, so a browser must be used to view the images.

 

Basic Operations

Now we will run through some basic operations to get you up and running

Install Docker

In CentOS 6, this requires the user space tools from the EPEL. Personally, I prefer to disable after installation so that later, full system updates don’t break the system.

 

Test Docker

This will automatically pull the latest CentOS 4 image from the remote repository and cache it locally.

 

Pull an Image

This will pull the latest CentOS 5 image from the remote repository and cache it in the local index. If the image you are pulling is made up of layers, all of the layers will be pulled.

 

List Images

This will list all of the images in the local index and display how they are linked to each other. Every time a new container is spawned from an image, it will create another copy on write image to save it’s changes too. The tree structure will help make things clear.

 

Tag an Image

It makes it easier to deal with images if they are tagged with simple names

 

Run a Container

Notice how easy it is to test a command in CentOS 4

 

Log in to the Hosted Docker Registry

To create repositories on the public Docker Registry, it is necessary to sign up at:

 

Once you have created an account, you will need to login from the command line

 

If login is successful.

 

Dockerfile: Commit an Image

Once logged in to the public Docker Registry, new images can be built and committed with code using a Dockerfile. This allows an administrator to automatically rebuild a known good starting point quickly and easily. It is recommended to always start with an image defined by a Dockerfile.

There are a couple of important things to notice with this Dockerfile. First, the FROM directive specifies a username and repository: fatherlinux/centos6-base. This will pull the latest image from the centos6-base repostiory. In this example, I have provided the source repository for you. Second, the only change we have specified in the Dockerfile, is to update CentOS to latest available packages.

 

 

Build and tag the image

 

Inspect the new image

This will list all of the layers in an image

 

Notice that the new image is now available for deployment.

Test the new image

 

Manually: Commit an Image

Once a container has changes made locally, they can be committed to the local index. This allows you to check point and continue. It also allows you to create new images based off of this modified container.

 

Modify the image

Make some changes inside the container. In this example, change the hostname and exit

 

Commit the container

First, get a list of containers. Notice that every container has a CONTAINTER ID and a STATUS. A status of Up means the container is currently running, while a status of Exit indicates that the container has been stopped. Think of the CONTAINER ID as a branch from the base image that contains all of the changes that were made to the container while it was running. By default this data is saved even after the container is shut down.

 

Now, commit the container back as a branch of it’s base image

 

Notice that the image is now available in the tree output. Also, notice that it is a branch of the root centos6-base:latest image, not the centos6-base-updated:latest image, which we previously built with a Dockerfile.

 

Tag the new image with something meaningful

 

Push a Container

Once a container is committed locally, it can be pushed back to the registry server to be shared. The changes will be pushed as a layered image. Notice how quickly it is able to push only the differences between your modified image and the base image. This is a big part of the value.

 

Advanced Operations

Pull All Standard Images

These CentOS images are in the public Docker repository.

 

Create Base Image

This method was developed with guidance from this script. This example is based on CentOS 6.

Create a tar file of the system

 

Copy the tar file to where the consuming system and Import the image

 

Test

 

Set Up a Registry Server

Notice that the entire application is packaged up and ran from inside of a docker container. This has the interesting consequence that we are not even concerned with what operating system is hosting this registry application. Also, notice that port 5000 in the docker container is mapped to port 5000 on the hosting virtual machine, which makes the application running in the container transparently appear to be running on the virtual machine.

 

Inspect the Registry Application

Notice the tree structure of the registry application. A branch has been saved for each minor release. Think about the consequenses of releasing software like this; when an application is packaged with docker, the end user can be given access to every version of the application simultaneously.

 

Search Private Registry

List all images in the repository

 

Search for all repositories with the word “rhel” in their name

 

Remove Old Docker Containers

By default, Docker keeps changes for every container which is instantiated. When testing, this can be undesirable. Be careful because this will remove all branches/data. Any containers which have not been committed will have all data deleted:

 

Links

Trackbacks/Pingbacks

  1. A Practical Introduction to Docker | Crunch Tools - […] Related Blog Post […]
  2. A Practical Introduction to the Docker Registry | Crunch Tools - […] I mentioned in my original article, one of the first things a user wants to do after learning about…
  3. Marketing Small OpenSource Projects: Packaging | Crunch Tools - […] article a long time ago, and the world may very well change with the introduction and popularity of Docker,…
  4. Core Builds in the Age of Service | Crunch Tools - […] understood, reproduce-able and automated. These mechanisms are not mutually exclusive. For example, Docker and Project Atomic are providing more…
  5. Running Docker in Production | Crunch Tools - […] A Practical Introduction to Docker Containers […]

Post a Reply

Your email address will not be published.