How to Create a Local Repository in Docker and Push & Pull Images

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 host
  • registry: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

ActionCommand
Start Registrydocker run -d -p 5000:5000 --name local-registry registry:2
Pull Imagedocker pull nginx
Tag Imagedocker tag nginx localhost:5000/nginx-local
Push Imagedocker push localhost:5000/nginx-local
Pull Imagedocker pull localhost:5000/nginx-local
Run Containerdocker run -d -p 8080:80 localhost:5000/nginx-local

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *