Docker部署Springboot服务
1、部署springboot之前需先pull一个jdk8,如果在镜像中找不到,就会直接去线上镜像仓库中pull,我这里已就绪
[root@data-anal docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/java 8 d23bdf5b1b1b 5 years ago 643MB
2、新建一个docker目录,用来放要启动的jar和Dockerfile
[root@project-deve docker]# ll 总用量 120836 -rw-r--r--. 1 root root 174 7月 22 10:31 Dockerfile -rw-r--r--. 1 root root 123728753 7月 22 10:31 oneAccept.jar
3、编写Dockerfile
##以java8镜像为基础镜像构建boot镜像 FROM java:8 ##所有者 MAINTAINER email@qq.com ##开放容器中的8081端口,一般为boot应用的启动端口 EXPOSE 8081 ## 挂载/tmp 目录 VOLUME /tmp ##将当前目录的jar拷贝至容器中并重新命令 COPY oneAccept.jar /app.jar ##容器启动时执行的命令 ENTRYPOINT ["java","-jar","/app.jar"]
4、构建镜像
## -t ,-tag 镜像的名字 . 使用当前目录下Dockerfile
[root@free-share docker]# docker build -t bootdemo .
Sending build context to Docker daemon 123.7 MB
Step 1/6 : FROM java:8
---> d23bdf5b1b1b
Step 2/6 : MAINTAINER bodata@bodatasvc.com
---> Running in c9bd65d2ca6e
---> 167ebe337b99
Removing intermediate container c9bd65d2ca6e
Step 3/6 : EXPOSE 8081
---> Running in f8413fc4fa4e
---> 5fd85f768eb1
Removing intermediate container f8413fc4fa4e
Step 4/6 : VOLUME /tmp
---> Running in 8957e50a6016
---> ee84011534ae
Removing intermediate container 8957e50a6016
Step 5/6 : COPY oneAccept.jar /app.jar
---> 9559d8270c6c
Removing intermediate container 3e6c9d962f22
Step 6/6 : ENTRYPOINT java -jar /app.jar
---> Running in 36d0911c53a9
---> f98e6be95e5a
Removing intermediate container 36d0911c53a9
Successfully built f98e6be95e5a
[root@free-share docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bootdemo latest f98e6be95e5a 7 minutes ago 767 MB
docker.io/java 8 d23bdf5b1b1b 5 years ago 643 MB
5、镜像构建好后,创建并启动容器
## -h:给容器修改hostname ## -v:将宿主机目录挂载至容器中,[宿主机目录]:[容器目录],此处/logs为容器中boot应用日志输出目录,可以在宿主机中实时查看容器中应用日志 [root@free-share docker]# docker run -itd -p 9081:8081 --name=bootdemo -h boot -v /logs:/logs --privileged bootdemo:latest 60023b26675646539664e00a2bbb42389b1a32c3d77f3d2699816f805cdf4312 [root@free-share docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60023b266756 bootdemo:latest "java -jar /app.jar" 10 seconds ago Up 7 seconds 0.0.0.0:9081->8081/tcp bootdemo 90d407f8bebb 688353a31fde "/bin/bash" 17 hours ago Up 17 hours 0.0.0.0:6022->22/tcp vm01
6、通过宿主机ip访问容器中应用,到此docker部署boot成功。