How to push a docker image to a private repository
DockerDocker RegistryDocker Problem Overview
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?
Docker Solutions
Solution 1 - Docker
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
Solution 2 - Docker
Just three simple steps:
-
docker login --username username
- prompts for password if you omit
--password
which is recommended as it doesn't store it in your command history
- prompts for password if you omit
-
docker tag my-image username/my-repo
-
docker push username/my-repo
Solution 3 - Docker
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
Solution 4 - Docker
First go to your Docker Hub account and make the repo. Here is a screenshot of my Docker Hub account:
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
HOW TO UPLOAD YOUR DOCKER IMAGE ONTO DOCKER HUB
Method #1= Pushing your image through the command line (cli)
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
-
docker run -it chaung/gene_commited_image
-
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”
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:
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:
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 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" ]
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>
.
Solution 5 - Docker
There are two options:
-
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.
-
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.
Solution 6 - Docker
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
-
Pull the
ubuntu:16.04
image from Docker Hub.$ docker pull ubuntu:16.04
-
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
-
Push the image to the local registry running at
localhost:5000
:$ docker push localhost:5000/my-ubuntu
-
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
-
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.
-
Edit the
daemon.json
file, whose default location is/etc/docker/daemon.json
on Linux orC:\ProgramData\docker\config\daemon.json
on Windows Server. If you useDocker for Mac
orDocker for Windows
, clickDocker icon -> Preferences -> Daemon
, add in theinsecure 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.
- First, try using HTTPS.
-
Restart Docker for the changes to take effect.
Solution 7 - Docker
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
Solution 8 - Docker
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
Solution 9 - Docker
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
Solution 10 - Docker
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)
- Third, I use
docker push johnsmith/private-repository:01
(Your private repo will be here example https://hub.docker.com/r/johnsmith/private-repository/)
Done!
Solution 11 - Docker
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.
Solution 12 - Docker
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
Solution 13 - Docker
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