Dockerfile

Dockerfile 具体解释

  1. Dockerfile是⾯向开发的,以后要发布项⽬,做镜像,就需要编写dockerfile⽂件。

  2. DockerFile: 构建⽂件,定义了⼀切步骤,源代码!!!

  3. DockerImages:在DockerFile 定义了⼀个⽂件之后,Docker build 时会产⽣⼀个Docker镜像,当运⾏

  4. Docker 镜像时,会真正开始提供服务。

  5. Docker容器:容器就是镜像运⾏起来的提供服务器!!

Dockerfile 指令

  1. FROM #基础镜像,⼀切从这⾥开始构建

  2. MAINTAINER #镜像是谁写的,名字+邮箱

  3. RUN #镜像构建的时候被需要运⾏的命令

  4. ADD #步骤,tomcat镜像,这个tomcat压缩包,添加内容

  5. WORKDIR #镜像的挂载⽬录

  6. VOLUME #挂载的⽬录

  7. EXPOST #保留端⼝配置

  8. CMD #指定这个容器启动的时候要运⾏的命令,只有之后⼀个会⽣效,可被替代

  9. ENTRYPOINT #指定这个容器启动的时候要运⾏的命令,可以追加命令

  10. COPY #类似ADD,将我们⽂件拷⻉到镜像中

  11. ENV #构建的时候设置环境变量

CMD ENTRYPOINT和RUN的区别

RUN指令是设置编译镜像时执⾏的脚本和程序,镜像编译完成后,RUN指令的⽣命周期结束

FROM centos:centos7
RUN yum -y install vim

容器启动时,可以通过CMD和ENTRYPOINT设置启动项,其中CMD叫做容器默认启动命令,如果在 docker run命令末尾添command,则会替换镜像中CMD设置的启动程序;ENRTYPOINT叫做⼊⼝程 序,不能被docker run命令末尾的command替换,⽽是将command当作字符串,传递给ENTRYPOINT作为参数。

[root@docker ~]# vim dockerfile
FROM ubuntu
ENTRYPOINT ["ps"]
​
[root@docker ~]# docker build -t test:v1 .
# 启动容器,打印ps的输出
[root@docker ~]# docker run --rm test:v1
   PID TTY          TIME CMD
     1 ?        00:00:00 ps
# 启动容器,打印ps -ef的输出 (后面可以追加参数)
[root@docker ~]# docker run --rm test:v1 -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 03:18 ?        00:00:00 ps -ef

在Dockerfile中,应该⾄少有⼀条CMD或者ENTRYPOINT指令,如果同时定义了CMD和ENTRYPOINT则CMD会作为参数传递给ENTRYPOINT

[root@docker ~]# vim dockerfile
FROM ubuntu
ENTRYPOINT ["ps"]
CMD ["-ef"]
​
[root@docker ~]# docker build -t test:v1 .
# 启动容器,打印ps -ef的输出
[root@docker ~]# docker run --rm test:v1
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 03:18 ?        00:00:00 ps -ef
#CMD的情况先后面不能追加参数
[root@docker ~]# docker run 27e0cb2c627a -l
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled

测试案例

[root@docker ~]# cat dockerfile
FROM centos:centos7
MAINTAINER cool
# 阿里云镜像(如果不加就执行不了yum命令)
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
EXPOSE 80
CMD echo $MYPATH
CMD echo "success---------------ok"
CMD /bin/bash
​
# 命令 docker build -f dockerfile⽂件路径 -t 镜像名:[tag]版本号
[root@docker ~]# docker build -f dockerfile -t hauhua:0.1 .
​
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
hauhua       0.1       27e0cb2c627a   About a minute ago   426MB
centos       centos7   eeb6ee3f44bd   7 months ago         204MB
centos       latest    5d0da3dc9764   7 months ago         231MB
# 启动 huahu:0.1
[root@docker ~]# docker run -it 27e0cb2c627a
[root@095c9d47804f local]# pwd
/usr/local

列出本地镜像的变更历史

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
hauhua       0.1       27e0cb2c627a   4 hours ago    426MB
centos       centos7   eeb6ee3f44bd   7 months ago   204MB
centos       latest    5d0da3dc9764   7 months ago   231MB
​
[root@docker ~]# docker history 27e0cb2c627a
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
27e0cb2c627a   4 hours ago    /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B        
7528e7f4d04a   4 hours ago    /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
39d2eec5d695   4 hours ago    /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
4f589e22e2da   4 hours ago    /bin/sh -c #(nop)  EXPOSE 80                    0B        
9889e6ef8d1d   4 hours ago    /bin/sh -c yum -y install vim && yum install…   222MB     
d294ad2a0a7b   4 hours ago    /bin/sh -c #(nop) WORKDIR /usr/local            0B        
5bccf67e74fb   4 hours ago    /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B        
70fffb9a8239   4 hours ago    /bin/sh -c curl -o /etc/yum.repos.d/CentOS-B…   23kB      
3cfa24a70894   4 hours ago    /bin/sh -c #(nop)  MAINTAINER cool              0B        
eeb6ee3f44bd   7 months ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      7 months ago   /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      7 months ago   /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4…   204MB

