【问题标题】:How to push a docker image to a private repository如何将 docker 镜像推送到私有仓库
【发布时间】:2015-04-05 15:05:07
【问题描述】:

我有一个标记为 me/my-image 的 docker 映像,并且我在 dockerhub 上有一个名为 me-private 的私有存储库。
当我推送我的me/my-image 时,我最终总是会访问公共回购。

将我的图像专门推送到我的私人仓库的确切语法是什么?

【问题讨论】:

标签: docker docker-registry


【解决方案1】:

您需要首先使用您的registryhost 正确标记您的图像:

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

然后 docker push 使用相同的标签。

docker push NAME[:TAG]

例子:

docker tag 518a41981a6a myRegistry.com/myImage
docker push myRegistry.com/myImage

【讨论】:

  • 那么,对于这张图片:518a41981a6a,让它进入我的私人仓库的实际标签语法是什么?
  • docker tag 518a41981a6a me-private.com/myPrivateImage && docker push me-private.com/myPrivateImage
  • 我在上面的回答中也修复了一些语法问题。此外,当您推送到注册表时,您必须使用实际的图像名称而不是图像 ID。
  • 哦,如果您使用的是私有 dockerhub 注册表,它应该非常简单。确保您首先执行docker login,然后标记您的图像:docker tag 518a41981a6a me-private/myPrivateImage 并推送:docker push me-private/myPrivateImage
  • 如果我更新图像并使用相同的标签,它会覆盖它吗?
【解决方案2】:

只需三个简单的步骤:

  1. docker login --username username

    • 如果您省略 --password,则提示输入密码,建议这样做,因为它不会将其存储在您的命令历史记录中
  2. docker tag my-image username/my-repo

  3. docker push username/my-repo

【讨论】:

  • 如果您不希望密码出现在历史记录中,请不要使用 --password 标志。它会提示你。
  • 不工作,因为没有提到私人注册主机名。
  • @BorisIvanov,你是什么意思?
  • @cowlinator,这个答案似乎是使用 Docker Hub 而不是问题所要求的私人仓库。
  • 这没有回答如何推送到 private 存储库的问题。
【解决方案3】:

如果您的 docker 注册表是私有的并且是自托管的,您应该执行以下操作:

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

例子:

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

