There are situations where you might want to set up your own private Docker registry even though Docker Hub offers a public registry for storing and sharing Docker images. In this guide, we’ll show you how to set up a private Docker registry on Ubuntu 22.04 so you can manage your container images safely within your company.
Prerequisites:
Before we begin, make sure you have the following:
- a virtual machine or an Ubuntu 22.04 server.
- access to the server as root or sudo.
- basic command-line and Docker knowledge.
Step 1: Update and Upgrade Packages index
Making sure your Ubuntu system is up to date is the first step. Run the following commands after opening a terminal:
sudo apt update
sudo apt upgrade -y
Step 2: Install Docker on Ubuntu
You must have Docker installed on your system in order to create a private Docker registry. Run the commands below to install Docker:
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
Step 3: Generate SSL Certificate
You must create an SSL certificate to protect your private Docker registry. To create a self-signed certificate, execute the commands below:
sudo mkdir /certs
sudo openssl req -newkey rsa:4096 -nodes -sha256 -keyout /certs/domain.key -x509 -days 365 -out /certs/domain.crt
Ensure that “domain” is changed to your own domain name or IP address.
Step 4: Create a Registry Configuration File
The Docker registry needs a configuration file, so we must do that next. Create a config.yml file using a text editor:
mkdir /etc/docker/registry/
sudo nano /etc/docker/registry/config.yml
Copy and paste the configuration shown below into the file:
version: 0.1
log:
level: info
formatter: json
fields:
service: registry
storage:
cache:
layerinfo: inmemory
filesystem:
rootdirectory: /var/lib/registry
http:
addr: :5000
tls:
certificate: /certs/domain.crt
key: /certs/domain.key
Step 5: Start the Docker Registry
Now that we have the configuration file, we can launch the Docker registry. Run the command line:
sudo docker run -d -p 5000:5000 --restart=always --name registry -v /etc/docker/registry:/etc/docker/registry -v /certs:/certs -v /var/lib/registry:/var/lib/registry registry:2
By using this command, a container called “registry” is started with port 5000 open, and the official Docker registry image is downloaded. If the server reboots, the container will also restart automatically.
Step 6: Configure Docker Daemon
You must set the Docker daemon to trust the SSL certificate in order for your Docker client to be able to communicate with the private registry. Create the file if it doesn’t already exist. Open the configuration file for the Docker daemon and add the following lines:
sudo nano /etc/docker/daemon.json
{
"insecure-registries": [],
"registry-mirrors": [],
"insecure-registries": ["your-domain-or-ip:5000"]
}
Replace “your-domain-or-ip” with the correct domain name or server’s IP address.
Step 7: Restart Docker Daemon
Restarting the Docker service after making changes to the Docker daemon configuration will make the changes effective:
sudo systemctl restart docker
Step 8: Push and Pull Images
- Pull the Nginx image from Docker Hub:
sudo docker pull nginx
- Tag the pulled Nginx image with your private registry address:
sudo docker tag nginx:latest server-ip:5000/image-name:tag
Replace “image-name” with the desired name for your image, and “tag” with the version or tag you want to assign to the image.
- Push the tagged image to your private docker registry:
sudo docker push server-ip:5000/image-name:tag
Make sure to replace “image-name” and “tag” with the same values used in the previous step.
These commands will allow Docker to push the Nginx image to your personal registry at server-ip:5000. The docker pull command can then be used on other machines with access to your registry to pull this image from your personal registry.
Final Thoughts
In this tutorial, you have learned how to set up a private Docker registry on Ubuntu 22.04. By following these steps, you can create a secure environment for storing and managing your Docker images within your organization. Having a private docker registry gives you more control over your container images and allows for easier collaboration and deployment.
0 Comments