How to push a docker image to a private repository
Asked Answered
N

14

682

I have a docker image tagged as me/my-image, and I have a private repo on the dockerhub named me-private.
When I push my me/my-image, I end up always hitting the public repo.

What is the exact syntax to specifically push my image to my private repo?

Nutbrown answered 5/2, 2015 at 16:42 Comment(4)
docs.docker.com/engine/getstarted/step_sixLegra
The word "private" does not even occur on the web page you linked.Aliform
Check this one out Docker publish to a private repositoryTiruchirapalli
simple quickstart: docs.docker.com/docker-hub Shows how to docker build and docker push to a dockerhub private repo.Android
L
974

You need to tag your image correctly first with your registryhost:

docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

Then docker push using that same tag.

docker push NAME[:TAG]

Example:

docker tag 518a41981a6a myRegistry.com/myImage
docker push myRegistry.com/myImage
Lillia answered 5/2, 2015 at 16:49 Comment(8)
so, with this image: 518a41981a6a, what is the actual tag syntax to make it go to the me-private repo, please?Nutbrown
docker tag 518a41981a6a me-private.com/myPrivateImage && docker push me-private.com/myPrivateImageLillia
I fixed some syntax issues in my answer above as well. Also, fyi when you push to a registry you have to use an actual image name and not the image id.Lillia
for some reason the image still goes to the public dockerhub registry instead of the private one. Just for the clarification - my private registry is also on the dockerhub. I tagged the image, and did the push, but the feedback was indicating that all layers have already been pushed, which would not be the case if the image was going to the private registry for the first timeNutbrown
Oh, if you're using a private dockerhub registry it should be pretty simple. Make sure you do a docker login first, then tag your image: docker tag 518a41981a6a me-private/myPrivateImage and push: docker push me-private/myPrivateImageLillia
The namespace is unique so if you push a private tag there's no way it can go to a different public tag.Lillia
If i updated the image and used the same tag, would it overwrite it?Molloy
As noted below, you need to create the repository on docker hub first.Arabinose
D
348

Just three simple steps:

  1. docker login --username username

    • prompts for password if you omit --password which is recommended as it doesn't store it in your command history
  2. docker tag my-image username/my-repo

  3. docker push username/my-repo

Distefano answered 22/5, 2017 at 9:16 Comment(7)
leave off the --password flag if you don't want your password to show up in history. it will prompt you.Pyxis
not working as no privateregistry hostname mentioned.Affiliation
@BorisIvanov , what do you mean?Arnulfo
@cowlinator, This answer seems to be using Docker Hub rather than a private repo as the question asks.Receivership
I'm asking what "no privateregistry hostname mentioned" refers toArnulfo
This doesn't answer the question, of how to push to a private repository.Digger
Funnily if I omit the --password flag and let it prompt for password, the credentials are "incorrect", if I type it with the flag they are "correct" and it works...Demers
O
134

If you docker registry is private and self hosted you should do the following :

docker login <REGISTRY_HOST>:<REGISTRY_PORT>
docker tag <IMAGE_ID> <REGISTRY_HOST>:<REGISTRY_PORT>/<APPNAME>:<APPVERSION>
docker push <REGISTRY_HOST>:<REGISTRY_PORT>/<APPNAME>:<APPVERSION>

Example :

docker login repo.company.com:3456
docker tag 19fcc4aa71ba repo.company.com:3456/myapp:0.1
docker push repo.company.com:3456/myapp:0.1
Overexert answered 25/7, 2017 at 20:32 Comment(3)
This is the best answer for me. However, I'm curious as to why self-hosted is important here?Seyler
There are two different kinds of "private registry", private registries hosted by dockerhub where you just go to dockerhub and create a private registry, and self-hosted private registries where you actually install and run the registry software yourself. The "self-hosted" is important because all of the comments on other answers saying things like "this doesn't answer the question about private registries" are using one of those definitions, and commenting on an answer that used the other definition.Fleta
Does anybody know why redundancy of "tagging" an image with a registry? At the end of the day, isn't an image just a file? Why does it need to be any more complicated than pulling a file, then pushing it where you want it to go. The image in question obviously did not originate from the destination you are pushing it to.. so the tag is in itself a false association with that image. Notice how in the example above, the push is directly repeating the exact coordinates in the tag. For what? At a minimum, you'd think a better example would be tag <image> <destination>, then simply push <image>Lafond
C
54

