赞
踩
- FROM #基础镜像,一切从这里开始构建
- MAINTAINER #镜像是谁写的,姓名+邮箱
- RUN #镜像构建的时候需要运行的命令
- ADD #步骤,tomcat镜像,这个tomcat的压缩包!添加内容
- WORKDIR #镜像的工作目录
- VOLUME #挂载的目录
- EXPOSE #暴露端口配置
- CMD #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
- ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令
- ONBUILD #当构建一个被继承 Dockerfile 这个时候就会运行ONBUILD 的指令
- COPY #类似ADD,将我们文件拷贝到镜像中
- ENV #构建的时候设置环境遍量
Docker Hub 中 99% 镜像都是从这个基础镜像过来的 FROM scratch,然后配置需要的软件和配置来进行构建的
- #创建一个自己的centos
- #编写Dockerfile 文件
- [root@root dockerfile01]# cat mydockerfile
- FROM centos
- MAINTAINER Hang<1754808@qq.com>
-
- ENV MYPATH /usr/local
- WORKDIR $MYPATH
-
- RUN yum -y install vim
- RUN yum -y install net-tools
-
- EXPOSE 80
-
- CMD echo $MYPATH
- CMD echo "----END----"
- CMD /bin/bash
-
-
- #通过这个文件构建镜像
- #docker build -f dockerfile文件路径 -t 镜像名:[tag] .
-
- [root@root dockerfile]# docker build -f mydockerfile -t mycentos1.0 .
- Removing intermediate container 59cd03ecce3f
- ---> 89ba49a34e1e
- Step 7/10 : EXPOSE 80
- ---> Running in a1efee3b0fe2
- Removing intermediate container a1efee3b0fe2
- ---> e58f7bcccc27
- Step 8/10 : CMD echo $MYPATH
- ---> Running in e882afef45eb
- Removing intermediate container e882afef45eb
- ---> 906c0671d05c
- Step 9/10 : CMD echo "----END----"
- ---> Running in f4ecc2caf6af
- Removing intermediate container f4ecc2caf6af
- ---> 6f4bef42e0fc
- Step 10/10 : CMD /bin/bash
- ---> Running in 44f884f709c9
- Removing intermediate container 44f884f709c9
- ---> e6062ccf13fd
- Successfully built e6062ccf13fd
- Successfully tagged mycentos1.0:latest
-
-
- #测试运行
- [root@root dockerfile]# docker run -it mycentos1.0 /bin/bash
- [root@f408b8773605 local]# pwd
- /usr/local
我们可以列出本地镜像的变更历史
- [root@root dockerfile]# docker history mycentos1.0
- IMAGE CREATED CREATED BY SIZE COMMENT
- e6062ccf13fd 6 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
- 6f4bef42e0fc 6 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
- 906c0671d05c 6 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
- e58f7bcccc27 6 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
- 89ba49a34e1e 6 minutes ago /bin/sh -c yum -y install net-tools 23.4MB
- 2ba5aff6da20 6 minutes ago /bin/sh -c yum -y install vim 58.1MB
- e34dc5e2146b 7 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
- bd4017671eca 7 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
- f23b466173fb 7 minutes ago /bin/sh -c #(nop) MAINTAINER root<175480… 0B
- 300e315adb2f 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
- <missing> 6 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
- <missing> 6 weeks ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
CMD 和 ENTRYPOINT 区别
CMD #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令
测试cmd
- # 编写dockerfile文件
- [root@root dockerfile]# vi dockerfile-cmd
- FROM centos
- CMD ["ls","-a"]
-
-
- # 构建镜像
- [root@root dockerfile]# docker build -f dockerfile-cmd -t cmd .
- Sending build context to Docker daemon 3.072kB
- Step 1/2 : FROM centos
- ---> 300e315adb2f
- Step 2/2 : CMD ["ls","-a"]
- ---> Running in 711a369ca8ce
- Removing intermediate container 711a369ca8ce
- ---> 96c1a6a5a855
- Successfully built 96c1a6a5a855
- Successfully tagged cmd:latest
-
-
- # run 运行 发现我们的 ls -a 命令生效
- [root@root dockerfile]# docker run 96c1a6a5a855
- .
- ..
- .dockerenv
- bin
- dev
- etc
- home
- lib
- lib64
- lost+found
- media
- mnt
- opt
- proc
- root
- run
- sbin
- srv
- sys
- tmp
- usr
- var
-
-
- # 想追加一个命令 -l ls -al
- [root@root dockerfile]# docker run 96c1a6a5a855 -l
- docker: Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
-
-
- # cmd 的情况下 -l 替换了 CMD ["ls","-a"] 命令 -l 不是命令 所以报错
测试ENTRYPOINT
- # 编写dockerfile文件
- [root@root dockerfile]# vi dockerfile-entrypoint
-
-
- # 构建镜像
- [root@root dockerfile]# docker build -f dockerfile-entrypoint -t entrypoint .
- Sending build context to Docker daemon 4.096kB
- Step 1/2 : FROM centos
- ---> 300e315adb2f
- Step 2/2 : ENTRYPOINT ["ls","-a"]
- ---> Running in 198ab82ed16c
- Removing intermediate container 198ab82ed16c
- ---> 951f0c5be0f2
- Successfully built 951f0c5be0f2
- Successfully tagged entrypoint:latest
-
-
- # run 运行 发现我们的 ls -a 命令生效
- [root@root dockerfile]# docker run 951f0c5be0f2
- .
- ..
- .dockerenv
- bin
- dev
- etc
- home
- lib
- lib64
- lost+found
- media
- mnt
- opt
- proc
- root
- run
- sbin
- srv
- sys
- tmp
- usr
- var
-
-
- # 想追加一个命令 -l ls -al 发现生效
- [root@root dockerfile]# docker run 951f0c5be0f2 -l
- total 0
- drwxr-xr-x. 1 root root 6 Jan 22 16:08 .
- drwxr-xr-x. 1 root root 6 Jan 22 16:08 ..
- -rwxr-xr-x. 1 root root 0 Jan 22 16:08 .dockerenv
- lrwxrwxrwx. 1 root root 7 Nov 3 15:22 bin -> usr/bin
- drwxr-xr-x. 5 root root 340 Jan 22 16:08 dev
- drwxr-xr-x. 1 root root 66 Jan 22 16:08 etc
- drwxr-xr-x. 2 root root 6 Nov 3 15:22 home
- lrwxrwxrwx. 1 root root 7 Nov 3 15:22 lib -> usr/lib
- lrwxrwxrwx. 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64
- drwx------. 2 root root 6 Dec 4 17:37 lost+found
- drwxr-xr-x. 2 root root 6 Nov 3 15:22 media
- drwxr-xr-x. 2 root root 6 Nov 3 15:22 mnt
- drwxr-xr-x. 2 root root 6 Nov 3 15:22 opt
- dr-xr-xr-x. 240 root root 0 Jan 22 16:08 proc
- dr-xr-x---. 2 root root 162 Dec 4 17:37 root
- drwxr-xr-x. 11 root root 163 Dec 4 17:37 run
- lrwxrwxrwx. 1 root root 8 Nov 3 15:22 sbin -> usr/sbin
- drwxr-xr-x. 2 root root 6 Nov 3 15:22 srv
- dr-xr-xr-x. 13 root root 0 Jan 22 13:11 sys
- drwxrwxrwt. 7 root root 145 Dec 4 17:37 tmp
- drwxr-xr-x. 12 root root 144 Dec 4 17:37 usr
- drwxr-xr-x. 20 root root 262 Dec 4 17:37 var
1、准备镜像文件 tomcat压缩包 、jdk压缩包
apache-tomcat-9.0.22.tar.gz
jdk-8u11-linux-x64.tar.gz
2、编写dockerfile文件 官方命名 Dockerfile ,build会自动寻找这个文件 就不需要 -f 指定了
- [root@root tomcat]# cat Dockerfile
-
- FROM centos
- MAINTAINER Hang<1754808103@qq.com>
-
- COPY readme.txt /usr/local/readme.txt
-
- ADD jdk-8u11-linux-x64.tar.gz /usr/local/
- ADD apache-tomcat-9.0.22.tar.gz /usr/local/
-
- RUN yum -y install vim
-
- ENV MYPATH /usr/local
- WORKDIR $MYPATH
-
- ENV JAVA_HOME /usr/local/jdk1.8.0_11
- ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
- ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
- ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
- ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
-
- EXPOSE 8080
-
- CMD /usr/local/apache-tomcat-9.0.22/startup.sh && tail -F /usr/local/apache-tomcat-9.0.22/bin/logs/catalina.out
3、构建镜像
- # docker build -t diytomcat .
-
- Removing intermediate container 8ae0bf3da639
- ---> 731ad02de5b7
- Step 7/15 : ENV MYPATH /usr/local
- ---> Running in 096e09b9bc21
- Removing intermediate container 096e09b9bc21
- ---> 4af21aee2224
- Step 8/15 : WORKDIR $MYPATH
- ---> Running in 754abbc7213e
- Removing intermediate container 754abbc7213e
- ---> b90b463448c6
- Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_11
- ---> Running in 8d88b767e5ba
- Removing intermediate container 8d88b767e5ba
- ---> 8be538aa441f
- Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
- ---> Running in 2d474aacec81
- Removing intermediate container 2d474aacec81
- ---> d2a196f068ee
- Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
- ---> Running in 57b51e71729c
- Removing intermediate container 57b51e71729c
- ---> 82bc59fffad0
- Step 12/15 : ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
- ---> Running in 132900cd06c0
- Removing intermediate container 132900cd06c0
- ---> 26764aa6f6e6
- Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
- ---> Running in dd532def866f
- Removing intermediate container dd532def866f
- ---> 20a93b435b70
- Step 14/15 : EXPOSE 8080
- ---> Running in 791216126e87
- Removing intermediate container 791216126e87
- ---> 6187b918f77d
- Step 15/15 : CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -F /url/local/apache-tomcat-9.0.22/bin/logs/catalina.out
- ---> Running in 1aee1676f7d3
- Removing intermediate container 1aee1676f7d3
- ---> 0c58fc779b7a
- Successfully built 0c58fc779b7a
- Successfully tagged dirtomcat:latest
启动容器,出现错误
启动镜像,ID出来,可是docker ps 发现没有运行
使用docker logs 容器ID 查看报错内容
出现 /bin/startup.sh:Pemission denied
- 进入容器
-
- #启动容器
- [root@root tomcat]# docker run -it -d dirtomcat /bin/bash
- f6dfac5e79aedf94afb79c9797aebb3f198a0b17a297b94ba72e5a686d7a0741
-
- #查看正在运行的容器
- [root@root tomcat]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- f6dfac5e79ae dirtomcat "/bin/bash" 2 seconds ago Up 1 second 8080/tcp strange_keller
-
- #进入容器
- [root@root tomcat]# docker exec -it f6dfac5e79ae /bin/bash
- [root@f6dfac5e79ae local]# ls
- apache-tomcat-9.0.22 bin etc games include jdk1.8.0_11 lib lib64 libexec readme.txt sbin share src
-
- #使用 startup.sh 命令 发现报错 原因是权限不够
- [root@f6dfac5e79ae local]# startup.sh
- bash: /usr/local/apache-tomcat-9.0.22/bin/startup.sh: Permission denied
-
- #赋权 再启动 发现 catalina.sh 权限不够
- [root@f6dfac5e79ae local]# chmod 777 /usr/local/apache-tomcat-9.0.22/bin/startup.sh
- [root@f6dfac5e79ae local]# startup.sh
- Cannot find /usr/local/apache-tomcat-9.0.22/bin/catalina.sh
- The file is absent or does not have execute permission
- This file is needed to run this program
-
- #继续给 catalina.sh 赋权
- [root@f6dfac5e79ae /]# chmod 777 /usr/local/apache-tomcat-9.0.22/bin/catalina.sh
-
- #再次启动 tomcat 启动成功
- [root@f6dfac5e79ae /]# startup.sh
- Using CATALINA_BASE: /usr/local/apache-tomcat-9.0.22
- Using CATALINA_HOME: /usr/local/apache-tomcat-9.0.22
- Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.22/temp
- Using JRE_HOME: /usr/local/jdk1.8.0_11
- Using CLASSPATH: /usr/local/apache-tomcat-9.0.22/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.22/bin/tomcat-juli.jar
那么我们修改一下dockerfile文件内容
- [root@root tomcat]# cat Dockerfile
- FROM centos
- MAINTAINER root<1754808@qq.com>
-
- COPY readme.txt /usr/local/readme.txt
-
- ADD jdk-8u11-linux-x64.tar.gz /usr/local/
- ADD apache-tomcat-9.0.22.tar.gz /usr/local/
-
- RUN yum -y install vim
- RUN chmod 777 /usr/local/apache-tomcat-9.0.22/bin/startup.sh
- RUN chmod 777 /usr/local/apache-tomcat-9.0.22/bin/catalina.sh
-
- ENV MYPATH /usr/local
- WORKDIR $MYPATH
-
- ENV JAVA_HOME /usr/local/jdk1.8.0_11
- ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
- ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
- ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
- ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
-
- EXPOSE 8080
-
- CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.22/bin/logs/catalina.out
构建镜像
- [root@root tomcat]# docker build -t lh_tomcat .
- Sending build context to Docker daemon 169.9MB
- Step 1/17 : FROM centos
- ---> 300e315adb2f
- Step 2/17 : MAINTAINER root<1754808103@qq.com>
- ---> Using cache
- ---> f23b466173fb
- Step 3/17 : COPY readme.txt /usr/local/readme.txt
- ---> Using cache
- ---> 5dfad04cae31
- Step 4/17 : ADD jdk-8u11-linux-x64.tar.gz /usr/local/
- ---> Using cache
- ---> e15afd0717d9
- Step 5/17 : ADD apache-tomcat-9.0.22.tar.gz /usr/local/
- ---> Using cache
- ---> 8b3b0b260f0c
- Step 6/17 : RUN yum -y install vim
- ---> Using cache
- ---> 731ad02de5b7
- Step 7/17 : RUN chmod 777 /usr/local/apache-tomcat-9.0.22/bin/startup.sh
- ---> Using cache
- ---> e60e5bb8d68d
- Step 8/17 : RUN chmod 777 /usr/local/apache-tomcat-9.0.22/bin/catalina.sh
- ---> Using cache
- ---> 1bdadcf5f05c
- Step 9/17 : ENV MYPATH /usr/local
- ---> Running in dc5f851a95aa
- Removing intermediate container dc5f851a95aa
- ---> 567826b1db9b
- Step 10/17 : WORKDIR $MYPATH
- ---> Running in a1b9fa1f25c8
- Removing intermediate container a1b9fa1f25c8
- ---> f2da44f2566d
- Step 11/17 : ENV JAVA_HOME /usr/local/jdk1.8.0_11
- ---> Running in 1525576adae9
- Removing intermediate container 1525576adae9
- ---> 422a260a9854
- Step 12/17 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
- ---> Running in ff3679d3d0d7
- Removing intermediate container ff3679d3d0d7
- ---> 1f2fb5b405ce
- Step 13/17 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
- ---> Running in c627139c2a0e
- Removing intermediate container c627139c2a0e
- ---> 941804071257
- Step 14/17 : ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
- ---> Running in 201a8d7d454b
- Removing intermediate container 201a8d7d454b
- ---> 6a3edac3ea02
- Step 15/17 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
- ---> Running in 5403ebe0c029
- Removing intermediate container 5403ebe0c029
- ---> 3de5361ec881
- Step 16/17 : EXPOSE 8080
- ---> Running in a1e4f56c1cb5
- Removing intermediate container a1e4f56c1cb5
- ---> 98fd728337df
- Step 17/17 : CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.22/bin/logs/catalina.out
- ---> Running in 3b183e06aecd
- Removing intermediate container 3b183e06aecd
- ---> 9fcc0d5c6a4e
- Successfully built 9fcc0d5c6a4e
- Successfully tagged lh_tomcat:latest
重新启动容器
- #启动
- [root@root tomcat]# docker run -d -p 9100:8080 --name lh_tomcat -v /home/tomcat/test:/usr/local/apache-tomcat-9.0.22/webapps/test -v /home/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.22/logs lh_tomcat
- 57f64d17045fe343a60cbbec368cac9b0a201a181253903828bb8cbb2a73e51d
-
- #查看正在运行的容器,发现正在运行
- [root@root tomcat]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 57f64d17045f lh_tomcat "/bin/sh -c '/usr/lo…" 2 seconds ago Up 2 seconds 0.0.0.0:9100->8080/tcp lh_tomcat
- #### 访问测试
-
- ##### 访问 本机ip:9100
在/home/tomcat/test 下构建两个文件
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
-
- </web-app>
index.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>哥的页面</title>
- </head>
- <body>
- <h1>哥牛逼!</h1>
- </body>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。