Modify docker.service
the configuration and open port 2375
vi /usr/lib/systemd/system/docker.service
-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
Restart docker
# 重新加载服务的配置文件
systemctl daemon-reload
# 重启 docker
service docker restart
# 开放端口
firewall-cmd --zone=public --permanent --add-port=2375/tcp
# 重启防火墙
systemctl restart firewalld
Configuration
Dockerfile
FROM hub.c.163.com/library/java:8-jdk
VOLUME /tmp
ADD target/docker_deploy-0.0.1-SNAPSHOT.war app.war
ENTRYPOINT ["java","-jar","/app.war"]
- The operation of the SpringBoot project relies on the Java environment, so the custom image must be built based on the Java image. The Docker image provided by NetEase is used here.
- SpringBoot requires a tmp directory when running. Here, the data volume is configured with a /tmp directory.
- Copy a new copy of the packaged .war file in the local target directory to /app.war.
- Configure the startup command.
pom.xml
Solve the problem that there is no /src/main/webapp/WEB-INF/web.xml file when generating a war package
<properties>
<!-- 如果打包方式为 war 包,并且没有 /src/main/webapp/WEB-INF/web.xml 文件 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
Add new plug-in
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<dockerHost>http://192.168.179.128:2375</dockerHost>
<imageName>yueyazhui/${project.artifactId}</imageName>
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
<forceTags>true</forceTags>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.war</include>
</resource>
</resources>
</configuration>
</plugin>
- Configure in the execution node that when executing mvn package, docker:build will be executed.
- Configure Docker's host address, image name, and image tags in the configuration node, where dockerDirectory represents the location of the specified Dockerfile.
- Just configure the location and name of the war in the resource node.
Controller
@RestController
public class IndexController {
@GetMapping
public String index() {
return "Docker Deploy";
}
}
Package build image
run
Option One:
docker run -d --name docker_deploy_demo -p 8080:8080 yueyazhui/docker_deploy:0.0.1-SNAPSHOT
Option 2: (Recommended, run the image through IDEA)
Finish!