docker-dockerfile-docker镜像制作-基于dockerfile创建(主要方式)
1.创建目录docker
[root@myhost docker]# mkdir /docker
2.接着在docker目录下创建文件夹tomcat
[root@myhost docker]# pwd /docker [root@myhost docker]# mkdir tomcat
3.上传jdk和tomcat安装包到/docker/tomcat目录下
apache-tomcat-8.5.93.tar.gz zulu8.72.0.17-ca-jdk8.0.382-linux_x64.tar.gz
4.编写Dockerfile
[root@myhost tomcat]# cat Dockerfile FROM centos:7 MAINTAINER [liangbosheng] ADD zulu8.72.0.17-ca-jdk8.0.382-linux_x64.tar.gz /usr/local ADD apache-tomcat-8.5.93.tar.gz /usr/local WORKDIR /usr/local RUN mv apache-tomcat-8.5.93 tomcat RUN mv zulu8.72.0.17-ca-jdk8.0.382-linux_x64 java ENV JAVA_HOME /usr/local/java ENV CLASS_PATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar ENV PATH $JAVA_HOME/bin:$PATH EXPOSE 8080 CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
注:
FROM centos:7 基础镜像 centos:7;MAINTAINER 维护者信息;
ADD 复制本地文件到容器内。使用ADD不使用COPY是因为ADD可以使归档文件(例如.tar或.zip)自动解压;
WORKDIR /usr/local 设置工作目录;
RUN mv apache-tomcat-8.5.93 tomcat 将文件夹重新命名;
RUN mv zulu8.72.0.17-ca-jdk8.0.382-linux_x64 java 将文件夹重新命名;
ENV JAVA_HOME /usr/local/java ENV:指定一个环境变量,会被后续 RUN 指令使用,并在容器运行时保持;
ENV CLASS_PATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar ENV:指定一个环境变量,会被后续 RUN 指令使用,并在容器运行时保持;
ENV PATH $JAVA_HOME/bin:$PATH ENV:指定一个环境变量,会被后续 RUN 指令使用,并在容器运行时保持;
EXPOSE 8080 设置tomcat容器暴露的端口;
CMD ["/usr/local/tomcat/bin/catalina.sh","run"] 启动命令
5.创建镜像
[root@myhost tomcat]# docker build -t tomcat:new . Sending build context to Docker daemon 117.6MB Step 1/12 : FROM centos:7 7: Pulling from library/centos Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 Status: Downloaded newer image for centos:7 ---> eeb6ee3f44bd Step 2/12 : MAINTAINER [liangbosheng] ---> Running in 4d373a4e8545 Removing intermediate container 4d373a4e8545 ---> 55a1ad9f34a9 Step 3/12 : ADD zulu8.72.0.17-ca-jdk8.0.382-linux_x64.tar.gz /usr/local ---> 2869f2575f13 Step 4/12 : ADD apache-tomcat-8.5.93.tar.gz /usr/local ---> 09d0ea2f9acc Step 5/12 : WORKDIR /usr/local ---> Running in 6d42a21bc452 Removing intermediate container 6d42a21bc452 ---> d3394c3050b6 Step 6/12 : RUN mv apache-tomcat-8.5.93 tomcat ---> Running in 9e4b802b9943 Removing intermediate container 9e4b802b9943 ---> d3fdd58d126b Step 7/12 : RUN mv zulu8.72.0.17-ca-jdk8.0.382-linux_x64 java ---> Running in 67d3eef87be0 Removing intermediate container 67d3eef87be0 ---> a8330f6d4451 Step 8/12 : ENV JAVA_HOME /usr/local/java ---> Running in 5b6598f9ea4a Removing intermediate container 5b6598f9ea4a ---> 6520d2c5e1f9 Step 9/12 : ENV CLASS_PATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar ---> Running in 49390f9956d7 Removing intermediate container 49390f9956d7 ---> b150e50ee1d8 Step 10/12 : ENV PATH $JAVA_HOME/bin:$PATH ---> Running in 3d87c95adbb7 Removing intermediate container 3d87c95adbb7 ---> 41cdef3edcdb Step 11/12 : EXPOSE 8080 ---> Running in 21e630b2480a Removing intermediate container 21e630b2480a ---> ff5205c1692b Step 12/12 : CMD ["/usr/local/tomcat/bin/catalina.sh","run"] ---> Running in 25ce9a324eec Removing intermediate container 25ce9a324eec ---> fba6d4b00ae5 Successfully built fba6d4b00ae5 Successfully tagged tomcat:new
-f:指定dockerfile文件,不写默认指定当前目录 -t:指定镜像名称 .:指定当前目录环境中的文件
6.查看所创建的镜像
[root@myhost tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat new fba6d4b00ae5 14 seconds ago 658MB
7.创建容器
[root@myhost tomcat]# docker run -itd -p 3333:8080 tomcat:new 4ad7689cb545655131fa6c4b3081e8ee5ce0dd7897d4229af6e80cf3d40db434
8.浏览器访问:IP:3333