我们用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镜像中了