Introduction
Docker enables developers to create, manage, and deploy containers efficiently. When working in a team or developing in an isolated environment, using a local Docker repository helps store, share, and manage container images without relying on external registries like Docker Hub.
In this article, we’ll explore how to:
β
Set up a local Docker repository using Docker Registry
β
Push Docker images to the local repo
β
Pull images from the local repo
Step 1: Install Docker
Ensure that Docker is installed on your system. You can verify the installation using:
docker --version
If not installed, download it from Docker Official Website and follow the installation steps.
Step 2: Run a Local Docker Registry
Docker provides an official registry image that allows you to create a local repository. To start it, run:
docker run -d -p 5000:5000 --name local-registry registry:2
πΉ This command does the following:
-d
β Runs the container in detached mode-p 5000:5000
β Maps port 5000 of the container to port 5000 of the hostregistry:2
β Uses the official Docker Registry v2 image
To check if the registry is running, use:
docker ps
You should see a running container named local-registry.
Step 3: Tag an Image for the Local Repository
Before pushing an image to the local repository, you need to tag it correctly.
1. List Available Docker Images
Check the images on your system using:
docker images
2. Tag the Image for the Local Repository
Letβs say we have an image named nginx
and want to push it to our local repository:
docker tag nginx:latest localhost:5000/nginx-local
Here, localhost:5000/nginx-local
represents the new image tag that points to our local registry.
Step 4: Push the Image to the Local Repository
Now, push the tagged image to the local repo:
docker push localhost:5000/nginx-local
If the push is successful, it will upload the image to localhost:5000/nginx-local
.
To verify, check the images stored in your local registry:
curl -X GET http://localhost:5000/v2/_catalog
It should return something like:
{"repositories":["nginx-local"]}
Step 5: Pull the Image from the Local Repository
To ensure everything works correctly, remove the local copy of the image:
docker rmi localhost:5000/nginx-local
Now, pull the image from the local repository:
docker pull localhost:5000/nginx-local
Run the container to confirm the image works:
docker run -d -p 8080:80 localhost:5000/nginx-local
Step 6: Use Local Repository Without localhost
(Optional)
By default, Docker does not trust insecure registries (like our localhost:5000
). To allow it, add the following to Dockerβs daemon configuration (/etc/docker/daemon.json
):
{
"insecure-registries": ["localhost:5000"]
}
Restart Docker for changes to take effect:
systemctl restart docker
Conclusion
π― We successfully created a local Docker repository and learned how to:
β
Run a local Docker registry
β
Tag images for the local repository
β
Push images to the local repo
β
Pull images from the local repo
This setup is useful for offline development, testing, and secure environments where external registries are restricted.
π Summary
Action | Command |
---|---|
Start Registry | docker run -d -p 5000:5000 --name local-registry registry:2 |
Pull Image | docker pull nginx |
Tag Image | docker tag nginx localhost:5000/nginx-local |
Push Image | docker push localhost:5000/nginx-local |
Pull Image | docker pull localhost:5000/nginx-local |
Run Container | docker run -d -p 8080:80 localhost:5000/nginx-local |