Dockerfile

Dockerfile


基本结构

Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 快速创建自定义镜像。

Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行。

Docker分为四部分:

  • 基础镜像信息
  • 维护者信息
  • 镜像操作指令
  • 容器启动时默认要执行的指令

例如:

# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: baoziong
# Command format: Instruction [arguments / command] ...

# 第一行必须指定基于的基础镜像
FROM centos

# 维护者信息
LABEL MAINTAINER='baoziong baozi@163.com'

# 镜像操作指令
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "
daemon off;" >> /etc/nginx/nginx.conf

# 容器启动时默认要执行的指令
CMD /usr/sbin/nginx

其中,一开始必须指明所基于的镜像名称,接下来一般会说明维护者信息。
后面则是镜像操作指令,例如RUN指令,RUN指令将对镜像执行跟随的命令。每运行一条RUN指令,镜像添加新的一层,并提交。
最后是CMD指令来指定运行容器时的操作指令。

指令

指令的一般格式为INSTRUCTION arguments,指令包括:

FROM

格式为FROM <image>FROM <imagge>:<tag>

第一条指令必须为FROM指令。并且,如果在同一个Dockerfile中创建多个镜像时,可以使用多个FROM指令(每个镜像一次)。

MAINTAINER

格式为MAINTAINER <name email_address>,指定维护者信息

RUN

格式为RUN <command>RUN ["executable","param1","param2"]

前者将在shell终端中运行命令,即/bin/sh -c;后者则使用exec执行。指定使用其他终端可以通过第二种方式实现,例如:

RUN ["/bin/bash","-c","echo hello"]

每条RUN指令将在当前镜像基础上执行指定命令,并提交为新的镜像。当命令较长时可以使用 来换行,例如:

RUN echo "hello world
hello tom" > /tmp/abc && 
    cat /tmp/abc

CMD

CMD支持三种格式:

  • CMD ["executable","param1","param2"]使用exec执行,推荐方式
  • CMD command param1 param2在/bin/sh中执行,提供给需要交互的应用
  • CMD ["param1","param2"]提供给ENTRYPOINT的默认参数

CMD用于指定启动容器时默认要执行的命令,每个Dockerfile只能有一条CMD命令。如果指定了多条命令,只有最后一条会被执行。

如果用户启动容器时指定了运行的命令,则会覆盖掉CMD指定的命令。

EXPOSE

格式为EXPOSE <port> [<port>...]
例如:

EXPOSE 22 80 8443

EXPOSE用于告诉Docker服务器容器暴露的端口号,供互联系统使用。

在启动容器时通过-P,Docker主机会自动分配一个端口转发到指定的端口;
使用-p则可以具体指定哪个本地端口映射过来。

ENV

格式为ENV <key> <value>。指定一个环境变量,会被后续RUN指令使用,并在容器运行时保持。例如:

ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && ...
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH

ADD

格式为ADD <src> <dest>

该命令将复制指定的到容器中的。其中可以是Dockerfile所在目录的一个相对路径(文件或目录);也可以是一个URL;还可以是一个tar文件(会自动解压为目录)。

COPY

格式为COPY <src> <dest>

复制本地主机的(为Dockerfile所在目录的相对路径,文件或目录)为容器中的。目标路径不存在时会自动创建。
当使用本地目录为源目录时,推荐使用COPY。

ENTRYPOINT

ENTRYPOINT有两种格式:

  • ENTRYPOINT ["executable","param1","param2"]
  • ENTRYPOINT command param1 param2(在shell中执行)

配置容器启动后执行的命令,并且不可被docker run提供的参数覆盖。而且,如果在docker run的后面提供了参数,这些命令行参数会被当作参数传递给ENTRYPOINT指定的程序。

每个Dockerfile中只能有一个ENTRYPOINT,当指定多个ENTRYPOINT时,只有最后一个生效。

VOLUME

格式为VOLUME ["/data"]

创建一个可以从本地主机或其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据等。

USER

格式为USER daemon

指定运行容器时的用户名或UID,后续的RUN也会使用指定用户。

