How to set up a local Docker Registry

How to set up a local Docker Registry

If your Server is not able to reach the Internet and you don`t have a private Docker registry in your company, you can install a local Docker registry on the k3s server of your PoC installation.

This approach is applicable for PoC installations only. For productive installations, set up a private Docker container registry in your infrastructure.

What you need

Besides the VM that hosts your k3s single node cluster, you need to have

  • A Linux PC or Server with internet connection (must be able to reach our docker registry “https://repo.mid.de”) and docker installed on it. You can find instructions in how to install docker in the Docker documentation. In the rest of the instructions this machine is called the “Docker machine”.

  • A way to transfer files from the Docker-Machine to the k3s server

  • sudo rights on the k3s server

Install the local registry

  1. Install Docker on the server. You can find instructions in how to install docker in the Docker documentation. Use the “Install from Package” method, so that you can download the installations packages and copy it to your server.

  2. On the Docker machine, run the following commands

    docker pull registry:2 docker save registry:2 | gzip > registry_2.tgz
  3. Copy the file “registry_2.tgz” to the k3s server

  4. Import the Docker registry image on the k3s server with

    sudo docker load -i registry_2.tgz
  5. Run the Docker registry on the k3s server

    sudo docker run -d -p 5000:5000 --restart always --name registry registry:2
  6. Validate that the container of the Docker registry is started

    sudo docker ps

    This command should bring up a result like this:

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 915ee60cb306 registry:2 "/entrypoint.sh /etc…" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp, [::]:5000->5000/tcp registry

Now the local container registry is set up and can be used.

Load all images used by the chart into the local registry

  1. On the docker machine

    1. Get the list of all images used in your installation with the command. This step should be done after finishing the values.yaml for you installation, because the vaules.yaml defines, which images are enabled or disabled.

      images=($(helm template smartfacts mid-smartfacts/smartfacts -f values.yaml | grep image: | sed -E 's/^[[:space:]]*image: "?([^"]+)"?[[:space:]]*/\1/'))
    2. Load the images into the Docker machine

      for image in "${images[@]}"; do sudo docker pull "$image"; done
    3. Retag all images, so that they use “localhost:5000”, as the registry name and save them to the file “local_images.tgz”

      local_images="" for image in "${images[@]}"; do local_image=$(echo "$image" | sed -E 's|^[^/]+/(.+)|localhost:5000/\1|') local_images+=" $local_image" sudo docker tag "$image" "$local_image" done sudo docker save $local_images | gzip > local_images.tgz echo $local_images | tr ' ' '\n' > local_images.txt sha256sum local_images.tgz > local_images.tgz.sha256
  2. Copy the files local_images.tgz, local_images.tgz.sha256 and local_images.txt to the k3s server

  3. On the k3s server

    1. Validate the checksum of the copied file

      sha256sum -c local_images.tgz.sha256

      The result must state “OK”

    2. Load the images on the docker machine

      sudo docker load -i local_images.tgz
    3. Push the local images to the local registry

      for image in $(cat local_images.txt); do sudo docker push $image; done

Adapt the values.yaml file to the local registry

In the values.yaml file set the following values:

Set the registry name to be used to your local registry:

global.registry: localhost:5000

The local registry has no authentication. Therefore the imagePullSecrets that are set by default have to be emptied.

  • global.imagePullSecrets: []