案例说明

docker 容器里面是没有网络的连 vim 都没用需要自己下载,登陆后的默认路径/home、安装vim编辑器、安装查看⽹络配置ifconfig。

[root@docker ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       centos7   eeb6ee3f44bd   7 months ago    204MB
[root@docker ~]# docker run -it eeb6ee3f44bd /bin/bash  
[root@5291887924c2 /]# ip a
bash: ip: command not found
[root@5291887924c2 /]# ipconfig
bash: ipconfig: command not found
[root@5291887924c2 /]# pwd
/

构建一个有网络有 vim 的 centos 镜像

[root@docker ~]# cat docker-centos 
FROM centos:centos7
MAINTAINER cool
ENV MYPATH /home
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
RUN yum -y install gcc
EXPOSE 80
CMD echo $MYPATH
CMD echo "----------end--------"
CMD /bin/bash
[root@docker ~]# docker build -f docker-centos  -t centos:7 .
[root@docker ~]# docker  images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
centos       7         ca1b5cf2be4e   About a minute ago   830MB
centos       centos7   eeb6ee3f44bd   7 months ago         204MB
[root@docker ~]# docker run -it centos:7
[root@cf2f33a6c465 home]# pwd
/home
[root@cf2f33a6c465 home]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
[root@cf2f33a6c465 home]# exit   
exit
[root@docker ~]# docker history ca1b5cf2be4e
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
ca1b5cf2be4e   2 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B        
16fdc5d7cf06   2 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
4654c5de9848   2 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
d00455b87168   2 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
41fc48a1d5ba   2 minutes ago   /bin/sh -c yum -y install gcc                   238MB     
66c469e8f862   3 minutes ago   /bin/sh -c yum -y install net-tools             166MB     
e9a37f1ef70d   3 minutes ago   /bin/sh -c yum -y install vim                   221MB     
0fdcd2e54c4f   4 minutes ago   /bin/sh -c #(nop) WORKDIR /home                 0B        
1b124d818ded   4 minutes ago   /bin/sh -c curl -o /etc/yum.repos.d/CentOS-B…   23kB      
0fceb429e542   4 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/home             0B        
c74fd2a9a4df   4 minutes ago   /bin/sh -c #(nop)  MAINTAINER cool              0B        
eeb6ee3f44bd   7 months ago    /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      7 months ago    /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      7 months ago    /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4…   204MB   

定义镜像 Tomcat

[root@docker ~]# mkdir docker-txt/docker-tomcat
[root@docker ~]# touch docker-txt/docker-tomcat/readme.txt
[root@docker ~]# cd docker-txt/docker-tomcat/
​
[root@docker docker-tomcat]# ls
apache-tomcat-9.0.46.tar.gz  dockerfile  jdk-18_linux-x64_bin.tar.gz  readme.txt
​
[root@docker docker-tomcat]# cat dockerfile 
FROM centos:centos7
MAINTAINER cool
COPY readme.txt /root/docker-txt/docker-tomcat/readme.txt
ADD apache-tomcat-9.0.46.tar.gz /usr/local/
ADD jdk-18_linux-x64_bin.tar.gz  /usr/local/
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_261
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.46
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.46
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.46/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.46/bin/logs/catalina.out
​
[root@docker docker-tomcat]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
tomcat       1         394a66e0ef27   11 minutes ago   759MB
<none>       <none>    5812256e582a   35 minutes ago   538MB
centos       centos7   eeb6ee3f44bd   7 months ago     204MB
​
[root@docker docker-tomcat]# docker run -d -p 9090:8080 --name mytomcat -v /home/guardwhy/tomcat/test:/usr/local/apache-tomcat-9.0.46/webapps/test -v /home/guardwhy/tomcat/tomcat9logs/:/usr/local/apache-tomcat-9.0.46/logs --privileged=true tomcat:1
​
[root@docker docker-tomcat]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
e6cf6169d20d   tomcat:1   "/bin/sh -c '/usr/lo…"   4 minutes ago   Up 4 minutes   0.0.0.0:9090->8080/tcp, :::9090->8080/tcp   mytomcat
 

wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.58/bin/apache-tomcat-9.0.58.tar.gz
wget https://mirrors-i.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/linux/OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz

[root@docker ~]# mkdir tomcat
[root@docker ~]# cd tomcat/
[root@docker tomcat]# ls
apache-tomcat-9.0.58.tar.gz  OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz
#readme.txt 文件没什么有就是来测试copy指令的
[root@docker tomcat]# touch readme.txt
[root@docker tomcat]# vi Dockerfile 
FROM centos:7
MAINTAINER cool

COPY readme.txt /root/tomcat/readme.txt

ADD apache-tomcat-9.0.58.tar.gz /usr/local/
ADD OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz  /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk8u322-bo6
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.58
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.58
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.58/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.58/bin/logs/catalina.out
[root@docker tomcat]# docker build -t huatomcat .

 

posted @ 2022-05-11 15:12  蜡笔小新๑  阅读(58)  评论(0编辑  收藏  举报