繁体   English   中英

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

[英]How to push a docker image to a private repository

提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文

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

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

您需要首先使用您的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

只需三个简单的步骤:

  1. docker login --username username

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

  3. docker push username/my-repo

如果您的 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

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

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

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

如何将您的 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 终端命令(从 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 文件中: 在此处输入图像描述

现在在您的 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 的解决方案 = 不要将<execution>与 maven 部署阶段同步,因为这样 maven 会尝试 2x 部署映像并在 jar 上放置时间戳。 这就是我使用<phase>install</phase>的原因。

有两种选择:

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

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

参考: 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 -> Preferences -> Daemon ,添加insecure registry

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

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

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

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

在 dockerhub 上创建存储库:

$docker tag IMAGE_ID UsernameOnDockerhub/repoNameOnDockerhub:latest

$docker push UsernameOnDockerhub/repoNameOnDockerhub:latest

注意:此处“repoNameOnDockerhub”:您提到的名称的存储库必须存在于 dockerhub

“最新”:只是标签

以下是将 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 docker login [提供 dockerHub 用户名和密码登录]

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

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

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

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

> 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

参考

简单的工作解决方案:

去这里https://hub.docker.com/创建一个 PRIVATE 存储库,其名称例如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 天前(CREATED) 305MB(SIZE)

完毕!

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

将切换设置为“私人”。

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

如果有人正在寻找一种将所有镜像推送到私有存储库的快速方法,您可以使用我的 bash 脚本 - 它会将所有 Docker 镜像推送到新的私有注册表:

#!/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

在本地拉取图像后,您可以执行以下操作:

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

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

码头工人推名称[:标签]

例子:

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

推送到Docker Hub帐户时,无论是公共帐户还是私有帐户,过程都是相同的。

OP说:

我有一个标记为 me/my-image 的 docker 映像,并且我在 dockerhub 上有一个名为 me-private 的私有仓库。
当我推动我/我的形象时,我最终总是会点击公共回购。

直接的问题是私有仓库 ( me-private ) 的名称似乎与图像 ( my-image ) 的名称不同。 存储库和图像必须具有相同的名称(减去任何标签)。

TLDR;
名为my-imagemy-image:tag必须具有my-image的存储库名称。

由于 OP 的 repo 被命名为me-private ,Docker Hub 不会将它们视为相同的,并将创建名为my-image的新 repo。

(默认情况下,新的存储库是公开的,除非您更改设置以将所有存储库设为私有。)


截至 2022 年 6 月,设置Docker Hub存储库的流程为:

给定以下值:

用户名 = 你的用户名
图像名称 = 图像
标记 = 标记

1)标记(或提交)本地图像,使用您的用户名添加前缀

docker tag theimage:thetag yourusername/theimage:thetag

备注

  • 如果您在一个组织中,则需要为图像添加双前缀- 如下所示:
docker tag theimage:thetag yourusername/yourorganizationname/theimage:thetag
  • 如果您的标签是latest的, :thetag部分可以省略; 如果您不输入:thetag部分,Docker 假定:latest

2)将前缀镜像推送到 Docker Hub:

 docker push yourusername/theimage:thetag

或者

 docker push yourusername/yourorganizationname/theimage:thetag

对于私人仓库,需要以下额外步骤之一:

任何一个

在上述第 1 步之前,在您的 Docker Hub 帐户中创建一个私有存储库

请记住,存储库名称必须与您计划推送的theimage相同。 不要在存储库名称中包含thetag部分。 例如,如果您的图像是ubuntu:14.04 ,您可以将存储库命名为ubuntu (所有标记的图像 - 例如, ubuntu:latestubuntu:14.04等 - 将进入ubuntu存储库。)

或者

如果您没有提前创建存储库(这不是必需的! ):转到您在Docker Hub中的帐户; 单击新推送的存储库,然后单击其设置选项卡 - 并将您的存储库设为私有。

问题未解决?试试搜索: 如何将 docker 镜像推送到私有仓库
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM