Post by Michael Smolyak

Running Pre-build Docker Images

Running Docker containers requires Docker runtime to be installed on the target machine. Docker runs natively on Linux platform, but can be installed on Mac OS andWindows using VirtualBox and Boot2Docker tools.

Docker consists of Docker daemon and Docker client which may be installed on the same or different machines. In the latter case, Docker client connects to the daemon remotely. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers.

The simplest way to use Docker is to connect to a Docker registry (e.g. Docker Hub), download an existing Docker image (e.g., MongoDB) and run it using docker runcommand.

The first time you try to run an image, Docker will download it from the Docker registry. Subsequent startups will use the image cached locally. When instructed to run an image, Docker creates a container from that image and runs it. Docker images are read-only. Docker container is a runtime instance of a corresponding image (which adds an execution environment to the image).

A container can be started in foreground (interactive mode) or in background. The former type of startup will typically give you a Linux command line prompt to interact with the running container. You can connect to a container running in background mode (a more common scenario) using docker exec command. The docker run command has a large number of options for customizing the way the container interacts with its host environment.

Docker also provides commands for listing running containers (ps), listing installed images (images), inspecting running containers (inspect) and stopping a running container (stop or kill).

This diagram describes Docker’s major moving parts.

Creating Your Own Images

To take full advantage of power and flexibility of Docker, you have to build custom images. Here are a few basic rules for Docker image creation:

  • A new image is typically created from an existing Docker image (e.g., CentOS or Ubuntu image) (you can create an image from scratch, but there is rarely a need for that).
  • Docker uses scripts consisting of instructions from a small set of commands to build an image.
  • The script for image creation is placed in a file called Dockerfile.
  • The commands inside Dockerfile allow you to
    • Define the source image (e.g. some flavor of Linux) (FROM)
    • Run Linux command on the image (e.g., yum install) (RUN)
    • Copy files from the host machine to the image (ADD or COPY)
    • Specify a startup command for the image (e.g., start the server) (CMD)
    • Mapping a directory inside the running container to a directory on the host (VOLUME)
    • Expose network ports from the running container (EXPOSE)
    • Label image with meta-data (LABEL)
  • Use docker build command to create an image based on the Dockerfile
  • The Docker image can be added to a Docker repository with a docker push command and downloaded from the repository with docker pull command.

I went through the steps necessary to configure Docker runtime, connect to the PartShop Docker registry, build Docker images for the Squawker project and integrate Docker with Jenkins (to automate image creation).