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
> docker login [OPTIONS] [SERVER]
[OPTIONS]:
-u username
-p password
eg:
> docker login localhost:8080
> docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
eg:
> docker tag myApp:v1 localhost:8080/myname/myApp:v1
>docker push [OPTIONS] NAME[:TAG]
eg:
> docker push localhost:8080/myname/myApp:v1
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)
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=gc.genechaung@gmail.com
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:
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 Eclipse Directory:
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:
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>
.
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