dockerfile
1.创建dockerfile:
[root@localhost test]# cat Dockerfile
From centos
Run yum install -y vim
[root@localhost test]# docker build -t centos:v1 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : From centos
—> 470671670cac
Step 2/2 : Run yum install -y vim
—> Running in 167df443990a
CentOS-8 - AppStream 2.5 MB/s | 7.0 MB 00:02
CentOS-8 - Base 1.4 MB/s | 2.2 MB 00:01
CentOS-8 - Extras 6.0 kB/s | 5.9 kB 00:00
Dependencies resolved.
Package Arch Version Repository Size
Installing:
vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M
Installing dependencies:
2.镜像的缓存:如果已有镜像层会直接调用缓存不会重新下载
[root@localhost test]# docker build -t centos:v1 /root/test/
Sending build context to Docker daemon 2.048kB
Step 1/3 : From centos
—> 470671670cac
Step 2/3 : Run yum install -y vim
—> Using cache
—> d53304d44d38
Step 3/3 : Run yum -y install net-tools
—> Running in ca6782dac77d
Last metadata expiration check: 0:04:58 ago on Fri May 22 15:18:08 2020.
可以看到直接调用的缓存 如果想重新下载 可以在构建时加上 --no-cache
命令:
1.From
必须是第一条指令 来自那个镜像
格式:
From 镜像名 from 镜像名:标签 from 镜像名:digest
[root@localhost test]# cat Dockerfile
From centos
2.Run
想要执行的命令:
格式:
Run 接命令 Run [“可执行文件”,”参数”…]
[root@localhost test]# cat Dockerfile
From centos
Run yum install -y vim
Run yum -y install net-tools
3.CMD
容器运行时执行的命令 后面不可以加参数
格式:CMD [“可执行文件”,”参数”,”参数”]或者 [“参数”,”参数”]
CMD [“ls”,"/root"] 不可以是单引号 CMD ls /root
实列:
[root@localhost test]# cat Dockerfile
From centos
#Run yum install -y vim
#Run yum -y install net-tools
CMD [“ls”,"/root"]
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
anaconda-ks.cfg anaconda-post.log original-ks.cfg
4.LABEL
为镜像指定标签:
格式:LABEL 键=”值”
[root@localhost test]# cat Dockerfile
From centos
LABEL com.abcdd.zuozhe=“nyh
aaaaaaaaa”
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker inspect d1410 |grep nyh
“LABEL com.abcdd.zuozhe=nyh aaaaaaaaa”
“com.abcdd.zuozhe”: “nyh aaaaaaaaa”,
“com.abcdd.zuozhe”: “nyh aaaaaaaaa”,
5.MAINTAINER
指定作者:
格式 MAINTAINER 作者名
[root@localhost test]# cat Dockerfile
From centos
MAINTAINER nyh
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker inspect bd36 |grep nyh
“MAINTAINER nyh”
“Author”: “nyh”,
6.EXPOSE
把端口映射出去 但是必须在启动时加上-p
格式 EXPOSE 端口号
[root@localhost test]# cat Dockerfile
From httpd
EXPOSE 80
[root@localhost test]# docker build -t httpd-80 /root/test/
[root@localhost test]# docker run -dit -p 82:80 httpd-80
把本机的82端口和虚拟机的80端口结合
7.ENV
设置环境变量:
格式: ENV 一个变量 ENV=多个变量
[root@localhost test]# cat Dockerfile
From centos
ENV test1=“hello”
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
[root@c1cd5d11ae0d /]# echo $test1
hello
8.ADD
把本地文件复制到虚拟机里 可以是url路径 也可以是软件包等等
如果是本地压缩文件 拷贝到虚拟机时会解开
本地文件必须是当前目录的文件和dockerfile一个目录下
格式: ADD 本地文件 目标位置 ADD [“本地”,”目标”]
当一个文件不是本地时 会去下载
[root@localhost test]# cat Dockerfile
From centos
ADD http://nginx.org/download/nginx-1.14.2.tar.gz /tmp
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
[root@4747cf282b83 /]# ls /tmp/
ks-script-_srt3u3c ks-script-gpqu_kuo nginx-1.14.2.tar.gz
9.COPY
用法和ADD差不多 不过COPY只能是本地文件url会报错 本地文件必须是当前目录的文件和dockerfile一个目录下
[root@localhost test]# cat Dockerfile
From centos
COPY http://nginx.org/download/nginx-1.12.2.tar.gz /tmp
URL 会报错
[root@localhost test]# docker build -t centos:v1 /root/test/
Sending build context to Docker daemon 2.048kB
Step 1/2 : From centos
—> 470671670cac
Step 2/2 : COPY http://nginx.org/download/nginx-1.12.2.tar.gz /tmp
COPY failed: source can’t be a URL for COPY
[root@localhost test]# cat Dockerfile
From centos
COPY test.txt /tmp
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
[root@89d3fd98425d /]# ls /tmp/
ks-script-_srt3u3c ks-script-gpqu_kuo test.txt
10.ENTRYPOINT 和cmd差不多
容器启动时的操作:
格式 ENTRYPOINT 命令 ENTRYPOINT [“可执行文件”,”参数”]可以和CMD配合使用cmd传参 但是必须都带中括号
ENTRYPOINT 和CMD 都是一条完整命令谁在最后谁生效,如果CMD不是一条完整命令那么将作为ENTRYPOINT的参数
[root@localhost test]# cat Dockerfile
From centos
ENTRYPOINT ls /
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@localhost test]# docker run -it centos:v1 echo hello 有参数也会执行ENTRYPOINT的 这点和CMD不同
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
CMD传参:
[root@localhost test]# cat Dockerfile
From centos
ENTRYPOINT [“ls”,"/"]
CMD ["/tmp","/root"]
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
/:
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
/root:
anaconda-ks.cfg anaconda-post.log original-ks.cfg
/tmp:
ks-script-_srt3u3c ks-script-gpqu_kuo
CMD的会不生效 被这个参数顶替掉
[root@localhost test]# docker run -it centos:v1 /bin/bash
/bin/bash
/:
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@localhost test]#
11.USER
设置容器的用户 后续操作都用这个用户 所以权限是一个问题
格式:user 名字 user uid
[root@localhost test]# cat Dockerfile
From centos
Run useradd test1
USER test1
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
[test1@8ae76b19b742 /]$
权限的问题: 可以给目录权限
[root@localhost test]# cat Dockerfile
From centos
Run useradd test1
USER test1
Run touch test
touch: cannot touch ‘test’: Permission denied
12.WORKDIR
设置工作目录
格式 WORKDIR 目录 或者 WORKDIR 目录\ 目录 目录 相当于
Test/test1/test2/
[root@localhost test]# cat Dockerfile
From centos
WORKDIR /test1
WORKDIR test2
WORKDIR test3
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
[root@1612b217f650 test3]# pwd
/test1/test2/test3
13.ARG 设置变量 只有在构建时有效 容器里无效:
格式 ARG 参数=默认值 ARG 参数
[root@localhost test]# cat Dockerfile
From centos
ARG test1=“aaaa”
RUN echo $test1
[root@localhost test]# docker build -t centos:v1 /root/test/
Sending build context to Docker daemon 2.048kB
Step 1/3 : From centos
—> 470671670cac
Step 2/3 : ARG test1=“aaaa”
—> Using cache
—> 999f5e05cea8
Step 3/3 : RUN echo $test1
—> Running in fd596efd09d1
aaaa
Removing intermediate container fd596efd09d1
—> 3f1ee2978f41
Successfully built 3f1ee2978f41
Successfully tagged centos:v1
[root@localhost test]# docker run -it centos:v1
[root@dd6240d283c2 /]# echo $test1
[root@dd6240d283c2 /]#
14.ONBUILD:
对当前镜像的子镜像有效:
比如在镜像A中设置,A不会生效 ,B是基于A创建的那么B生效
格式:ONBUILD Run 命令
[root@localhost test]# cat Dockerfile
From centos
ONBUILD Run touch test.txt
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
[root@ea780cc114e9 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@ea780cc114e9 /]# ls /
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@ea780cc114e9 /]# ls /root/
anaconda-ks.cfg anaconda-post.log original-ks.cfg
可以看到 没有那个文件
创建镜像B
[root@localhost test]# cat Dockerfile
From centos:v1
[root@localhost test]# docker build -t centos:v2 /root/test/
[root@localhost test]# docker run -it centos:v2
[root@d62b2474545a /]# ls
bin etc lib lost+found mnt proc run srv test.txt usr
dev home lib64 media opt root sbin sys tmp var
可以看到在镜像B中创建了这个文件
15.STOPSIGNAL用于设置停止容器所要发送的系统调用信号
STOPSIGNAL signal 9或这什么 用kill -l可以查看
16.容器的健康检查
格式:HEALTHCHECK --interval=时间 两次检查时间的间隔 --timeout=时间 健康检查的超时时间 --retries=次数 失败多少次
[root@localhost test]# cat Dockerfile
From centos
HEALTHCHECK --interval=1m
–timeout=30s
–retries=3
CMD curl -f http://localhost/ || exit 1
[root@localhost test]# docker build -t centos:v1 /root/test/
[root@localhost test]# docker run -it centos:v1
[root@002f473053f9 /]#