Dockerfile
Dockerfile介绍
1、dockerfile是用来构建镜像的文件
1.1编写dockerfile文件
1.2 docker build 创建镜像
1.3 docker run 运行镜像
1.4 docker push 上传镜像
2、执行顺序从上至下
3、#代表注释
4、指令全部是大写
FROM
MAINTAINER
RUN
EXPOSE
ADD
COPY
LABEL
VOLUME
WORKDIR
CMD
ENTRYPOINT
ENV
ARG
CMD与ENTRYPOINT的区别
CMD指令(替换)
$ cat dockerfile-cmd-test
FROM centos:7.8.2003
CMD echo "aaa"
$ docker build -f dockerfile-cmd-test -t cmd-test .
$ docker run -it cmd-test
aaa
$ docker run -it cmd-test ls -al
total 12
drwxr-xr-x. 1 root root 6 Aug 11 05:54 .
drwxr-xr-x. 1 root root 6 Aug 11 05:54 ..
-rwxr-xr-x. 1 root root 0 Aug 11 05:54 .dockerenv
-rw-r--r--. 1 root root 12114 May 4 17:11 anaconda-post.log
lrwxrwxrwx. 1 root root 7 May 4 17:09 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Aug 11 05:54 dev
drwxr-xr-x. 1 root root 66 Aug 11 05:54 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 May 4 17:09 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 4 17:09 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 129 root root 0 Aug 11 05:54 proc
dr-xr-x---. 2 root root 114 May 4 17:11 root
drwxr-xr-x. 11 root root 148 May 4 17:11 run
lrwxrwxrwx. 1 root root 8 May 4 17:09 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Aug 6 08:21 sys
drwxrwxrwt. 7 root root 132 May 4 17:11 tmp
drwxr-xr-x. 13 root root 155 May 4 17:09 usr
drwxr-xr-x. 18 root root 238 May 4 17:10 var
ENTRYPOINT指令(Dockerfile中命令一定会被执行)
$ cat dockerfile-entrypoint-test
FROM centos:7.8.2003
ENTRYPOINT echo "aaa"
$ docker build -f dockerfile-entrypoint-test -t entrypoint-test .
$ docker run -it entrypoint-test
aaa
$ docker run -it entrypoint-test ls -l
aaa
$ cat Dockerfile
FROM centos:7
ENTRYPOINT ["/bin/echo","aaa"]
$ docker build -t test:v1 .
$ docker run test:v1 ls -l
aaa ls -l
CMD与ENTRYPOINT同时存在
$ cat Dockerfile
FROM centos:7
ENTRYPOINT echo "aaa"
CMD echo "bbb"
ENTRYPOINT echo "ccc"
CMD echo "ddd"
CMD echo "eee"
$ docker build -t test:v1 .
$ docker run test:v1
ccc
$ cat dockerfile-entrypoint-test
FROM centos:7.8.2003
ENTRYPOINT ["/bin/echo","hello"]
CMD ["world"]
$ docker build -f dockerfile-entrypoint-test -t entrypoint-test .
$ docker run -it entrypoint-test
hello world
$ docker run -it entrypoint-test test
hello test
总结
1、如果各自单独存在,只会执行最后一个CMD或者ENTRYPOINT。
2、如果同时存在Dockerfile,且都是可执行的命令(即shell模式下),只会执行Dockerfile中最后的ENTRYPOINT,docker run时传递的参数也无效.
原因是shell模式下,任何cmd和run的参数都无法传递到entrypoint中。
3、如果同时存在Dockerfile,但是CMD与ENTRYPOINT为JSON形式,CMD会作为参数传递给ENTRYPOINT。
编写centos镜像
$ cat mydockerfile
FROM centos:7.8.2003
MAINTAINER jss<1255109811@qq.com>
RUN yum -y install net-tools
RUN yum -y install vim
ARG SET_MYPATH=/usr/local
ENV MYPATH $SET_MYPATH
WORKDIR $MYPATH
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end-----"
CMD /bin/bash
有时候我们会发现Dockerfile文件中的引用的文件很少,但是docker build时向Docker daemon中提交的大小有几个G。
原因:docker build . 命令会将Dockerfile所在层级目录下的所有文件打包提交给Docker daemon。
例如:Dockerfile文件在/opt目录下,同时opt目录下还存在其他无用的2G文件,docker build时它会将这无用的文件同样提交给Docker daemon,
所以Dockerfile文件所在的层级下最好不要放无用文件。
$ docker build -f mydockerfile -t mycentos:2.0 .
Sending build context to Docker daemon 2.048G
Step 1/3 : FROM centos:7.8.2003
..........
Successfully built 6d50b9d08626
Successfully tagged mycentos:2.0
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 2.0 6d50b9d08626 5 minutes ago 433MB
$ docker run -it mycentos:2.0
$ pwd
/usr/local
$ echo $MYPATH
/usr/local
$ 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 656 (656.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
编写Tomcat镜像
编写Dockerfile文件
$ ll
总用量 200688
-rw-r--r--. 1 root root 10398269 8月 11 16:15 apache-tomcat-8.5.57.tar.gz
-rw-r--r--. 1 root root 562 8月 11 16:56 Dockerfile
-rw-r--r--. 1 root root 195094741 7月 19 2019 jdk-8u221-linux-x64.tar.gz
-rw-r--r--. 1 root root 22 8月 11 15:24 README.txt
$ cat Dockerfile
FROM centos:7.8.2003
MAINTAINER JSS<1255109811@qq.com>
COPY README.txt /opt/sudytech/
ADD apache-tomcat-8.5.57.tar.gz /opt/sudytech/
ADD jdk-8u221-linux-x64.tar.gz /opt/sudytech/
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 8080
WORKDIR /opt/sudytech/
ENV JAVA_HOME /opt/sudytech/jdk1.8.0_221
ENV CLASSPATH $CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
ENV PATH $JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
CMD /opt/sudytech/apache-tomcat-8.5.57/bin/startup.sh && tail -f /opt/sudytech/apache-tomcat-8.5.57/logs/catalina.out
构建镜像
$ docker build -t mytomcat .
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat latest a727ea78aa9f 6 seconds ago 855MB
运行镜像并外挂文件
$ docker run -d -p 8080:8080 --name tomcat -v /home/tomcattest/webapps/test:/opt/sudytech/apache-tomcat-8.5.57/webapps/test -v /home/tomcattest/logs:/opt/sudytech/apache-tomcat-8.5.57/logs mytomcat
$ pwd
/home/tomcattest/webapps/test
$ ll
总用量 4
-rw-r--r--. 1 root root 319 8月 11 16:44 index.jsp
drwxr-xr-x. 2 root root 21 8月 11 16:50 WEB-INF
$ cd WEB-INF/
$ ll
总用量 4
-rw-r--r--. 1 root root 279 8月 11 16:48 web.xml
$ cat web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
</web-app>
$ cat index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>第一个 JSP 程序</title>
</head>
<body>
Hello World<br>
<%
System.out.println("Hello World!");
%>
</body>
</html>
浏览器访问测试
