Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

Setting Up a Private Docker Registry

  • Print
  • + Share This
This chapter from Docker Containers: Build and Deploy with Kubernetes, Flannel, Cockpit, and Atomic explains how to create a private Docker registry in Fedora or Ubuntu, use the docker-registry package, use the registry container image, and understand the Docker image namespace.
This chapter is from the book

One of the foundations of Docker is the ability to request to use an existing container image and then, if it is not already on your system, grab it from somewhere and download it to your system. By default, “somewhere” is the Docker Hub Registry (https://hub.docker.com). However, there are ways to configure other locations from which you can pull docker images. These locations are referred to as registries.

By setting up your own private registry, you can keep your private images to yourself. You can also save time by pushing and pulling your images locally, instead of having them go out over the Internet.

Setting up a private registry is simple. It requires getting the service (by installing a package or using the registry Docker container image), starting the service, and making sure the proper port is open so the service is accessible. Using registries requires a bit more explanation than setting up one, especially when you consider that features are added to Docker every day that are changing how Docker uses and searches registries for images.

In particular, the way that Docker uses the image namespace is changing to be more adaptable. If your location is disconnected from the Internet, with the Docker hub inaccessible, features are being developed to allow you to use a different default registry. Likewise, new features let you add registries to your search order, much the same way you can have an Internet browser look at different DNS servers.

This chapter describes how to set up a private Docker registry on several different Linux systems. The first examples are simply to help you get a Docker registry up and running quickly to begin testing or learning how to use registries. After that, I describe some techniques for making a Docker registry more production ready.

Later in the chapter, I tell you how to adapt the way your local Docker service uses Docker registries, including how to replace Docker.io as the default registry and add other registries to the search path.

Getting and Starting a Private Docker Registry

You can run a Docker registry on your Linux system in a number of different ways to store your own Docker images. For Linux distributions that include a docker-registry package (such as Fedora and Red Hat Enterprise Linux), you can install that package and start up the service. For other distributions, you can run the official registry container image from Docker.io to provide the service.

See the section later in the chapter that corresponds to the Linux system you are using for instructions on installing and running a Docker registry on that system. For Fedora, I illustrate how to use the docker-registry package, while for Ubuntu I show how to use the registry container.

Here are a few general things you should know about setting up a Docker registry:

  • Install anywhere: Like most servers, the Docker registry does not need to be installed on client systems (that is, where you run your docker commands). You can install it on any Linux system that your clients can reach over a network. That way, multiple Docker clients can access your Docker registry.
  • Open port: If your Docker registry is not on the client, you must be sure that TCP port 5000 is not being blocked by the firewall where the Docker registry is running.
  • Provide space: If you push a lot of images to your registry, space can fill up quickly. For the docker-registry package, stored images are contained in the /var/lib/docker-registry directory. Make sure you configure enough space in that directory to meet your needs, or you can configure a different directory, if you want.

Setting Up a Docker Registry in Fedora

Follow these instructions to install and start up a Docker registry on a Fedora system. At the moment, this procedure creates a version 1 Docker registry from the docker-registry RPM package. Although this procedure was tested on Fedora, the same basic procedures should work for the following Linux distributions:

  • Fedora 22 or later
  • Red Hat Enterprise Linux 7.1 or later
  • CentOS 7.1 or later

The docker-registry package is not included in the Atomic project Fedora, RHEL, and CentOS distributions. So you must use the registry container, described later for setting up a Docker registry in Ubuntu, to get that feature on an Atomic Linux system.

  1. Install docker-registry: When you install the docker-registry package in Fedora, it pulls in more than a dozen dependent packages as well. To install those packages, type the following:

    # yum install docker-registry
    ...
    Transaction Summary
    ============================================
    Install  1 Package (+15 Dependent packages)
    Total download size: 6.8 M
    Installed size: 39 M
    Is this ok [y/d/N]: y
  2. List docker-registry contents: Use the rpm command to list the contents of the docker-registry file in Fedora. There are nearly 200 files (mostly python code in the package). This command shows you only documentation and configuration files (I describe how to configure them later):

    # rpm -ql docker-registry | grep -E "(/etc)|(/usr/share)|(systemd)"
    /etc/docker-registry.yml
    /etc/sysconfig/docker-registry
    /usr/lib/systemd/system/docker-registry.service
    /usr/share/doc/docker-registry
    /usr/share/doc/docker-registry/AUTHORS
    /usr/share/doc/docker-registry/CHANGELOG.md
    /usr/share/doc/docker-registry/LICENSE
    /usr/share/doc/docker-registry/README.md
  3. Open firewall: If your Fedora system is running a firewall that blocks incoming connections, you may need to open TCP port 5000 to allow access to the Docker registry service. Assuming you are using the firewall service in Fedora, run these commands to open the port on the firewall (immediately and permanently) and see that the port has been opened:

    # firewall-cmd --zone=public --add-port=5000/tcp
    # firewall-cmd --zone=public --add-port=5000/tcp --permanent
    # firewall-cmd --zone=public --list-ports
    5000/tcp
  4. Start the docker-registry service: If you want to do any special configuration for your Docker registry, refer to the next sections before starting the service. For a simple docker-registry installation, however, you can simply start the service and begin using it, as follows (as the status shows, the docker-registry service is active and enabled):

    # systemctl start docker-registry
    # systemctl enable docker-registry
    Created symlink from
      /etc/systemd/system/multi-user.target.wants/docker-registry.
    service
      to /usr/lib/systemd/system/docker-registry.service.
    # systemctl status docker-registry
    docker-registry.service - Registry server for Docker
     Loaded: loaded (/usr/lib/systemd/system/docker-registry.
    service;enabled)
     Active: active (running) since Mon 2015-05-25 12:02:14 EDT; 42s ago
    Main PID: 5728 (gunicorn)
       CGroup: /system.slice/docker-registry.service
           boxvr.jpg5728 /usr/bin/python /usr/bin/gunicorn --access-logfile
                - --max-requests 100 --graceful-timeout 3600-t 36...
    ...
  5. Get an image: A common image used to test Docker is the hello-world image available from the Docker Hub Registry. Run that image as follows (which pulls that image to the local system and runs it):

    # docker run --name myhello hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from docker.io/hello-world
    91c95931e552: Download complete
    a8219747be10: Download complete
    Hello from Docker.
    docker.io/hello-world:latest: The image you are pulling has been
    verified.
    ...
  6. Allow access to registry: The docker clients in Fedora and Red Hat Enterprise Linux require that you either obtain a certificate from the registry or you identify the registry as insecure. For this example, you can identify the registry as insecure by editing the /etc/sysconfig/docker file and creating the following lines in that file:

    ADD_REGISTRY='--add-registry localhost:5000'
    INSECURE_REGISTRY='--insecure-registry localhost:5000'

    After that, restart the local Docker service:

    # systemctl restart docker
  7. Tag the image: Use docker tag to give the image a name that you can use to push it to the Docker registry on the local system:

    # docker tag hello-world localhost:5000/hello-me:latest
  8. Push the image: To push the hello-world to the local Docker registry, type the following:

    # docker push localhost:5000/hello-me:latest
    The push refers to a repository [localhost:5000/hello-me] (1 tags)
    ...
    Pushing tag for rev [91c95931e552] on
         {http://localhost:5000/v1/repositories/hello-me/tags/latest}
  9. Pull the image: To make sure you can retrieve the image from the registry, in the second Terminal, remove the image from your system, then try to retrieve it from your local registry:

    # docker rm myhello
    # docker rmi hello-world localhost:5000/hello-me:latest
    # docker pull localhost:5000/hello-me:latest
    Pulling repository localhost:5000/hello-me
    91c95931e552: Download complete
    a8219747be10: Download complete
    # docker images
    REPOSITORY              TAG    IMAGE ID     CREATED     VIRTUAL SIZE
    localhost:5000/hello-me latest 91c95931e552 5 weeks ago 910 B

In the example just shown, the image was successfully pushed to and pulled from the local repository. At this point, you have these choices:

  • If you want to learn more about how the Docker registry works and possibly modify its behavior, skip to the “Configuring a Private Docker Registry” section later in this chapter.
  • If you are ready to start using Docker containers, skip ahead to Chapter 4, “Running Container Images.”

The next section describes how to set up a Docker registry in Ubuntu.

Setting Up a Docker Registry in Ubuntu

Instead of installing a Docker registry from a software package, you can download the registry container from the Docker Hub Registry and use that to provide the Docker registry service. This is a quick and easy way to try out a Docker registry, although the default registry doesn’t scale well for a production environment and is more difficult to configure.

Although this procedure was tested on Ubuntu 14.04, the same basic procedure should work on any Linux system running the Docker service.

To get started here, install Docker as described in Chapter 2, “Setting Up a Container Run-Time Environment,” and start up the Docker service. I suggest you open two Terminal windows (shells) to do this procedure. Open one where you plan to run the registry service, so you can watch it in progress as you start up and test it. Open another Terminal, from which you can push and pull images.

  1. Get the registry image: Run the docker pull command as follows to pull the registry image from the Docker Hub Registry (see Chapter 5, “Finding, Pulling, Saving, and Loading Container Images,” for a description of docker pull):

    $ sudo docker pull registry:latest
    Pulling repository registry
    204704ce3137: Download complete
    e9e06b06e14c: Download complete
    ...
  2. Run the registry image: To try out the Docker registry, run the image in the foreground so you can watch messages produced as the container image is running (see Chapter 4 for a description of docker run). This command starts the latest registry image, exposes TCP port 5000 on the system so clients outside the container can use it, and runs it as a foreground process in the first terminal:

    $ sudo docker run -p 5000:5000 registry:latest
    [2015-05-25 21:33:35 +0000][1][INFO] Starting gunicorn 19.1.1
    [2015-05-25 21:33:35 +0000][1][INFO] Listening at:
    http://0.0.0.0:5000 (1)
    [2015-05-25 21:33:35 +0000][1][INFO] Using worker: gevent
    ...
  3. Get an image: To test that you can push and pull images, open a second Terminal window. A common image used to test Docker is the hello-world image available from the Docker Hub Registry. Run that image as follows (which pulls that image to the local system and runs it):

    $ sudo docker run --name myhello hello-world
    Pulling repository hello-world
    91c95931e552: Download complete
    a8219747be10: Download complete
    Hello from Docker.
    This message shows that your installation appears to be working
    correctly.
    ...
  4. Tag the image: Use docker tag to give the image a name that you can use to push it to the Docker registry on the local system:

    $ sudo docker tag hello-world localhost:5000/hello-me:latest
  5. Push the image: To push the hello-world to the local Docker registry, type the following:

    $ sudo docker push localhost:5000/hello-me:latest
    The push refers to a repository [localhost:5000/hello-me] (len: 1)
    ...
    Pushing tag for rev [91c95931e552] on
         {http://localhost:5000/v1/repositories/hello-me/tags/latest}
  6. Check the Docker registry log messages: If the image was pushed to the registry successfully, in the first Terminal you should see messages showing PUT commands succeeding. For example:

    172.17.42.1 - - [25/May/2015:22:12:37 +0000] "PUT
    /v1/repositories/hello-me/images HTTP/1.1" 204 - "-" "docker/1.0.1
    go/go1.2.1 git-commit/990021a kernel/3.13.0-24-generic os/linux
    arch/amd64"
  7. Pull the image: To make sure you can retrieve the image from the registry, in the second Terminal remove the image from your system, and then try to retrieve it from your local registry:

    $ sudo docker rm myhello
    $ sudo docker rmi hello-world localhost:5000/hello-me:latest
    $ sudo docker pull localhost:5000/hello-me:latest
    Pulling repository localhost:5000/hello-me
    91c95931e552: Download complete
    a8219747be10: Download complete
    # docker images
    REPOSITORY              TAG    IMAGE ID     CREATED     VIRTUAL SIZE
    localhost:5000/hello-me latest 91c95931e552 5 weeks ago 910 B
  8. Run the docker registry again: Instead of running the registry image in the foreground, holding the Terminal open, you can have it run more permanently in the background (-d). To do that, close the running registry container and start a new image as follows:

    $ sudo docker run -d -p 5000:5000 registry:latest

The Docker registry is running in the background now, ready to use. At this point, you have these choices:

  • If you want to learn more about how the Docker registry works and possibly modify its behavior, skip to the “Configuring a Private Docker Registry” section later in this chapter.
  • If you are ready to start using Docker containers, skip ahead to Chapter 4.

The next section describes how to set up a Docker registry in other Linux distributions.

  • + Share This
  • 🔖 Save To Your Account