1、开启docker远程访问
#CentOS 6,修改/etc/default/docker文件,重启后生效(service docker restart)。
DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:2375"
#CentOS 7,打开/usr/lib/systemd/system/docker.service文件,修改ExecStart这行。
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
#重启后生效
systemctl daemon-reload
systemctl restart docker.service
#测试是否生效
curl http://127.0.0.1:2375/info
2、关闭Linux防火墙
#查看防火墙状态,绿的running表示防火墙开启:
systemctl status firewalld.service
#执行关闭命令:
systemctl stop firewalld.service
#再次执行查看防火墙命令
systemctl status firewalld.service
#执行开机禁用防火墙自启命令
systemctl disable firewalld.service
#启动
systemctl start firewalld.service
#防火墙随系统开启启动
systemctl enable firewalld.service
2、修改pom文件,添加docker插件
<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.2.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!--<dockerHost>http://192.168.199.135:2375</dockerHost>-->
<dockerHost>http://192.168.199.135:2375</dockerHost>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
3、编写Dockerfile
在src/main下面新建一个docker目录,并建一个Dockerfile文件
Dockerfile内容如下:
FROM java:8
MAINTAINER demo demo@xxx.com
VOLUME /ROOT/docker_springboot
ADD sboot-01-helloworld-quick-0.0.1-SNAPSHOT.jar sboothelloworld.jar
RUN bash -c 'touch /sboothelloworld.jar'
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "sboothelloworld.jar"]
4、build 项目镜像
在Idea的命令行执行maven命令
mvn clean install -DskipTests docker:build
或者 Idea的maven插件中的docker:build
执行完毕,在docker中查看build好的镜像
docker images
5、启动容器
#前台启动,然后Ctrl + P + Q 退到后台
docker run -it --name springBootDocker -p 8080:8080 sboothelloworld/sboot-01-helloworld-quick:latest
#后台启动,然后Ctrl + P + Q 退到后台
docker run -d --name springBootDocker -p 8080:8080 sboothelloworld/sboot-01-helloworld-quick:latest
测试SpringBoot接口
http://192.168.199.135:8080/docker
注意:本次过程中,启动容器时出现的问题
docker: Error response from daemon: driver failed programming
external connectivity on endpoint springBootDocker
(3e11fee9833f9afb79c653da5c45e0f724ecb1ed27ecb0ce9380ebf676963c41):
(iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0
--dport 8000 -j DNAT --to-destination 172.17.0.2:8080 ! -i
docker0: iptables: No chain/target/match by that name.
(exit status 1)).
解决:重启docker
systemctl restart docker