Docker学习02:Docker部署Springboot项目
Docker安装方法,请参考Docker学习01:Centos7下安装Docker
部署 Springboot项目
一、核心流程
1- 设计Dockerfile文件
2- 构建镜像
3- 运行容器
二、具体操作方案
1- 创建文件夹(/opt/java/docker,这个可以自定义),用于存放Dockerfile,项目war文件
2- 上传war文件;可以通过xftp上传
3- 设计Dockerfile文件(就是一个命名为Dockerfile的txt文件)
#基础镜像 FROM tomcat:8.5.31-jre8 ENV JAVA_OPTS="-server -Xms256M -Xmx512M -Xss512k \ -XX:+AggressiveOpts \ -XX:+UseBiasedLocking \ -XX:PermSize=64M \ -XX:MaxPermSize=300M \ -XX:+DisableExplicitGC \ -XX:MaxTenuringThreshold=10 \ -XX:+UseConcMarkSweepGC \ -XX:+UseParNewGC \ -XX:+CMSParallelRemarkEnabled \ -XX:+UseCMSCompactAtFullCollection \ -XX:LargePageSizeInBytes=128m \ -XX:+UseFastAccessorMethods \ -XX:+UseCMSInitiatingOccupancyOnly \ -Djava.awt.headless=true" #作者信息 MAINTAINER tyj 828366550@qq.com #定义变量、后续会使用,具体路径可以先启动容器然后进入进行查看 ENV DIR_WEBAPP /usr/local/tomcat/webapps/ #删除webapp下所有文件,因为当前应用作为根应用 RUN rm -rf $DIR_WEBAPP/* #设置时区 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo 'Asia/Shanghai' >/etc/timezone #添加本地的war包到容器中 ADD ./*.war /usr/local/tomcat/webapps/ #配置文件夹映射 VOLUME /usr/local/tomcat/webapps/ #配置工作目录 WORKDIR /usr/local/tomcat/webapps/ #暴露端口,映射端口尽量和yml中一致 EXPOSE 8080 #启动tomcat CMD ["catalina.sh", "run"]
4- 构建镜像(注意最后有一个".",表示当前目录)(注意是在/opt/java/docker目录下)
[root@ecs-x-large-2-linux-20200229160057 docker]# docker build -t docker_lop_02 . Sending build context to Docker daemon 79.37MB Step 1/12 : FROM tomcat:8.5.31-jre8 ---> df50c9d355cf Step 2/12 : ENV JAVA_OPTS="-server -Xms256M -Xmx512M -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:PermSize=64M -XX:MaxPermSize=300M -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -Djava.awt.headless=true" ---> Running in 95159201e1a8 Removing intermediate container 95159201e1a8 ---> d4499771c41a Step 3/12 : MAINTAINER tyj 828366550@qq.com ---> Running in c72e8a55e699 Removing intermediate container c72e8a55e699 ---> e4a1d05eca23 Step 4/12 : ENV DIR_WEBAPP /usr/local/tomcat/webapps/ ---> Running in 9c396e1e5ef6 Removing intermediate container 9c396e1e5ef6 ---> 966ce512d5c1 Step 5/12 : RUN rm -rf $DIR_WEBAPP/* ---> Running in 257c30aa5b07 Removing intermediate container 257c30aa5b07 ---> 3ca330f4e605 Step 6/12 : RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Running in 038bd5ae28e7 Removing intermediate container 038bd5ae28e7 ---> 9c7f0720285c Step 7/12 : RUN echo 'Asia/Shanghai' >/etc/timezone ---> Running in fc987458515e Removing intermediate container fc987458515e ---> 8b72e2147bb8 Step 8/12 : ADD ./*.war /usr/local/tomcat/webapps/ ---> fdabb8452331 Step 9/12 : VOLUME /usr/local/tomcat/webapps/ ---> Running in 45d11b03c14a Removing intermediate container 45d11b03c14a ---> 3702aaa88153 Step 10/12 : WORKDIR /usr/local/tomcat/webapps/ ---> Running in 38e1ba39205f Removing intermediate container 38e1ba39205f ---> 0107a69c23b3 Step 11/12 : EXPOSE 8080 ---> Running in 29fee93bdccb Removing intermediate container 29fee93bdccb ---> 87fb8f69146b Step 12/12 : CMD ["catalina.sh", "run"] ---> Running in 731a3b54f72a Removing intermediate container 731a3b54f72a ---> 75f47211aa37 Successfully built 75f47211aa37 Successfully tagged docker_lop_02:latest
5- 运行构建的镜像
[root@ecs-x-large-2-linux-20200229160057 docker]# docker run -it -d -p 8080:8080 docker_lop_02 f598bbc60d147fa42193c88f1769a518f2555c4b88d5c7f3f109912eeb9815c0
6-测试项目是否正常运行
END