18

我已经创建了自己的 docker 文件(运行一个打印“helloworld”的 shell 脚本)。图像是“hellodocker”,标签是“mytag”我现在有:

bash-3.2$ docker images
REPOSITORY          TAG                   IMAGE ID            CREATED                 VIRTUAL SIZE
hellodocker         mytag                 3514c8dc11a8        39 minutes ago      2.433 MB
busybox             buildroot-2013.08.1   d200959a3e91        10 weeks ago        2.489 MB
busybox             ubuntu-14.04          37fca75d01ff        10 weeks ago        5.609 MB
busybox             ubuntu-12.04          fd5373b3d938        10 weeks ago        5.455 MB
busybox             buildroot-2014.02     a9eb17255234        10 weeks ago        2.433 MB
busybox             latest                a9eb17255234        10 weeks ago        2.433 MB

docker ps -a
CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS                        PORTS               NAMES
97c29510069e        hellodocker:mytag           /bin/sh -c /Users/in   33 minutes ago                  Exited (127) 26 minutes ago                       happy_pasteur       
8d04a1385c24        hellodocker:mytag           /bin/sh -c /Users/in   37 minutes ago      Exited (127) 30 minutes ago                       mad_bell            
8998d61c0513        hellodocker:mytag           /bin/sh -c /Users/in   37 minutes ago      Exited (127) 30 minutes ago                       boring_thompson         
64314c304a29        hellodocker:mytag           /bin/sh -c /Users/in   37 minutes ago      Exited (127) 31 minutes ago                       sad_wilson          
8bc20e0555b8        hellodocker:mytag           /bin/sh -c /Users/in   38 minutes ago      Exited (127) 31 minutes ago                       sleepy_mayer        
97664a4ba870        hellodocker:mytag           .                      38 minutes ago                                                        kickass_poincare    
8bb752631cb6        busybox:buildroot-2014.02   /bin/echo Hello Doct   18 hours ago        Exited (0) 18 hours ago                           dreamy_kowalevski   
6aa66b55ca94        busybox:buildroot-2014.02   bash-3.2$ sudo docke   18 hours ago                                                          ecstatic_lovelace   
2cc657f65342        busybox:buildroot-2014.02   /bin/echo Hello Dock   18 hours ago        Exited (0) 18 hours ago                           dreamy_poincare 

如何将 docker 镜像推送到 dockerhub?我用docker push <myuserid>/hellodocker. 这给出了“没有这样的 id”错误。我错过了什么?提前致谢。

4

3 回答 3

36

您要么需要<myuser>/hellodocker在构建时标记它,例如

docker build -t <myuser>/hellodocker:mytag .

或创建一个绑定到同一图像的新标签,即

docker tag hellodocker:mytag <myuser>/hellodocker:mytag
于 2014-08-19T18:19:24.857 回答
9

如果你想要命名空间hellodocker下的存储库myuserid,你必须首先标记你的本地hellodocker喜欢myuserid

码头工人标签 hellodocker myuserid/hellodocker

然后将此存储库推myuserid/hellodocker送到集线器,例如:

docker push myuserid/hellodocker

于 2014-08-19T18:47:37.973 回答
1

首先转到您的 Docker Hub 帐户并制作 repo。这是我的 Docker Hub 帐户的屏幕截图: 在此处输入图像描述

从图中可以看出我的repo是“chuangg”</p>

如何将您的 Docker 映像上传到 Docker HUB

方法 #1= 通过命令行 (cli) 推送图像

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

是的,我认为它必须是容器 ID。它可能不能是图像 ID。

例如=docker commit 99e078826312 chuangg/gene_commited_image

2)docker run -it chaung/gene_commited_image

3)docker login --username=<user username> --email=<user email address>

例如=docker login --username=chuangg --email=gc.genechaung@gmail.com

是的,您必须先登录。使用“docker logout”注销</p>

4)docker push chuangg/gene_commited_image

方法 #2= 使用 pom.xml 和命令行推送您的图像。

请注意,我使用了一个名为“build-docker”的 Maven 配置文件。如果您不想使用配置文件,只需删除<profiles>, <profile>, and <id>build-docker</id>元素。

在父 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 映像的 Docker 终端命令(从 pom.xml 所在的目录)=mvn clean deploy -Pbuild-docker docker:push

对于那些不使用 Maven 配置文件的人,命令只是mvn clean deploy docker:push

请注意,方法#2 和#3 之间的区别在于方法#3 有一个额外<execution>的部署。

方法 #3= 使用 Maven 自动部署到 Docker Hub

将此内容添加到您的父 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>

转到 C:\Users\Gene.docker\ 目录并将其添加到您的 config.json 文件中: 在此处输入图像描述

现在在您的 Docker 快速入门终端中输入 =mvn clean install -Pbuild-docker

对于那些不使用个人资料的人,只需键入mvn clean install

这是一个屏幕截图,如果它有效,它会是什么样子: 在此处输入图像描述

这是我的完整 pom.xml 和我的目录结构的屏幕截图:

<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>

这是我的 Eclipse 目录: 在此处输入图像描述

这是我的 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" ] 

常见错误 #1: 在此处输入图像描述

错误 #1 的解决方案 = 不要<execution>与 maven 部署阶段同步,因为这样 maven 会尝试将映像部署 2x 并在 jar 上放置时间戳。这就是我使用<phase>install</phase>.

于 2016-12-01T19:37:20.887 回答