11.3. dockerfile使用
11.3.1. 主要命令
1FROM 指明镜像的从那个镜像的基础上。
2ARG: 指明build阶段的变量
3ENV: 指明run阶段的变量
4LABEL: 指明当前指向的一些元数据信息,如果作者信息,镜像生成日期。
5COPY: 复制工作目录的内容到容器中。
6ADD: COPY的加强版本,支持网络资源和自动tar解包
7WORKDIR: 命令的设定工作目录
8VOLUME: 定义卷
9EXPOSE: 用于给容器打开外部通信端口
10RUN: build构建阶段运行的命令
11CMD: run阶段运行的命令,通常只是ENTRYPOINT的参数
12ENTRYPOINT: 程序的默认的运行进入点程序,通常需要容器cmd程序设置些环境准备工作。
13USER: 指定程序运行的用户和用户组
14STOPSIGNAL: 终止信号
15HEALTHCHECK: 健康检查
16SHELL: 指定解释器
17ONBUILD: 在别人FROM你的这个镜像的时候执行
11.3.2. 制作nginx镜像
我们从centos镜像的基础上面制作nginx的镜像
1[root@centos-151 ~]# docker pull centos
2[root@centos-151 ~]# docker image ls
3REPOSITORY TAG IMAGE ID CREATED SIZE
4docker.io/centos latest e934aafc2206 11 hours ago 199 MB
5
6[root@centos-151 ~]# mkdir images
7[root@centos-151 ~]# cd images/
8[root@centos-151 images]# mkdir nginx
9[root@centos-151 images]# cd nginx/
10[root@centos-151 nginx]# ls
11[root@centos-151 nginx]# pwd
12/root/images/nginx
13
14[root@centos-151 nginx]# cat Dockerfile
15######################################################################
16#name: zzjlogin
17#created: 2018-04-07
18#github: https//github.com/zzjlogin/
19######################################################################
20FROM centos:latest
21
22LABEL maintainer="LinuxPanda <zzjlogin@outlook.com>"
23ADD http://download2.linuxpanda.tech/yum/epel-7.repo /etc/yum.repos.d/epel-7.repo
24ADD http://download2.linuxpanda.tech/yum/ali-7.repo /etc/yum.repos.d/ali-7.repo
25
26RUN mkdir "/etc/yum.repos.d/bak" \
27 && mv /etc/yum.repos.d/Cent*.repo /etc/yum.repos.d/bak/ \
28 && yum clean all \
29# && rm -rf /var/cache/yum \
30 && yum install nginx -y -q \
31 && echo "daemon off;" >> /etc/nginx/nginx.conf
32
33#VOLUME [ "/var/www/html"]
34ADD entrypoint.sh /bin/enterpoint.sh
35ENTRYPOINT [ "/bin/enterpoint.sh" ]
36CMD ["nginx"]
37
38EXPOSE 80
39#EXPOSE 443/tcp
40
41HEALTHCHECK --interval=10s --timeout=20s --retries=3 CMD kill -0 nginx && exit 0 || exit 1
42
43[root@centos-151 nginx]# cat entrypoint.sh
44#!/bin/bash
45
46PORT=${PORT:-80}
47sed -r -i "s@(.*listen.*)80(.*)@\1${PORT}\2@g" /etc/nginx/nginx.conf
48exec "$@"
49
50[root@centos-151 nginx]# docker build -t nginx:v2 .
51[root@centos-151 nginx]# docker image ls
52REPOSITORY TAG IMAGE ID CREATED SIZE
53nginx v2 6d458ae074a1 10 seconds ago 388 MB
54docker.io/centos latest e934aafc2206 15 hours ago 199 MB
55[root@centos-151 nginx]# docker run -d -P --name nginxv2-01 nginx:v2
5681a995340911b5776429144fbd5ea94f335c2b6b01a3a9610f7114d601cd5a25
57[root@centos-151 nginx]# docker inspect nginxv2-01 |grep -i ipa
58 "SecondaryIPAddresses": null,
59 "IPAddress": "172.17.0.2",
60 "IPAMConfig": null,
61 "IPAddress": "172.17.0.2",
62[root@centos-151 nginx]# curl 172.17.0.2