使用Maven来构建Docker镜像并上传到私服仓库

准备工作

开启远程端口
docker服务器上
1.直接在公有仓库拉取仓库的镜像

docker pull registry

2.修改docker配置

vim   /usr/lib/systemd/system/docker.service

ExecStart 添加 -H tcp://0.0.0.0:2375
添加后如下

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd --insecure-registry 0.0.0.0:5000  -H tcp://0.0.0.0:2375  -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target

重新加载配置文件重启

systemctl daemon-reload
systemctl restart docker

maven配置:
无需yml的pom.xml配置

 <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <configuration>
                   <forceTags>true</forceTags>    <!--覆盖相同标签镜像-->
                    <imageName>my/eureka:0.0.1</imageName>  <!--指定镜像名称 仓库/镜像名:标签-->
                    <baseImage>lwieske/java-8</baseImage>    <!--指定基础镜像,相当于dockerFile中的FROM<image> -->
                    <dockerHost>http://192.168.10.72:2375</dockerHost>  <!-- 指定仓库地址 -->
                    <entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint> <!-- 容器启动执行的命令,相当于dockerFile的ENTRYPOINT -->
                    <resources>
                        <resource>                                             <!-- 指定资源文件 -->
                            <targetPath>/</targetPath>                         <!-- 指定要复制的目录路径,这里是当前目录 -->
                            <directory>${project.build.directory}</directory>  <!-- 指定要复制的根目录,这里是target目录 -->
                            <include>${project.build.finalName}.jar</include>  <!-- 指定需要拷贝的文件,这里指最后生成的jar包 -->
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

需要yml的pom文件配置

<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <configuration>
                    <forceTags>true</forceTags>    <!--覆盖相同标签镜像-->
                    <imageName>my/eureka:0.0.2</imageName>  <!--指定镜像名称 仓库/镜像名:标签-->
                    <dockerHost>http://192.168.10.73:2375</dockerHost>  <!-- 指定仓库地址 -->
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>   <!--dockerfile文件路径-->
                    <resources>
                        <resource>                                             <!-- 指定资源文件 -->
                            <targetPath>/</targetPath>                         <!-- 指定要复制的目录路径,这里是当前目录 -->
                            <directory>${project.build.directory}</directory>  <!-- 指定要复制的根目录,这里是target目录 -->
                            <include>${project.build.finalName}.jar</include>  <!-- 指定需要拷贝的文件,这里指最后生成的jar包 -->
                        </resource>
                    </resources>
                </configuration>
            </plugin>

maven 发布
重点!!!!
镜像名称一定不能有大写字母
因为镜像名称取的是项目名称,而项目名称有大写英文字母导致镜像一直上传失败提示:Broken pipe (Write failed)
在maven中配置名称小写就上传成功了

clean package  docker:build
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容