首页 文章

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

提问于
浏览
278

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

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

9 回答

  • 41

    您需要先使用 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
    
  • 3

    只需三个简单的步骤:

    • docker login --username username --password password

    • docker tag my-image username/my-repo

    • docker push username/my-repo

  • 23

    首先转到Docker Hub帐户并进行回购 . 以下是我的Docker Hub帐户的屏幕截图:
    enter image description here

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

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

    如何将你的DOCKER图像上传到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>

    是的,我认为它必须是容器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

    Method #2= Pushing your image using pom.xml and command line.

    注意,我使用了名为“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> 用于部署 .

    Method #3= Using Maven to automatically deploy to 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> 的原因 .

  • 13

    如果您的docker注册表是私有 and self hosted ,您应该执行以下操作:

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

    有两种选择:

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

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

  • 149

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

    > 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
    

    参考

  • 3

    简单工作方案:

    到这里 https://hub.docker.com/ 创建一个名称为 johnsmith/private-repository 的PRIVATE存储库,这是构建图像时用于图像的 NAME/REPOSITORY .

    • 首先, docker login

    • 其次,我使用“ docker build -t johnsmith/private-repository:01 . " (where 01 is my version name) to create image, and I use " docker images ”来确认创建的图像,如下面的黄色框中所示:(抱歉,我不能粘贴表格格式而只是文本字符串)

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

    完成!

  • 408

    参考: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复制到您的注册处

    • 从Docker Hub中拉出 ubuntu:16.04 图像 .
    $ docker pull ubuntu:16.04
    
    • 将图像标记为 localhost:5000/my-ubuntu . 这会为现有图像创建一个附加标记 . 当标记的第一部分是主机名和端口时,Docker在推送时将其解释为注册表的位置 .
    $ docker tag ubuntu:16.04 localhost:5000/my-ubuntu
    
    • 将图像推送到运行在 localhost:5000 的本地注册表:
    $ docker push localhost:5000/my-ubuntu
    
    • 删除本地缓存的图像 . 这不会从您的注册表中删除 localhost:5000/my-ubuntu 图像 .
    $ docker image remove ubuntu:16.04
    $ docker image remove localhost:5000/my-ubuntu
    
    • 从本地注册表中拉出 localhost:5000/my-ubuntu 图像 .
    $ docker pull localhost:5000/my-ubuntu
    

    部署纯HTTP注册表
    根据docs.docker.com,这是 very 不安全,是 not recommended .

    • 编辑 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 .

    • 重新启动Docker以使更改生效 .

  • 0

    在dockerhub上创建存储库:

    $docker tag IMAGE_ID UsernameOnDockerhub/repoNameOnDockerhub:latest

    $docker push UsernameOnDockerhub/repoNameOnDockerhub:latest

    Note :这里"repoNameOnDockerhub":您提及的名称的存储库必须出现在dockerhub上

    “最新”:只是标签

相关问题