如何将docker镜像推送到私有存储库

时间:2015-02-05 16:42:50

标签: docker docker-registry

我有一个标记为me / my-image的docker图片,我在dockerhub上有一个名为me-private的私人仓库。当我推送我/我的形象时,我最终总是打到公共回购。

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

11 个答案:

答案 0 :(得分:481)

您需要先使用registryhost

正确标记图片
docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

然后docker使用相同的标签推送。

docker push NAME[:TAG]

示例:

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

答案 1 :(得分:186)

只需三个简单的步骤:

  1. docker login --username username --password password

  2. docker tag my-image username/my-repo

  3. docker push username/my-repo

答案 2 :(得分:40)

首先转到您的Docker Hub帐户并制作回购。以下是我的Docker Hub帐户的屏幕截图: enter image description here

从图片中,你可以看到我的回购是“chuangg”

现在点击图片的名称进入回购并将其设为私有。所以对我来说,我点击了“chuangg / gene_commited_image”,然后我去了设置 - >私有化然后我按照屏幕上的说明操作 enter image description here

如何将你的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> --password=<user password>

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

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

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 Terminal命令(来自pom.xml所在的目录)= mvn clean deploy -Pbuild-docker 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文件中: enter image description here

现在,您的Docker快速入门终端类型= mvn clean install -Pbuild-docker

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

即可

以下是成功消息的屏幕截图: enter image description here

这是我的完整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目录: enter image description here

这是我的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: enter image description here

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

答案 3 :(得分:34)

如果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 :(得分:13)

有两种选择:

  1. 进入集线器,首先创建存储库,并将其标记为私有。然后当你推到那个回购时,它将是私有的。这是最常见的方法。

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

答案 5 :(得分:5)

参考: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
    
  6. 部署纯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 -> Preferences -> Daemon,添加insecure registry

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

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

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

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

答案 6 :(得分:4)

首先登录您的私人存储库。

> 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

参考

答案 7 :(得分:3)

简单的工作解决方案:

到这里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)

  • 第三,我使用docker push johnsmith/private-repository:01(你的私人仓库将在这里展示.one

完成!

答案 8 :(得分:2)

以下是将 Docker Image 推送到 DockerHub 私有仓库的步骤

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

泊坞窗图像

2- 检查 Docker 标签命令帮助

docker 标签帮助

3- 现在为您创建的图像标记名称

docker tag localImgName:tagName DockerHubUser\Private-repoName:tagName(标签名称是可选的,默认名称是最新的)

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

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

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

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

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

答案 9 :(得分:1)

在dockerhub上创建存储库:

$docker tag IMAGE_ID UsernameOnDockerhub/repoNameOnDockerhub:latest

$docker push UsernameOnDockerhub/repoNameOnDockerhub:latest

注意:此处  “ repoNameOnDockerhub”:具有您提到的名称的存储库     出现在dockerhub上

“最新”:只是标签

答案 10 :(得分:0)

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

将开关设置为“私人”。

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

相关问题