First go to your Docker Hub account and make the repo. Here is a screenshot of my Docker Hub account: enter image description here

From the pic, you can see my repo is “chuangg”

Now go into the repo and make it private by clicking on your image’s name. So for me, I clicked on “chuangg/gene_commited_image”, then I went to Settings -> Make Private. Then I followed the on screen instructions enter image description here

HOW TO UPLOAD YOUR DOCKER IMAGE ONTO DOCKER HUB

Method #1= Pushing your image through the command line (cli)

1) docker commit <container ID> <repo name>/<Name you want to give the image>

Yes, I think it has to be the container ID. It probably cannot be the image ID.

For example= docker commit 99e078826312 chuangg/gene_commited_image

2) docker run -it chaung/gene_commited_image

3) docker login --username=<user username> --password=<user password>

For example= docker login --username=chuangg [email protected]

Yes, you have to login first. Logout using “docker logout”

4) docker push chuangg/gene_commited_image

Method #2= Pushing your image using pom.xml and command line.

Note, I used a Maven Profile called “build-docker”. If you don’t want to use a profile, just remove the <profiles>, <profile>, and <id>build-docker</id> elements.

Inside the parent pom.xml:

<profiles>
        <profile>
            <id>build-docker</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <version>0.18.1</version>
                        <configuration>
                            <images>
                                <image>
                                    <name>chuangg/gene_project</name>
                                    <alias>${docker.container.name}</alias>
                                    <!-- Configure build settings -->
                                    <build>
                                        <dockerFileDir>${project.basedir}\src\docker\vending_machine_emulator</dockerFileDir>
                                        <assembly>
                                            <inline>
                                                <fileSets>
                                                    <fileSet>
                                                        <directory>${project.basedir}\target</directory>
                                                        <outputDirectory>.</outputDirectory>
                                                        <includes>
                                                            <include>*.jar</include>
                                                        </includes>
                                                    </fileSet>
                                                </fileSets>
                                            </inline>
                                        </assembly>
                                    </build>
                                </image>
                            </images>
                        </configuration>
                        <executions>
                            <execution>
                                <id>docker:build</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

Docker Terminal Command to deploy the Docker Image (from the directory where your pom.xml is located)= mvn clean deploy -Pbuild-docker docker:push

Note, the difference between Method #2 and #3 is that Method #3 has an extra <execution> for the deployment.

Method #3= Using Maven to automatically deploy to Docker Hub

Add this stuff to your parent pom.xml:

    <distributionManagement>
        <repository>
            <id>gene</id>
            <name>chuangg</name>
            <uniqueVersion>false</uniqueVersion>
            <layout>legacy</layout>
            <url>https://index.docker.io/v1/</url>
        </repository>
    </distributionManagement>


    <profiles>
        <profile>
            <id>build-docker</id>
            <build>
                <plugins>

                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <version>0.18.1</version>
                        <configuration>
                            <images>
                                <image>
                                    <name>chuangg/gene_project1</name>
                                    <alias>${docker.container.name}</alias>
                                    <!-- Configure build settings -->
                                    <build>
                                        <dockerFileDir>${project.basedir}\src\docker\vending_machine_emulator</dockerFileDir>
                                        <assembly>
                                            <inline>
                                                <fileSets>
                                                    <fileSet>
                                                        <directory>${project.basedir}\target</directory>
                                                        <outputDirectory>.</outputDirectory>
                                                        <includes>
                                                            <include>*.jar</include>
                                                        </includes>
                                                    </fileSet>
                                                </fileSets>
                                            </inline>
                                        </assembly>
                                    </build>
                                </image>
                            </images>
                        </configuration>
                        <executions>
                            <execution>
                                <id>docker:build</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>docker:push</id>
                                <phase>install</phase>
                                <goals>
                                    <goal>push</goal>
                                </goals>
                            </execution>

                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Go to C:\Users\Gene.docker\ directory and add this to your config.json file: enter image description here