当服务不需要管理员权限时,可以通过该命令指定运行用户。并且可以在之前创建所需要的用户,例如:

RUN groupadd -r postgres && useradd -r -g postgres postgres

要临时获取管理员权限可以使用gosu,而不推荐sudo。如果不指定,容器默认是root运行。

WORKDIR

格式为WORKDIR /path/to/workdir

为后续的RUN、CMD、ENTRYPOINT指令配置工作目录。
可以使用多个WORKDIR指令,后续命令如果参数是相对路径,则会基于之前命令指定的路径。例如:

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd

则最终路径为/a/b/c。

ONBUILD

格式为ONBUILD [INSTRUCTION]

配置当所创建的镜像作为其他镜像的基础镜像时,所执行的操作指令。

例如,Dockerfile使用如下的内容创建了镜像image-A

[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
[...]

此时,如果基于image-A创建新的镜像时,新的Dockerfile中使用FROM image-A指定基础镜像时,会自动执行ONBUILD指令的内容,等价于在后面添加了两条指令。

FROM image-A

# Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src

使用ONBUILD指令的镜像,推荐在标签中注明,例如ruby:1.9-onbuild。

创建镜像

编写完成Dockerfile后,可以通过docker build命令来创建镜像。

基本的格式为docker build [选项] 路径,该命令将读取指定路径下(包括子目录)的Dockerfile,并将该路径下所有内容发送给Docker服务端,由服务端来创建镜像。因此一般建议放置Dockerfile的目录为空目录。

另外,可以通过 .dockerignore 文件(每一行添加一条匹配模式)来让Docker忽略路径下的目录和文件。

要指定镜像的标签信息,可以通过-t选项。

例如,指定Dockerfile所在路径为/tmp/docker_builder/,并且希望生成镜像标签为build_repo/first_image,可以使用下面的命令:

docker build -t build_repo/first_image /tmp/docker_builder/

dockerfile制作nginx镜像(centos)

[root@yqh ~]# mkdir -p nginx/software
[root@yqh ~]# cd nginx/software
[root@yqh software]# wget http://nginx.org/download/nginx-1.19.7.tar.gz
[root@yqh software]# ls
nginx-1.19.7.tar.gz
[root@yqh software]# cd ..
[root@yqh nginx]# vim Dockerfile
FROM centos:latest
  
LABEL MAINTAINER "baoziong qinghao_yu@163.com"

ENV version 1.19.7
ADD software/nginx-${version}.tar.gz /usr/src
EXPOSE 80 443

RUN useradd -r -M -s /sbin/nologin nginx && 
    yum -y groups mark install 'Development Tools' && 
    yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make && 
    mkdir -p /var/log/nginx && 
    chown -R nginx.nginx /var/log/nginx && 
    cd /usr/src/nginx-${version} && 
    ./configure 
    --prefix=/usr/local/nginx 
    --user=nginx 
    --group=nginx 
    --with-debug 
    --with-http_ssl_module 
    --with-http_realip_module 
    --with-http_image_filter_module 
    --with-http_gunzip_module 
    --with-http_gzip_static_module 
    --with-http_stub_status_module 
    --http-log-path=/var/log/nginx/access.log 
    --error-log-path=/var/log/nginx/error.log && 
    make && make install && 
    rm -rf /var/cache/* /usr/src/*

WORKDIR /usr/local/nginx

CMD ["-g","daemon off;"]

ENTRYPOINT ["/usr/local/nginx/sbin/nginx"]

[root@yqh nginx]# cd
[root@yqh ~]# docker build -t nginx:v0.1 nginx/
Successfully built a7ff9bbf4t27
Successfully tagged nginx:v0.1
[root@yqh ~]# docker run -itd --rm --name yqh1 -p 80:80 nginx:v0.1
[root@yqh ~]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                        *:2375                    *:*       
LISTEN    0          128                     [::]:22                   [::]:* 
[root@yqh ~]# docker ps -a
CONTAINER ID   IMAGE        COMMAND                  CREATED         STATUS         PORTS                         NAMES
bbdcc04135b2   nginx:v0.1   "/usr/local/nginx/sb…"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, 443/tcp   yqh1
[root@yqh ~]# curl 192.168.10.2
<title>Welcome to nginx!</title>

dockerfile制作httpd镜像(centos)

[root@yqh ~]# mkdir -p httpd/software
[root@yqh ~]# cd httpd/software/
[root@yqh software]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
[root@yqh software]# wget https://mirrors.bfsu.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
[root@yqh software]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.gz
[root@yqh software]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.gz
[root@yqh software]# cd ..
[root@yqh httpd]# vim Dockerfile
FROM centos:latest

LABEL MAINTAINER "baoziong qinghao_yu@163.com"

ENV httpd_version 2.4.46

ADD software/apr-1.7.0.tar.gz /usr/src
ADD software/apr-util-1.6.1.tar.gz /usr/src
ADD software/httpd-${httpd_version}.tar.gz /usr/src

EXPOSE 80 443

RUN useradd -r -M -s /sbin/nologin apache && 
    yum -y groups mark install "Development Tools" && 
    yum -y install make gcc gcc-c++ openssl-devel pcre-devel expat-devel libtool libxml2-devel && 
    cd /usr/src/apr-1.7.0 && 
    sed -i '/$RM "$cfgfile"/d' configure && 
    ./configure --prefix=/usr/local/apr && 
    make && make install && 
    cd /usr/src/apr-util-1.6.1 && 
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && 
    make && make install && 
    cd /usr/src/httpd-${httpd_version} && 
    ./configure --prefix=/usr/local/apache 
    --sysconfdir=/etc/httpd24 
    --enable-so 
    --enable-ssl 
    --enable-cgi 
    --enable-rewrite 
    --with-zlib 
    --with-pcre 
    --with-apr=/usr/local/apr 
    --with-apr-util=/usr/local/apr-util/ 
    --enable-modules=most 
    --enable-mpms-shared=all 
    --with-mpm=prefork && 
    make && make install && 
    sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf && 
    rm -rf /var/cache/* /usr/src/*
 
WORKDIR /usr/local/apache

CMD ["-X"]

ENTRYPOINT ["/usr/local/apache/bin/apachectl"]

[root@yqh ~]# docker build -t httpd:v0.1 httpd/
Successfully built e8cc9bbf5c28
Successfully tagged httpd:v0.1
[root@yqh ~]# docker run -itd --rm --name yqh2 -p 80:80 httpd:v0.1
[root@yqh ~]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                        *:2375                    *:*       
LISTEN    0          128                     [::]:22                   [::]:*       
[root@yqh ~]# docker ps -a
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                         NAMES
c081111cbe6d   httpd:v0.1   "/usr/local/apache/b…"   17 seconds ago   Up 16 seconds   0.0.0.0:80->80/tcp, 443/tcp   yqh1
[root@yqh ~]# curl 192.168.10.2
<html><body><h1>It works!</h1></body></html>

dockerfile制作nginx镜像(alpine)

[root@yqh ~]# mkdir -p nginx-small/software
[root@yqh ~]# cd nginx-small/software
[root@yqh software]# wget http://nginx.org/download/nginx-1.19.7.tar.gz
[root@yqh software]# ls
nginx-1.19.7.tar.gz
[root@yqh software]# cd ..
[root@yqh nginx-small]# vim Dockerfile
FROM alpine:latest
  
LABEL MAINTAINER "baoziong qinghao_yu@163.com"

ENV version 1.19.7
ENV PATH /usr/local/nginx/sbin:$PATH
ADD software/nginx-${version}.tar.gz /usr/src
EXPOSE 80 443

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && 
    adduser -S -H -s /sbin/nologin nginx && 
    apk add --no-cache -U pcre-dev openssl openssl-dev gd-dev g++ zlib-dev make && 
    mkdir -p /var/log/nginx && 
    chown -R nginx /var/log/nginx && 
    cd /usr/src/nginx-${version} && 
    ./configure 
    --prefix=/usr/local/nginx 
    --user=nginx 
    --group=nginx 
    --with-debug 
    --with-http_ssl_module 
    --with-http_realip_module 
    --with-http_image_filter_module 
    --with-http_gunzip_module 
    --with-http_gzip_static_module 
    --with-http_stub_status_module 
    --http-log-path=/var/log/nginx/access.log 
    --error-log-path=/var/log/nginx/error.log && 
    make && make install && 
    sed -i '/nobody/s/#//g' /usr/local/nginx/conf/nginx.conf && 
    rm -rf /var/cache/* /usr/src/*

WORKDIR /usr/local/nginx

CMD ["-g","daemon off;"]

ENTRYPOINT ["/usr/local/nginx/sbin/nginx"]

[root@yqh ~]# docker build -t nginx-s:v0.1 nginx-small/
Successfully built 3591e03ba021
Successfully tagged nginx-s:v0.1
[root@yqh ~]# docker run -itd --rm --name yqh3 -p 80:80 nginx-s:v0.1
[root@yqh ~]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                        *:2375                    *:*       
LISTEN    0          128                     [::]:22                   [::]:*  
[root@yqh ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                         NAMES
677f23e0699c   nginx-s:v0.1   "/usr/local/nginx/sb…"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, 443/tcp   yqh3
[root@yqh ~]# curl 192.168.10.2
<title>Welcome to nginx!</title>

dockerfile制作httpd镜像(alpine)

[root@yqh ~]# mkdir -p httpd-small/software
[root@yqh ~]# cd httpd-small/software
[root@yqh software]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
[root@yqh software]# wget https://mirrors.bfsu.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
[root@yqh software]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.gz
[root@yqh software]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.gz
[root@yqh software]# cd ..
[root@yqh httpd-small]# vim Dockerfile
FROM alpine:latest

LABEL MAINTAINER "baoziong qinghao_yu@163.com"

ENV httpd_version 2.4.46

ADD software/apr-1.7.0.tar.gz /usr/src
ADD software/apr-util-1.6.1.tar.gz /usr/src
ADD software/httpd-${httpd_version}.tar.gz /usr/src

EXPOSE 80 443

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && 
    adduser -S -H -s /sbin/nologin apache && 
    apk add --no-cache -U make g++ pcre-dev openssl-dev expat-dev libtool libxml2-dev && 
    cd /usr/src/apr-1.7.0 && 
    sed -i '/$RM "$cfgfile"/d' configure && 
    ./configure --prefix=/usr/local/apr && 
    make && make install && 
    cd /usr/src/apr-util-1.6.1 && 
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && 
    make && make install && 
    cd /usr/src/httpd-${httpd_version} && 
    ./configure --prefix=/usr/local/apache 
    --sysconfdir=/etc/httpd24 
    --enable-so 
    --enable-ssl 
    --enable-cgi 
    --enable-rewrite 
    --with-zlib 
    --with-pcre 
    --with-apr=/usr/local/apr 
    --with-apr-util=/usr/local/apr-util/ 
    --enable-modules=most 
    --enable-mpms-shared=all 
    --with-mpm=prefork && 
    make && make install && 
    sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf && 
    rm -rf /var/cache/* /usr/src/*

WORKDIR /usr/local/apache

CMD ["-X"]

ENTRYPOINT ["/usr/local/apache/bin/apachectl"]

[root@yqh ~]# docker build -t httpd-s:v0.1 httpd-small/
Successfully built d7aa79ea32cb
Successfully tagged httpd-s:v0.1
[root@yqh ~]# docker run -itd --rm --name yqh4 -p 80:80 httpd-s:v0.1
[root@yqh ~]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                        *:2375                    *:*       
LISTEN    0          128                     [::]:22                   [::]:*       
[root@yqh ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS                         NAMES
99dc1d35c7e9   httpd-s:v0.1   "/usr/local/apache/b…"   39 seconds ago   Up 38 seconds               0.0.0.0:80->80/tcp, 443/tcp   yqh4
[root@yqh ~]# curl 192.168.10.2
<html><body><h1>It works!</h1></body></html>
原文地址:https://www.cnblogs.com/yuqinghao/p/14486403.html