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

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

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

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

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

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

 
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                com.spotify
                docker-maven-plugin
                1.1.0
                
                    
                        build-image
                        package
                        
                            build
                        
                    
                
                
                    mall/${project.artifactId}:${project.version}
                    
                    http://192.168.1.232:2375
                    java:8
                    ["java", "-jar", "-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]
                    
                        
                            /
                            ${project.build.directory}
                            ${project.build.finalName}.jar
                        
                    
                
            
        
    

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

MAVEN插件编译镜像Docker上传远程服务器Docker容器_第1张图片

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

MAVEN插件编译镜像Docker上传远程服务器Docker容器_第2张图片

第二种:

在ExecStart=后面添加

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

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

  MAVEN插件编译镜像Docker上传远程服务器Docker容器_第3张图片

//=========================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镜像中了

你可能感兴趣的:(docker,maven-docker,linux)