安装docker服务或者系统学习docker参考这篇文档:https://shimo.im/docs/fE0eJCx8IIojQXzB/
1.配置docker的远程端口
vim /usr/lib/systemd/system/docker.service
找到ExecStart,在最后追加
-H tcp://0.0.0.0:2375
2.重启docker
systemctl daemon-reload
systemctl stop docker
systemctl start docker
3.如果开启了防火墙需开发端口,我这里直接将防火墙关闭,所以不用执行此命令
firewall-cmd --zone=public --add-port=2375/tcp --permanent
4.idea安装docker插件
5.连接远程docker
连接成功够会展示docker中容器和镜像
6.之后在需要部署的项目pom文件中添加以下配置:
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.springframework.bootgroupId> 5 <artifactId>spring-boot-maven-pluginartifactId> 6 plugin> 7 <plugin> 8 <groupId>com.spotifygroupId> 9 <artifactId>docker-maven-pluginartifactId> 10 <version>1.0.0version> 11 <configuration> 12 <dockerDirectory>src/main/dockerdockerDirectory> 13 <resources> 14 <resource> 15 <targetPath>/targetPath> 16 <directory>${project.build.directory}directory> 17 <include>${project.build.finalName}.jarinclude> 18 resource> 19 resources> 20 configuration> 21 plugin> 22 <plugin> 23 <artifactId>maven-antrun-pluginartifactId> 24 <executions> 25 <execution> 26 <phase>packagephase> 27 <configuration> 28 <tasks> 29 <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}">copy> 30 tasks> 31 configuration> 32 <goals> 33 <goal>rungoal> 34 goals> 35 execution> 36 executions> 37 plugin> 38 39 plugins> 40 build>
7.在src/main目录下创建docker目录,并创建Dockerfile文件,文件内容:
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
8.idea配置docker
Image tag : 指定镜像名称和tag,镜像名称为 docker-demo,tag为1.1
Bind ports : 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]
9.添加maven打包命令:第一步清除上次编译或者打包的文件,第二步是对项目进行编译以及打成jar
包
clean package -Dmaven.test.skip=true
10.启动完成后
访问: