S O F T W A R E S P I C E

Loading

How to Push a Docker Image to a Private Repository

If you have already logged into the Docker Hub on your virtual machine with docker login command, the push process is the same. In the previous tutorial, we learned to tag a Docker image and pushing it to a public repository on Docker Hub. Luckily, you will not encounter any additional changes here in this article. We only need to change the private repository name while pushing the image. Fortunately, hub.docker.com allows you to create only one private repository.  I hope you created one for testing purposes and if not, you can go through this article to know how to create one. Let’s do the drill together and learn how to push a Docker image to a private repository.

Are you ready?

Let’s get started!

1. What is a Private Repository?

A repository is where you store, push and pull images. A repository can be public or private. As I mentioned above, you can create only ONE private repository on Docker Hub. The images you push in a private repository will remain private to your own account. Also, these images won’t appear in the search results. 

I have already created a private repository on my Docker Hub account. You can see I am using the 1 of 1 private repository available. 

docker hub private repo

2. Set the Proper Tags on the Images

Since, we are pushing images to a private repository, the name of the image must align with the repository name. We’ve covered image tagging in detail and how it works. I have the following private repository name on Docker Hub.

softwarespice/prodimages

So, the image name should be the same. Otherwise, we will get the following error.

An image does not exist locally with the tag: softwarespice/prodimages

It means there is no image with this tag and hence, we can not push it. 

I’ll quickly tag an NGINX image with the docker tag command.

docker tag nginx:latest softwarespice/prodimages:prodimage1

You should see the following output.

[email protected]:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
softwarespice/prodimages prodimage1 bd29370f84ea 6 days ago 138MB

We are ready to push the image.

3. Pushing the Image to the Private Repository

Before pushing the image to the private repository, make sure you are logged in. Use the docker login command and give the username and password of your Docker Hub account. This is a quick way to login if you have not stored your credentials on the virtual machine. 

login to docker hub via ssh

Now run the following command to push the image.

docker push softwarespice/prodimages:prodimage1

Boom!

We have pushed the image and we can confirm on our Docker Hub account.

push docker image to private repository

4. Creating Your Own Local Repository

In order to create a local registry on our virtual machine, execute the following command:

docker run -d -p 5000:5000 --restart=always --name registry registry:2

This command will run a container with the name “registry” on port 5000 from the image registry:2.

We can use this local registry to push and pull images locally on our virtual machine.

Create a local registry on the vm

Set a tag for this image as “localhost:5000“.

Why are we tagging like that?

It’s because the registry is created locally and it runs on port 5000. 

I have tagged it as localhost:5000/database because it’s a database image.

[email protected]:~$ docker push localhost:5000/database
Using default tag: latest
The push refers to repository [localhost:5000/database]
750644870eed: Pushed
354cd4440aa7: Pushed
96789aa73d8f: Pushed
956b4f80a078: Pushed
df8b8ee18301: Pushed
7e50f83cc60c: Pushed
b98c96f5ae6b: Pushed
b699e149afa6: Pushed
0552d4cbc6e9: Pushed
a70daca533d0: Pushed
latest: digest: sha256:84075fa0ee8106f8e2975dca79d3c6f9587b41afefa7aec57e76a2fc9506df6c size: 2412

We have pushed the image to our local registry. The most beautiful part is that if we remove the image locally from the machine, we’ll still be able to pull it again from the local repository.

That’s how an image is removed with the docker rmi command which stands for remove image.

[email protected]:~$ docker rmi localhost:5000/database:latest 
Untagged: localhost:5000/database:latest
Untagged: localhost:5000/[email protected]:84075fa0ee8106f8e2975dca79d36a2fc9506df6c

We have removed the image from the machine locally.

Now let’s pull the same image from the local repository we created to see if it works.

[email protected]:~$ docker pull localhost:5000/database
Using default tag: latest
latest: Pulling from database
Digest: sha256:84075fa0ee8106f8e2975dca79d3c6f9587b41afefa7aec57e76a2fc9506df6c
Status: Downloaded newer image for localhost:5000/database:latest
localhost:5000/database:latest

Boom!

We have pulled the image from our local repository with the docker pull command.

I have confirmed it on my Ubuntu machine running the docker images command.

[email protected]:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:5000/database latest f55fb7cb2f36 2 days ago 409MB

So, having a local repository is a good idea to keep images locally. I would strongly recommend you guys to create one on your system and try to pull and push images locally.

5. A Brief Summary

So far we have built a strong grip over pulling and pushing the Docker images. It shouldn’t be a problem for us to run these commands. The method is the same whether you are pushing an image to a public or private repository. However, we must know the difference between these repositories and their purpose. 

Finally, I’ll advise you to go through each command and change the image names accordingly. Practice these commands so that you know how to push a Docker image to a private repository. Also, create a local repository as well to see how pulling and pushing works locally.

If you want to know more about Docker in the meanwhile, go through the following simple guides:

Let me know if you have any questions in the comments section.

Happy Docking!!!

Leave a Comment

Recent Posts

how to stop a docker container
How to Run a Docker Container
How to Push a Docker Image to a Private Repository
How to push a docker image
How to Tag a Docker Image
Software Spice
Share via
Copy link
Powered by Social Snap