Now in your Docker Quickstart Terminal type= mvn clean install -Pbuild-docker

For those of you not using Maven Profiles, just type mvn clean install

Here is the screenshot of the success message: enter image description here

Here is my full pom.xml and a screenshot of my directory structure:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gene.app</groupId>
<artifactId>VendingMachineDockerMavenPlugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Maven Quick Start Archetype</name>
<url>www.gene.com</url>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.gene.sample.Customer_View</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>

                    <source>1.7</source>
                    <target>1.7</target>

                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<distributionManagement>
    <repository>
        <id>gene</id>
        <name>chuangg</name>
        <uniqueVersion>false</uniqueVersion>
        <layout>legacy</layout>
        <url>https://index.docker.io/v1/</url>
    </repository>
</distributionManagement>


<profiles>
    <profile>
        <id>build-docker</id>
        <properties>
            <java.docker.version>1.8.0</java.docker.version>
        </properties>
        <build>
            <plugins>

                <plugin>
                    <groupId>io.fabric8</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>0.18.1</version>
                    <configuration>
                        <images>
                            <image>
                                <name>chuangg/gene_project1</name>
                                <alias>${docker.container.name}</alias>
                                <!-- Configure build settings -->
                                <build>
                                    <dockerFileDir>${project.basedir}\src\docker\vending_machine_emulator</dockerFileDir>
                                    <assembly>
                                        <inline>
                                            <fileSets>
                                                <fileSet>
                                                    <directory>${project.basedir}\target</directory>
                                                    <outputDirectory>.</outputDirectory>
                                                    <includes>
                                                        <include>*.jar</include>
                                                    </includes>
                                                </fileSet>
                                            </fileSets>
                                        </inline>
                                    </assembly>
                                </build>
                            </image>
                        </images>
                    </configuration>
                    <executions>
                        <execution>
                            <id>docker:build</id>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>docker:push</id>
                            <phase>install</phase>
                            <goals>
                                <goal>push</goal>
                            </goals>
                        </execution>

                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Here is my Eclipse Directory: enter image description here

Here is my Dockerfile:

FROM java:8

MAINTAINER Gene Chuang
RUN echo Running Dockerfile in src/docker/vending_machine_emulator/Dockerfile directory

ADD maven/VendingMachineDockerMavenPlugin-1.0-SNAPSHOT.jar /bullshitDirectory/gene-app-1.0-SNAPSHOT.jar 

CMD ["java", "-classpath", "/bullshitDirectory/gene-app-1.0-SNAPSHOT.jar", "com/gene/sample/Customer_View" ] 

Common Error #1: enter image description here

Solution for Error #1= Do not sync the <execution> with maven deploy phase because then maven tries to deploy the image 2x and puts a timestamp on the jar. That’s why I used <phase>install</phase>.

Circumpolar answered 1/12, 2016 at 19:7 Comment(0)
F
27

There are two options:

  1. Go into the hub, and create the repository first, and mark it as private. Then when you push to that repo, it will be private. This is the most common approach.

  2. log into your docker hub account, and go to your global settings. There is a setting that allows you to set what your default visability is for the repositories that you push. By default it is set to public, but if you change it to private, all of your repositories that you push will be marked as private by default. It is important to note that you will need to have enough private repos available on your account, or else the repo will be locked until you upgrade your plan.

Filberto answered 29/5, 2015 at 15:6 Comment(3)
Granted, the question posted is not as straightforward as one could wish for, but I have no doubt the key issue for the questioner is the fact that repositories on Docker Hub are public by default. Yet, everybody on this thread is mostly occupied pumping out wikis for private registries as well as touching on the docker push command. But if I have understood the question right, none of these answers are correct and the one posted above by Ken Cochrane is the only one that should be accepted.Aliform
Exactly what I am looking for. As @MartinAndersson mentioned, answers above in command line will still let your pushed image be in public, if you're pushing on DockerHub.Janettjanetta
I agree @MartinAndersson, this should be the correct answer to the OP question.Sacchariferous
D
20

Ref: dock.docker.com

This topic provides basic information about deploying and configuring a registry

