Hosting Private Docker Registry on Cloud
If you use Docker, you are familiar with the command docker pull
to download an image from DockerHub. This command reaches to DockerHub, which is the official registry storing many images. Instead of using the official DockerHub registry, you can configure a private registry hosted on Digital Ocean to keep and access your images remotely.
I preferred to use a private registry hosted on Digital Ocean because it is reliable and cheap as you can use the credit earned in registration.
Configuring Digital Ocean Droplet
After you have created a Digital Ocean account, you can create a Ubuntu droplet with minimum hardware requirements.
In your terminal, establish a connection to your droplet by typing:
ssh root@<DROPLET-IP>
After logging in, you can run the following commands to configure your droplet:
- Upgrade packages to the latest version:
apt-get update && apt-get upgrade -y
- Install Docker and Docker Compose:
apt install docker.io
apt install docker-compose
- Create a docker-compose file to configure your droplet:
mkdir docker-registry
cd docker-registry
touch docker-compose.yml
- Add the following lines to your docker-compose.yml file:
version: "3" services:
docker-registry:
image: registry:2
container_name: docker-registry
ports:
- 5000:5000
restart: always
volumes:
- ./volume:/var/lib/registry
- Create a new directory to keep images pushed to the registry, and start services:
mkdir volume
docker-compose up -d
At this point, all things should be fine and running in your droplet. Now, it is time to configure the local development environment so that images can be pushed and pulled from private registry.
Configuring Local Development Environment
This section will be run on your local development environment.
To make Docker recognize the private registry we just created, Daemon configuration file should be modified. If daemon.json
file does not exist, it is just going to be created by running the following:
- For Linux:
vim /etc/docker/daemon.json
- For macOS:
vim ~/.docker/daemon.json
Add following lines into Daemon configuration file, and restart Docker services:
{
"insecure-registries": ["<DROPLET-IP>:5000"]
}
Testing
Let’s test push and pull operations on the private registry by using alpine
image since it is light-weight.
- Pull the image:
docker pull alpine
- Tag the image using the registry address:
docker tag alpine:latest <DROPLET-IP>:5000/fo/alpine-fo:v1
- Push the tagged image to private registry:
docker push <DROPLET-IP>:5000/fo/alpine-fo:v1
- Remove original and tagged versions of alpine image:
docker rmi alpine:latest <DROPLET-IP>:5000/fo/alpine-fo:v1
- Pull tagged image again:
docker pull <DROPLET-IP>:5000/fo/alpine-fo:v1
Now, try to interact with the image by running the following command.
docker run -it <DROPLET-IP>:5000/fo/alpine-fo:v1
If everything goes well, you should see a terminal like following: