dockerfile制作apache镜像

基本结构

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

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

Docker分为四部分:

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

    instructions to be executed by default when the container is started*

FROM

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

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

#&#x8BED;&#x6CD5;&#xFF1A;
FROM <镜像名称>
</镜像名称>

LABEL MAINTAINER

指定维护者信息

#&#x8BED;&#x6CD5;&#xFF1A;
LABEL MAINTAINER "[&#x4F5C;&#x8005;] [&#x90AE;&#x7BB1;]"

ENV

设置环境变量,定义环境变量,然后可以在后续说明中使用该环境变量

[En]

Set the environment variable, define the environment variable, then you can use this environment variable in subsequent instructions

#&#x8BED;&#x6CD5;&#xFF1A;
ENV <key> <value>
ENV <key1>=<value1> <key2>=<value2>...

</value2></key2></value1></key1></value></key>

ARG

构建参数,与 ENV 作用一致。不过作用域不一样。ARG 设置的环境变量仅对 Dockerfile 内有效,也就是说只有 docker build 的过程中有效,构建好的镜像内不存在此环境变量。

#&#x8BED;&#x6CD5;&#xFF1A;
ARG <参数名>[=<默认值>]
</默认值></参数名>

RUN

用于执行后面的命令行命令。有两种格式:

[En]

Used to execute the command line commands that follow. There are two formats:

shell格式

#&#x8BED;&#x6CD5;&#xFF1A;
RUN <命令行的命令>
RUN ["/bin/bash","-c","echo hello"]
</命令行的命令>

exec格式

#&#x8BED;&#x6CD5;&#xFF1A;
RUN ["&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;", "&#x53C2;&#x6570;1", "&#x53C2;&#x6570;2"]
RUN echo "hello world\nhello tom" > /tmp/abc && \
    cat /tmp/abc

CMD

  • CMD 在docker run 时运行。
  • RUN 是在 docker build。
  • 作用:为启动的容器指定默认要运行的程序,程序运行结束,容器也就结束。CMD 指令指定的程序可被 docker run 命令行参数中指定要运行的程序所覆盖。

注意:如果 Dockerfile 中如果存在多个 CMD 指令,仅最后一个生效。

#&#x8BED;&#x6CD5;&#xFF1A;
CMD <shell 命令>
CMD ["<可执行文件或命令>","<param1>","<param2>",...]
CMD ["<param1>","<param2>",...]  # &#x8BE5;&#x5199;&#x6CD5;&#x662F;&#x4E3A; ENTRYPOINT &#x6307;&#x4EE4;&#x6307;&#x5B9A;&#x7684;&#x7A0B;&#x5E8F;&#x63D0;&#x4F9B;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;
</param2></param1></param2></param1></可执行文件或命令></shell>

ENTRYPONT

但是, 如果运行 docker run 时使用了 –entrypoint 选项,将覆盖 ENTRYPOINT 指令指定的程序。

优点:在执行 docker run 的时候可以指定 ENTRYPOINT 运行所需的参数

&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C; Dockerfile &#x4E2D;&#x5982;&#x679C;&#x5B58;&#x5728;&#x591A;&#x4E2A; ENTRYPOINT &#x6307;&#x4EE4;&#xFF0C;&#x4EC5;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x751F;&#x6548;&#x3002;

#&#x8BED;&#x6CD5;&#xFF1A;
ENTRYPOINT <shell 命令>
ENTRYPOINT ["<executeable>","<param1>","<param2>",...]
</param2></param1></executeable></shell>

WORKDIR

WORKDIR : 指定工作目录。用 WORKDIR 指定的工作目录,会在构建镜像的每一层中都存在。(WORKDIR 指定的工作目录,必须是提前创建好的)。
docker build 构建镜像过程中的,每一个 RUN 命令都是新建的一层。只有通过 WORKDIR 创建的目录才会一直存在。

#&#x8BED;&#x6CD5;&#xFF1A;
WORKDIR <工作目录路径>
</工作目录路径>

COPY

COPY指令中,将文件或目录从上下文目录复制到容器以指定路径。

[En]

Copy directive, copy a file or directory from the context directory to the container to specify the path.

#&#x8BED;&#x6CD5;&#xFF1A;
COPY [--chown=<user>:<group>] <源路径1>...  <目标路径>
COPY [--chown=<user>:<group>] ["<源路径1>",...  "<目标路径>"]
</目标路径></源路径1></group></user></目标路径></源路径1></group></user>

ADD

ADD :ADD 指令和 COPY 的使用格类似(同样需求下,官方推荐使用 COPY)。功能也类似,不同之处如下:

ADD 的优点:在执行

#&#x8BED;&#x6CD5;&#xFF1A;
ADD <src> <dest>
</dest></src>

VOLUME

定义匿名数据卷。如果您在启动容器时忘记挂载数据卷,则会自动挂载到匿名卷。

[En]

Define anonymous data volumes. If you forget to mount the data volume when starting the container, it will be automatically mounted to the anonymous volume.

#&#x8BED;&#x6CD5;&#xFF1A;
VOLUME ["<路径1>", "<路径2>"...]
VOLUME <路径>
</路径></路径2></路径1>

EXPOSE

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

#&#x8BED;&#x6CD5;&#xFF1A;
EXPOSE &#x7AEF;&#x53E3;&#x53F7;

USER

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

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

[En]

When the service does not require administrator privileges, you can specify the running user through this command. And you can create the required users before.

#&#x8BED;&#x6CD5;&#xFF1A;
USER <用户名>[:<用户组>]
</用户组></用户名>

ONBUILD

配置将创建的镜像用作其他镜像的基本镜像时执行的操作说明。

[En]

Configure the operation instructions that are performed when the created mirror is used as the base mirror for other mirrors.

#&#x8BED;&#x6CD5;&#xFF1A;
ONBUILD <其它指令>
</其它指令>

使用Dokcerfile制作镜像

[root@localhost ~]# mkdir apache
[root@localhost ~]# cd apache/
[root@localhost apache]# mkdir files
[root@localhost apache]# touch Dockerfile
[root@localhost apache]# ls
Dockerfile  files
[root@localhost apache]# cd files/
[root@localhost files]# wget http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz
[root@localhost files]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
[root@localhost apache]# cd files/
[root@localhost files]# vim entrypoint.sh
#!/bin/bash

sed -i '/^ServerName/s/#//g' /usr/local/apache/conf/httpd/conf

exec "$@"
[root@localhost files]# chmod +x entrypoint.sh
[root@localhost files]# ll
total 11136
-rw-r--r--. 1 root root 1093896 Apr  5  2019 apr-1.7.0.tar.gz
-rw-r--r--. 1 root root  554301 Oct 23  2017 apr-util-1.6.1.tar.gz
-rwxr-xr-x. 1 root root      87 Aug 30 20:51 entrypoint.sh
-rw-r--r--. 1 root root 9743277 Jun  8 16:42 httpd-2.4.54.tar.gz
[root@localhost files]# cd ..

[root@localhost apache]# vim Dockerfile
FROM centos

LABEL MANTAINER "zxr 2428341246@qq.com"

ENV apr_version=1.7.0 apr_util_version=1.6.1 httpd_version=2.4.54

ADD files/* /usr/src/
ADD files/entrypoint.sh /

RUN rm -rf /etc/yum.repos.d/* && \
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo  && \
yum -y install make gcc gcc-c++ openssl-devel pcre-devel expat-devel libtool libxml2-devel && \
    useradd -r -M -s /sbin/nologin apache && \
    cd /usr/src/apr-${apr_version} && \
    sed -i '/$RM "$cfgfile"/d' configure && \
    ./configure --prefix=/usr/local/apr && make && make install && \
    cd /usr/src/apr-util-${apr_util_version} && \
    ./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 && \
    yum clean all && \
    yum -y remove gcc gcc-c++ make && \
    rm -rf /tmp/* /usr/src/*

WORKDIR /usr/local/apache

EXPOSE 80

CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]

ENTRYPOINT ["/bin/bash","/entrypoint.sh"]
[root@localhost ~]# podman build -t xinruizhong/httpd:v1 apache/
[root@localhost ~]# podman images
REPOSITORY                   TAG         IMAGE ID      CREATED            SIZE
docker.io/xinruizhong/httpd  v1          31de2ac7a44b  About an hour ago  417 MB
quay.io/centos/centos        latest      300e315adb2f  21 months ago      217 MB
[root@localhost ~]# podman run -d -p 80:80 xinruizhong/httpd:v1
a3be623f7ba0d7145515538d5be1273b5e22ea838ffd81a1adf8739d576e2cdd
[root@localhost ~]# curl 127.0.0.1:80
<html><body><h1>It works!</h1></body></html>

Original: https://www.cnblogs.com/Their-own/p/16640792.html
Author: 事愿人为
Title: dockerfile制作apache镜像

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/522785/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球