Run a local registry

Before you can deploy a registry, you need to install Docker on the host.

Use a command like the following to start the registry container:

start_registry.sh

#!/bin/bash

docker run -d \
  -p 5000:5000 \
  --restart=always \
  --name registry \
  -v /data/registry:/var/lib/registry \
  registry:2

Copy an image from Docker Hub to your registry

  1. Pull the ubuntu:16.04 image from Docker Hub.

    $ docker pull ubuntu:16.04
    
  2. Tag the image as localhost:5000/my-ubuntu. This creates an additional tag for the existing image. When the first part of the tag is a hostname and port, Docker interprets this as the location of a registry, when pushing.

    $ docker tag ubuntu:16.04 localhost:5000/my-ubuntu
    
  3. Push the image to the local registry running at localhost:5000:

    $ docker push localhost:5000/my-ubuntu
    
  4. Remove the locally-cached images. This does not remove the localhost:5000/my-ubuntu image from your registry.

    $ docker image remove ubuntu:16.04
    $ docker image remove localhost:5000/my-ubuntu
    
  5. Pull the localhost:5000/my-ubuntu image from your local registry.

    $ docker pull localhost:5000/my-ubuntu
    
Deploy a plain HTTP registry

According to docs.docker.com, this is very insecure and is not recommended.

  1. Edit the daemon.json file, whose default location is /etc/docker/daemon.json on Linux or C:\ProgramData\docker\config\daemon.json on Windows Server. If you use Docker for Mac or Docker for Windows, click Docker icon -> Preferences -> Daemon, add in the insecure registry.

    If the daemon.json file does not exist, create it. Assuming there are no other settings in the file, it should have the following contents:

    {
      "insecure-registries" : ["myregistrydomain.com:5000"]
    }
    

    With insecure registries enabled, Docker goes through the following steps:

    • First, try using HTTPS.
      • If HTTPS is available but the certificate is invalid, ignore the error about the certificate.
      • If HTTPS is not available, fall back to HTTP.
  2. Restart Docker for the changes to take effect.

Dissect answered 21/5, 2018 at 2:12 Comment(0)
A
14

Create repository on dockerhub :

$docker tag IMAGE_ID UsernameOnDockerhub/repoNameOnDockerhub:latest

$docker push UsernameOnDockerhub/repoNameOnDockerhub:latest

Note : here "repoNameOnDockerhub" : repository with the name you are mentioning has to be present on dockerhub

"latest" : is just tag

Alcina answered 7/12, 2018 at 10:7 Comment(1)
repoNameOnDockerhub is an illegal image name as the rules say it must consist of groups of lowercase characters and digits separated by a separator which can be a dash/hyphen, an underscore or a double underscore. One place in the docs says a dot/period can be a separator and another doesn't mention it. I suspect the username must be lowercase as well but I can only find informal rules on dockerhub saying they require username to have lower case, digits, dash and underscore and be 2-255 in length.Midland
A
12

First login your private repository.

> docker login [OPTIONS] [SERVER]

[OPTIONS]: 
-u username 
-p password

eg:

> docker login localhost:8080

And then tag your image for your private repository

> docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

eg:

> docker tag myApp:v1 localhost:8080/myname/myApp:v1

Finally push your taged images to your private repository

>docker push [OPTIONS] NAME[:TAG]

eg:

> docker push localhost:8080/myname/myApp:v1

Reference

Adamsite answered 1/9, 2017 at 3:39 Comment(0)
P
9

Following are the steps to push Docker Image to Private Repository of DockerHub

1- First check Docker Images using command

docker images

2- Check Docker Tag command Help

docker tag --help

3- Now Tag a name to your created Image

docker tag localImgName:tagName DockerHubUser\Private-repoName:tagName (tag name is optional. Default name is latest)

4- Before pushing Image to DockerHub Private Repo, first login to DockerHub using command

docker login [provide dockerHub username and Password to login]

5- Now push Docker Image to your private Repo using command

docker push [options] ImgName[:tag] e.g docker push DockerHubUser\Private-repoName:tagName

6- Now navigate to the DockerHub Private Repo and you will see Docker image is pushed on your private Repository with name written as TagName in previous steps

