MAVEN插件编译镜像Docker上传远程服务器Docker容器

2 篇文章 0 订阅
1 篇文章 0 订阅

我们用Maven来构建Docker镜像上传到服务器

  maven是一个很牛很厉害的项目管理构建工具,

  那我们现在来使用maven来构建docker镜像,

  并自动远程上传到私服仓库

1.首先我们在POM文件中添加docker-maven-plugin插件设置好远程地址

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.1.0</version>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <imageName>mall/${project.artifactId}:${project.version}</imageName>
                    <!--docker服务器地址及端口-->
                    <dockerHost>http://192.168.1.232:2375</dockerHost>
                    <baseImage>java:8</baseImage>
                    <entryPoint>["java", "-jar", "-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.然后在编译工具中打包上传,mvn package

3.此时上传还会遇到下列错误

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.1.0:build (build-image) on project mall-admin: Exception caught: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: com.spotify.docker.client.shaded.org.apache.http.conn.HttpHostConnectException: Connect to 192.168.1.232:2375 [/192.168.1.232] failed: Connection refused (Connection refused) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Connect to 192.168.1.232:2375 [/192.168.1.232] failed: Connection refused (Connection refused) -> [Help 1]

这个时候服务器是拒绝连接

那我们需要修改一下docker配置文件 在/usr/lib/systemd/system目录下docker.service

vi 编辑 下    下面安装的Docker 配置文件有两种,修改对应的配置有所不同分别是

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

 

第一种:

把ExecStart=/usr/bin/dockerd -H 这一行替换为

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

第二种:

在ExecStart=后面添加

-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \

//=========================before=========================

  

//=========================after=========================

ExecStart=/usr/bin/dockerd-current \
          -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \
          --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
          --default-runtime=docker-runc \
          --exec-opt native.cgroupdriver=systemd \
          --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
          --init-path=/usr/libexec/docker/docker-init-current \
          --seccomp-profile=/etc/docker/seccomp.json \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
          $REGISTRIES

4.加载docker配置文件

    加载docker守护线程

systemctl daemon-reload

5.重启docker

systemctl restart docker

6.关闭防火墙或者开启防火墙端口

  方式一:开启端口

firewall-cmd --zone=public --add-port=2375/tcp --permanent

  方式二:关闭防火墙

systemctl  stop firewalld

最后我们重新 package 下

docker images 

就能看到我们的项目已经打包上传到docker镜像中了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值