【讨论】:

    【解决方案4】:

    首先转到您的 Docker Hub 帐户并创建 repo。这是我的 Docker Hub 帐户的屏幕截图:

    从图中可以看出我的repo是“chuangg”

    现在进入存储库并通过单击您的图像名称将其设为私有。所以对我来说,我点击了“chuangg/gene_commited_image”,然后我去设置 -> 设为私有。然后我按照屏幕上的说明进行操作

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

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

    1) docker commit &lt;container ID&gt; &lt;repo name&gt;/&lt;Name you want to give the image&gt;

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

    例如=docker commit 99e078826312 chuangg/gene_commited_image

    2)docker run -it chaung/gene_commited_image

    3)docker login --username=&lt;user username&gt; --password=&lt;user password&gt;

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

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

    4) docker push chuangg/gene_commited_image

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

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

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

    注意,方法#2 和#3 的区别在于方法#3 有一个额外的&lt;execution&gt; 用于部署。

    方法 #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

    对于那些不使用 Maven 配置文件的人,只需输入 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 的解决方案 = 不要将 &lt;execution&gt; 与 maven 部署阶段同步,因为这样 maven 会尝试 2x 部署映像并在 jar 上放置时间戳。这就是我使用&lt;phase&gt;install&lt;/phase&gt; 的原因。

    【讨论】:

      【解决方案5】:

      有两种选择:

      1. 进入中心,首先创建存储库,并将其标记为私有。然后,当您推送到该存储库时,它将是私有的。这是最常用的方法。

      2. 登录您的 docker hub 帐户,然后转到您的global settings。有一个设置允许您设置您推送的存储库的默认可见性。默认情况下,它设置为公共,但如果您将其更改为私有,则您推送的所有存储库将默认标记为私有。请务必注意,您的帐户需要有足够的私有存储库可用,否则存储库将被锁定,直到您升级计划。

      【讨论】:

      • 当然,发布的问题并不像人们希望的那样简单,但我毫不怀疑提问者的关键问题是 Docker Hub 上的 repositories 是默认公开。然而,这个线程上的每个人都忙于为私人 registries 提供 wiki 以及接触docker push 命令。但是,如果我正确理解了这个问题,那么这些答案都不是正确的,而上面 Ken Cochrane 发布的答案是唯一应该被接受的答案。
      • 正是我想要的。正如@MartinAndersson 所提到的,如果您在 DockerHub 上推送,上述命令行中的答案仍然会让您推送的图像公开。
      • 我同意@MartinAndersson,这应该是 OP 问题的正确答案。
      【解决方案6】:

      参考:dock.docker.com

      本主题提供有关部署和配置注册表的基本信息

      运行本地注册表

      在部署注册表之前,您需要在主机上安装 Docker。

      使用如下命令启动注册表容器:

      start_registry.sh

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

      将镜像从 Docker Hub 复制到您的注册表

      1. 从 Docker Hub 拉取 ubuntu:16.04 映像。

        $ docker pull ubuntu:16.04
        
      2. 将图像标记为localhost:5000/my-ubuntu。这会为现有图像创建一个附加标签。当标签的第一部分是主机名和端口时,Docker 在推送时将其解释为注册表的位置。

        $ docker tag ubuntu:16.04 localhost:5000/my-ubuntu
        
      3. 将镜像推送到在localhost:5000运行的本地注册表:

        $ docker push localhost:5000/my-ubuntu
        
      4. 删除本地缓存的图像。这不会从您的注册表中删除 localhost:5000/my-ubuntu 图像。

        $ docker image remove ubuntu:16.04
        $ docker image remove localhost:5000/my-ubuntu
        
      5. 从本地注册表中提取 localhost:5000/my-ubuntu 图像。

        $ docker pull localhost:5000/my-ubuntu
        
      部署一个普通的 HTTP 注册表

      根据docs.docker.com,这非常不安全,不推荐

      1. 编辑daemon.json 文件,其默认位置在Linux 上为/etc/docker/daemon.json,在Windows Server 上为C:\ProgramData\docker\config\daemon.json。如果你使用Docker for MacDocker for Windows,点击Docker icon -&gt; Preferences -&gt; Daemon,添加insecure registry

        如果daemon.json 文件不存在,请创建它。假设文件中没有其他设置,它应该有以下内容:

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

        启用不安全的注册表后,Docker 会执行以下步骤:

        • 首先,尝试使用 HTTPS。
          • 如果 HTTPS 可用但证书无效,请忽略证书错误。
          • 如果 HTTPS 不可用,请回退到 HTTP。
      2. 重启 Docker 以使更改生效。

      【讨论】:

        【解决方案7】:

        在 dockerhub 上创建仓库:

        $docker tag IMAGE_ID UsernameOnDockerhub/repoNameOnDockerhub:latest

        $docker push UsernameOnDockerhub/repoNameOnDockerhub:latest

        注意:这里 "repoNameOnDockerhub" :您提到的名称的存储库有 出现在 dockerhub 上

        "latest" : 只是标签

        【讨论】:

        • repoNameOnDockerhub 是非法图像名称,因为规则规定它必须由小写字符组和由分隔符分隔的数字组成,分隔符可以是破折号/连字符、下划线或双下划线。文档中的一个地方说点/句点可以是分隔符,而另一个没有提到它。我怀疑用户名也必须是小写的,但我只能在 dockerhub 上找到非正式规则,说他们要求用户名有小写字母、数字、破折号和下划线,长度为 2-255。
        【解决方案8】:

        首先登录你的私有仓库。

        > docker login [OPTIONS] [SERVER]
        
        [OPTIONS]: 
        -u username 
        -p password
        

        例如:

        > docker login localhost:8080
        

        然后为您的私有存储库标记您的图像

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

        例如:

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

        最后将你标记的图片推送到你的私有仓库

        >docker push [OPTIONS] NAME[:TAG]
        

        例如:

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

        参考

        【讨论】:

          【解决方案9】:

          以下是将 Docker Image 推送到 DockerHub 的 Private Repository 的步骤

          1- 首先使用命令检查 Docker 镜像

          docker images

          2- 检查 Docker Tag 命令帮助

          docker tag --help

          3- 现在为您创建的图像添加名称

          docker tag localImgName:tagName DockerHubUser\Private-repoName:tagName(标签名称可选,默认名称为latest

          4- 在将镜像推送到 DockerHub Private Repo 之前,首先使用命令登录 DockerHub

          docker login[提供dockerHub用户名和密码登录]

          5- 现在使用命令将 Docker Image 推送到您的私有仓库

          docker push [options] ImgName[:tag] 例如docker push DockerHubUser\Private-repoName:tagName

          6- 现在导航到 DockerHub 私有存储库,您将看到 Docker 映像被推送到您的私有存储库,名称在前面的步骤中写为 TagName

          【讨论】:

            【解决方案10】:

            简单的工作解决方案:

            转到此处https://hub.docker.com/ 创建一个私有存储库,其名称例如为johnsmith/private-repository,这是您在构建映像时将用于映像的NAME/REPOSITORY

            • 首先,docker login

            • 其次,我使用“docker build -t johnsmith/private-repository:01 .”(其中 01 是我的版本名称)创建图像,并使用“docker images”确认创建的图像,例如下面这个黄色框:(对不起我不能粘贴表格格式,只能粘贴文本字符串)

            johnsmith/private-repository(REPOSITORY) 01(TAG) c5f4a2861d6e(IMAGE ID) 2 天前(已创建) 305MB(SIZE)

            完成!

            【讨论】:

              【解决方案11】:

              dockerhub 中还有一个“默认隐私”设置。访问https://hub.docker.com/settings/default-privacy或点击账户设置->默认隐私。

              将切换设置为“私人”。

              这不是一个完整的解决方案,但至少默认情况下私有优于默认情况下的公共。您可以返回并公开您想要的。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-12-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-08-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多