Purveyor answered 14/2, 2021 at 13:11 Comment(0)
S
9

When pushing to a Docker Hub account, whether public or private, the process is the same.

The OP said:

I have a docker image tagged as me/my-image, and I have a private repo on the dockerhub named me-private.
When I push my me/my-image, I end up always hitting the public repo.

The immediate problem is that the private repo (me-private) appears to have a different name than the image (my-image). The repo and the image must have the same name (minus any tag).

TLDR;
An image named my-image or my-image:tag must have a repo name of my-image.

Since the OP's repo was named me-private, Docker Hub wouldn't see those as the same and would create the new repo named my-image.

(That new repo would be public by default, unless you change settings to make all your repositories private.)


As of June 2022, the process for setting up a Docker Hub repo is:

Given the following values:

username = yourusername
image name = theimage
tag = thetag

1) tag (or commit) the local image, adding a prefix with your username:

docker tag theimage:thetag yourusername/theimage:thetag

Notes:

  • if you're in an organization, you need to double prefix the image - like so:
docker tag theimage:thetag yourusername/yourorganizationname/theimage:thetag
  • if your tag is latest, :thetag part can be left out; Docker assumes :latest if you don't enter a :thetag part

2) push the prefixed image to Docker Hub:

 docker push yourusername/theimage:thetag

OR

 docker push yourusername/yourorganizationname/theimage:thetag

For a private repo, one of these extra steps is necessary:

Either

before step 1 above, create a private repository in your Docker Hub account

Remember that the repository name must be the same as theimage that you're planning to push. Do not include a thetag portion on the repository name. Eg, if your image is ubuntu:14.04, you would name your repository ubuntu. (All tagged images - eg, ubuntu:latest, ubuntu:14.04, etc - will go into the ubuntu repo.)

Or

if you didn't create the repository in advance (which is not required!): go to your account in Docker Hub; click on the newly pushed repo, then its Settings tab - and make your repo private.

Safko answered 24/6, 2022 at 16:53 Comment(0)
P
4

Simple working solution:

Go here https://hub.docker.com/ to create a PRIVATE repository with name for example johnsmith/private-repository this is the NAME/REPOSITORY you will use for your image when building the image.

  • First, docker login

  • Second, I use "docker build -t johnsmith/private-repository:01 ." (where 01 is my version name) to create image, and I use "docker images" to confirm the image created such as in this yellow box below: (sorry I can not paste the table format but the text string only)

johnsmith/private-repository(REPOSITORY) 01(TAG) c5f4a2861d6e(IMAGE ID) 2 days ago(CREATED) 305MB(SIZE)

Done!

Pentastich answered 16/9, 2017 at 6:48 Comment(0)
G
3

If somebody looking for a quick way to push all images to a private repository, you can use my bash script - it will push all the Docker images to the new private registry:

#!/bin/bash

repo="<change_to_your_new_repo>"
remote_repo="<the_new_repo_name>"

for img in $(docker images --format "{{.Repository}}:{{.Tag}}")
do
        image=$(echo $img | cut -d ":" -f 1)
        image_tag=$(echo $img | cut -d ":" -f 2)

        docker image tag $image:$image_tag $repo/$remote_repo/$image:$image_tag
        docker image push $repo/$remote_repo/$image:$image_tag

        docker rmi $repo/$remote_repo/$image:$image_tag
done
Genoese answered 13/2, 2022 at 9:15 Comment(0)
A
0

There is also a "default privacy" setting in dockerhub. Visit https://hub.docker.com/settings/default-privacy or click account settings->default privacy.

Set the toggle to "private".

This is not a complete solution but at least private by default is better than public by default. You can go back and make the ones public you want.

Amu answered 21/7, 2021 at 6:33 Comment(0)
C
0

After pull an image in local, you can do the next:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Then docker push using that same tag.

docker push NAME[:TAG]

Example:

docker tag gvenzl/oracle-xe:21-slim quay.io/repository/yourDirectory/oracle_xe:oracle-xe

docker push quay.io/repository/yourDirectory/oracle_xe:oracle-xe
Computer answered 23/2, 2022 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.