How to Set Up a Local Docker Registry on Ubuntu 22.04 LTS

Setting up a local Docker registry is an essential practice for DevOps engineers looking to manage and share container images within their team or organization efficiently. By hosting a local registry, you can increase the speed of deployments, reduce external dependencies, and maintain better control over your containerized assets. In this guide, we’ll walk through the process of setting up a local Docker registry on Ubuntu 22.04 LTS.

Prerequisites

Before starting, ensure you have:

  • Administrative access to the server.
  • A basic understanding of Docker, CI/CD, and Linux.

Technical Implementation

Follow these steps to set up your local Docker registry on Ubuntu 22.04 LTS.

Step 1: Install the Required Packages

Begin by updating your package list and installing necessary packages:

# Update and install required packages
sudo apt update && sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

Step 2: Add the Docker Repository

Add the official Docker repository to your system:

# Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Add Docker repository to the system
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Step 3: Install Docker

Install Docker using the apt package manager:

# Install Docker CE (Community Edition)
sudo apt update && sudo apt install docker-ce -y

Step 4: Verify Docker Installation

Check if Docker is installed correctly by verifying the version:

# Verify Docker installation
docker --version

Step 5: Start and Enable the Docker Service

Ensure that Docker is running and set to start on boot:

# Start Docker and enable it at boot
sudo systemctl start docker
sudo systemctl enable docker

Step 6: Set Up a Local Docker Registry Container

Run a Docker container for the registry:

# Run the Docker registry container
sudo docker run -d -p 5000:5000 --name local-registry registry:2

Explanation:

  • -d: Runs the container in detached mode.
  • -p 5000:5000: Exposes port 5000 for the registry.

Step 7: Test the Local Docker Registry

Tag an image and push it to your local registry:

# Pull a test image
docker pull alpine:latest

# Tag the image for the local registry
docker tag alpine:latest localhost:5000/alpine:latest

# Push the image to the local registry
docker push localhost:5000/alpine:latest

To verify the image is in the local registry:

# List images in the local registry
curl http://localhost:5000/v2/_catalog

Step 8: Secure Your Docker Registry (Optional but Recommended)

By default, the local Docker registry is unsecure. To secure it:

  1. Generate Self-Signed Certificates:
   openssl req -newkey rsa:4096 -nodes -keyout domain.key -x509 -days 365 -out domain.crt
  1. Configure Docker to Trust the Self-Signed Certificates:
    Place the domain.crt in /etc/docker/certs.d/localhost:5000/ on the client machine.
  2. Restart Docker:
   sudo systemctl restart docker

Best Practices

  • Secure Access: Use TLS/SSL to encrypt communication between Docker clients and the local registry.
  • Authentication: Implement user authentication for access control to the registry.
  • Monitor Logs: Regularly monitor the registry logs for suspicious activity to ensure the security and performance of your local Docker registry.

Troubleshooting

Common Issues and Solutions

  • Docker Not Starting:
  • Check Docker logs with: sudo journalctl -u docker.service
  • Registry Not Accessible:
  • Verify the registry container is running:
    bash sudo docker ps | grep registry
  • Ensure port 5000 is not blocked by a firewall.

Conclusion

In this guide, we covered the step-by-step process of setting up a local Docker registry on Ubuntu 22.04 LTS. By following these instructions, you can effectively manage and share container images, enhancing the efficiency of your development and deployment processes. Remember to follow best practices for securing and maintaining your registry to ensure optimal performance and reliability.

Next Steps

  • Integrate with CI/CD Pipelines: Use your local registry in your CI/CD pipelines to automate the push and pull of images.
  • Scale Up: Consider using Docker Compose or Kubernetes to scale your registry setup.
  • Explore Advanced Features: Implement features like storage backends and image retention policies for better management.