Dockerfile

Dockerfile

基本结构

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

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

Docker分为四部分:

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

[En]

Instructions to be executed by default when the container starts

例如:

第一行必须指定基于的基础镜像
FROM centos
维护者信息
LABEL MANTAINER "lvnanhai66 1@36.com"
镜像操作指令
RUN  useradd -r -M -s /sbin/nologin apache
容器启动时默认要执行的指令
CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]

首先,必须指定它所基于的映像的名称,然后是维护者信息。

[En]

At first, the name of the image on which it is based must be specified, followed by maintainer information.

后面则是镜像操作指令,例如RUN指令,RUN指令将对镜像执行跟随的命令。每运行一条RUN指令,镜像添加新的一层,并提交。
最后是CMD指令来指定运行容器时的操作指令。

指令

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

FROM
LABEL MAINTAINER
RUN
CMD
EXPOSE
ENV
ADD
COPY
ENTRYPOINT
VOLUME
USER
WORKDIR
ONBUILD

Dockerfile
FROM
该函数用于指定基本图像,并且必须是第一条指令。
[En]

The function is to specify the base image and must be the first instruction.

如果不以任何镜像为基础,那么写法为:FROM scratch。

这也意味着下一条指令将从镜像的第一层开始。

[En]

It also means that the next instruction will start as the first layer of the mirror image.

FROM centos    //基于centos的镜像
语法:
FROM <image>
FROM <image>:<tag>
FROM <image>:<digest>
&#x4E09;&#x79CD;&#x9009;&#x9879;&#xFF0C;&#x5176;&#x4E2D;<tag>&#x548C;<digest>&#x662F;&#x53EF;&#x9009;&#x9879;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x9009;&#x62E9;&#x90A3;&#x4E48;&#x9ED8;&#x8BA4;&#x5C31;&#x662F;latest
</digest></tag></digest></image></tag></image></image>

LABEL MAINTAINER
指定维护者信息

&#x8BED;&#x6CD5;:LABEL MAINTAINER <name email_address>
LABEL MANTAINER "lvnanhai66 1@36.com"
</name>

RUN
功能为运行指定的命令
RUN命令有两种格式
1.RUN command
2.RUN [“executable”, “param1”, “param2”]
前者将在shell终端中运行命令,即/bin/sh -c;后者则使用exec执行。指定使用其他终端可以通过第二种方式实现,例如:RUN [“/bin/bash”,”-c”,”echo hello”]
每条RUN指令将在当前镜像基础上执行指定命令,并提交为新的镜像。当命令较长时可以使用 \ 来换行

[root@localhost ~]# cd httpd/
[root@localhost httpd]# ls
Dockerfile  files
[root@localhost httpd]# vim Dockerfile
[root@localhost httpd]# cat Dockerfile
FROM busybox

LABEL MANTAINER "lvnanhai66 1@36.com"

RUN echo "hello lnh" > /tmp/abc
[root@localhost httpd]#  podman build -t httpd:1.0 .
STEP 1/3: FROM busybox
STEP 2/3: LABEL MANTAINER "lvnanhai66 1@36.com"
--> d761206551e
STEP 3/3: RUN echo "hello lnh" > /tmp/abc
COMMIT httpd:1.0
--> 80125d342c8
Successfully tagged localhost/httpd:1.0
80125d342c8d86708b7c9a572ea46876a361a2c6a6b21c6b4ad66c4c00dd0b37
[root@localhost httpd]# podman run -it --rm httpd:1.0 /bin/sh
/ # cd /tmp/
/tmp # ls
abc

CMD

CMD支持三种格式:
1.CMD [“executable”,”param1″,”param2″]使用exec执行,推荐方式
2.CMD command param1 param2在/bin/sh中执行,提供给需要交互的应用
3.CMD [“param1″,”param2”]提供给ENTRYPOINT的默认参数
CMD用于指定启动容器时默认要执行的命令,每个Dockerfile只能有一条CMD命令。如果指定了多条命令,只有最后一条会被执行。

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

不要把RUN和CMD搞混了。

RUN是构件容器时就运行的命令以及提交运行结果

CMD是容器启动时执行的命令,在构件时并不运行,构件时紧紧指定了这个命令到底是个什么样子

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

EXPOSE

格式为EXPOSE port [port…]。
例如:

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

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

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

ENV PATH /usr/local/apache/bin:$PATH   //&#x914D;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;

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

[root@localhost httpd]# ls
Dockerfile  files
[root@localhost httpd]# ls files/
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  entrypoint.sh  httpd-2.4.54.tar.gz
[root@localhost httpd]# vim Dockerfile
[root@localhost httpd]# cat Dockerfile
FROM busybox

ADD files/apr-1.7.0.tar.gz /tmp/
[root@localhost httpd]#  podman build -t httpd:2.0 .
STEP 1/2: FROM busybox
STEP 2/2: ADD files/apr-1.7.0.tar.gz /tmp/
COMMIT httpd:2.0
--> 5a9f6d599dc
Successfully tagged localhost/httpd:2.0
5a9f6d599dc3245e3bfba5877bd84f155d9410e1fff6a679a8ebab954f530f1a
[root@localhost httpd]#  podman run -it --rm  httpd:2.0 /bin/sh
/ # cd tmp/
/tmp # ls
apr-1.7.0
/tmp #

COPY

&#x683C;&#x5F0F;&#x4E3A;COPY <src> <dest>&#x3002;

&#x590D;&#x5236;&#x672C;&#x5730;&#x4E3B;&#x673A;&#x7684;<src>&#xFF08;&#x4E3A;Dockerfile&#x6240;&#x5728;&#x76EE;&#x5F55;&#x7684;&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;&#xFF0C;&#x6587;&#x4EF6;&#x6216;&#x76EE;&#x5F55;&#xFF09;&#x4E3A;&#x5BB9;&#x5668;&#x4E2D;&#x7684;<dest>&#x3002;&#x76EE;&#x6807;&#x8DEF;&#x5F84;&#x4E0D;&#x5B58;&#x5728;&#x65F6;&#x4F1A;&#x81EA;&#x52A8;&#x521B;&#x5EFA;&#x3002;
&#x5F53;&#x4F7F;&#x7528;&#x672C;&#x5730;&#x76EE;&#x5F55;&#x4E3A;&#x6E90;&#x76EE;&#x5F55;&#x65F6;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528;COPY&#x3002;
</dest></src></dest></src>

ENTRYPOINT
ENTRYPOINT有两种格式:

ENTRYPOINT [“executable”,”param1″,”param2″]
ENTRYPOINT command param1 param2(在shell中执行)
配置容器启动后执行的命令,并且不可被docker run提供的参数覆盖。而且,如果在docker run的后面提供了参数,这些命令行参数会被当作参数传递给ENTRYPOINT指定的程序。

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

&#x4F8B;&#x5982;:ENTRYPOINT ["/bin/bash","/entrypoint.sh"]

VOLUME

格式为VOLUME [“/data”]。

创建可从本地主机或其他容器装载的装载点,该装载点通常用于存储数据库、要维护的数据等。

[En]

Create a mount point that can be mounted from a local host or other container, which is generally used to store databases, data to be maintained, etc.

USER
格式为USER daemon。

指定运行容器时的用户名或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 desired users before, for example:

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

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

WORKDIR
语法:

WORKDIR /path/to/workdir

设置工作目录,对RUN,CMD,ENTRYPOINT,COPY,ADD生效。如果不存在则会创建,也可以设置多次。
如:

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd

pwd执行的结果是/a/b/c

WORKDIR也可以解析环境变量

如:

ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
RUN pwd

pwd的执行结果是/path/$DIRNAME

ONBUILD
格式为ONBUILD [INSTRUCTION]。

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

[En]

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

例如,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。

创建镜像(centos版)

用podman进行dockefile做一个httpd编译安装的镜像,要求要控制版本号,要用到dockerfile中大部分指令及用脚本方式启动程序,上传到官方镜像仓库

[root@localhost ~]# mkdir httpd
[root@localhost ~]# cd httpd/
[root@localhost httpd]# touch Dockerfile
[root@localhost httpd]# mkdir files
[root@localhost httpd]# ls
Dockerfile  files
//&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x76EE;&#x5F55;&#x4E0B;&#x9762;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Dockerfile&#x6587;&#x4EF6;&#x548C;&#x5B58;&#x653E;&#x5B89;&#x88C5;&#x5305;&#x7684;&#x76EE;&#x5F55;files
[root@localhost httpd]# cd files/
[root@localhost files]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz https://downloads.apache.org/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
//&#x4E0B;&#x8F7D;&#x5B89;&#x88C5;&#x5305;
[root@localhost files]# vim entrypoint.sh
[root@localhost files]# cat entrypoint.sh
#!/bin/bash

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

exec "$@"
[root@localhost files]# chmod +x entrypoint.sh //&#x8D4B;&#x4E88;&#x811A;&#x672C;&#x6267;&#x884C;&#x6743;&#x9650;
[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      88 Aug 30 16:43 entrypoint.sh
-rw-r--r--. 1 root root 9743277 Jun  8 16:42 httpd-2.4.54.tar.gz
//&#x5199;&#x4E00;&#x4E2A;&#x811A;&#x672C;&#x4F5C;&#x4E3A;&#x542F;&#x52A8;&#x7A0B;&#x5E8F;
[root@localhost files]# cd ..

[root@localhost httpd]# ls
Dockerfile  files
[root@localhost httpd]# vim Dockerfile
[root@localhost httpd]# cat Dockerfile
FROM centos

LABEL MANTAINER "lvnanhai66 1@36.com"
ENV apache_version 2.4.54     //apache&#x7248;&#x672C;&#x53F7;
ENV PATH /usr/local/apache/bin:$PATH   //&#x914D;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;

ADD files/apr-1.7.0.tar.gz /usr/src/
ADD files/apr-util-1.6.1.tar.gz /usr/src/
ADD files/httpd-${apache_version}.tar.gz /usr/src/   //&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x66F4;&#x6362;
ADD files/entrypoint.sh /     //&#x5C06;&#x8FD9;&#x4E2A;&#x811A;&#x672C;&#x653E;&#x5230;&#x6839;&#x4E0B;&#x9762;

RUN  useradd -r -M -s /sbin/nologin apache && \
     cd /etc/yum.repos.d && rm -r * && \
     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo  && \
     sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo  && \
     yum clean all  && yum makecache && \
     yum -y install gcc gcc-c++ make openssl-devel pcre-devel expat-devel libtool && \
     cd /usr/src/apr-1.7.0 && \
     sed -i '/$RM "$cfgfile"/d' configure && \
     ./configure --prefix=/usr/local/apr && \
     make && make install && \
     cd ../apr-util-1.6.1 && \
     ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  && \
     make && make install && \
     cd ../httpd-${apache_version}  && \
     ./configure --prefix=/usr/local/apache \
        --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 && \       //&#x6E05;&#x9664;&#x7F13;&#x5B58;
     yum -y remove gcc gcc-c++ make && \    //&#x8FD9;&#x4E9B;&#x7F16;&#x8BD1;&#x5DE5;&#x5177;&#x53EF;&#x4EE5;&#x6E05;&#x9664;
     rm -rf /tmp/* /usr/src/*    //&#x8FD9;&#x4E2A;&#x4E0B;&#x9762;&#x7684;&#x6587;&#x4EF6;&#x53EF;&#x4EE5;&#x6E05;&#x9664;

EXPOSE 80       //&#x6620;&#x5C04;&#x7684;&#x7AEF;&#x53E3;&#x53F7;
WORKDIR /usr/local/apache    //&#x76F8;&#x5F53;&#x4E8E;cd&#x8FD9;&#x4E2A;&#x76EE;&#x5F55;
CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"] //&#x6B64;&#x5904;&#x52A0;&#x4E86;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;
ENTRYPOINT ["/bin/bash","/entrypoint.sh"]   //&#x4E0D;&#x77E5;&#x9053;&#x6709;&#x6CA1;&#x6709;&#x6743;&#x9650;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x52A0;bin/bash
[root@localhost httpd]# podman build -t httpd:v3.0 .
....

STEP 13/13: ENTRYPOINT ["/bin/bash","/entrypoint.sh"]
COMMIT httpd:v3.0
--> f65d0bb9ee1
Successfully tagged localhost/httpd:v3.0
f65d0bb9ee17efdffdb53dc52fb92188aaa5fa1ea90cc11e2939855ba71f8ec3
&#x8BA9;&#x8FD9;&#x4E2A;&#x955C;&#x50CF;&#x8FD0;&#x884C;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#xFF1A;
[root@localhost httpd]# podman images
REPOSITORY               TAG         IMAGE ID      CREATED             SIZE
localhost/httpd          v3.0        f65d0bb9ee17  About a minute ago  405 MB
docker.io/library/httpd  latest      dabbfbe0c57b  8 months ago        148 MB
quay.io/centos/centos    latest      300e315adb2f  21 months ago       217 MB
[root@localhost httpd]# podman run -d httpd:v3.0
fcf786713d9b55793b997cc98e3571535b1fd4c0553409f3bb18bfd9d51fd234
[root@localhost httpd]# podman ps
CONTAINER ID  IMAGE                 COMMAND               CREATED        STATUS            PORTS       NAMES
fcf786713d9b  localhost/httpd:v3.0  /usr/local/apache...  4 seconds ago  Up 4 seconds ago              xenodochial_morse
[root@localhost httpd]# podman inspect -l |grep -i ipaddr
//&#x8FC7;&#x6EE4;&#x67E5;&#x770B;ip
            "IPAddress": "10.88.0.6",
                    "IPAddress": "10.88.0.6",
[root@localhost httpd]# curl 10.88.0.6    //&#x8BBF;&#x95EE;&#x6210;&#x529F;
<html><body><h1>It works!</h1></body></html>
&#x4E0A;&#x4F20;&#x955C;&#x50CF;&#xFF1A;
[root@localhost httpd]# podman tag httpd:v3.0 docker.io/lvnanhai66/httpd:v3.0
[root@localhost httpd]# podman images
REPOSITORY                  TAG         IMAGE ID      CREATED        SIZE
localhost/httpd             v3.0        f65d0bb9ee17  8 minutes ago  405 MB
docker.io/lvnanhai66/httpd  v3.0        f65d0bb9ee17  8 minutes ago  405 MB
docker.io/library/httpd     latest      dabbfbe0c57b  8 months ago   148 MB
quay.io/centos/centos       latest      300e315adb2f  21 months ago  217 MB
[root@localhost httpd]# podman login docker.io
Username: lvnanhai66
Password:
Login Succeeded!

[root@localhost httpd]# podman push docker.io/lvnanhai66/httpd:v3.0
Getting image source signatures
Copying blob 3f49a2cae693 done
Copying blob 90fc92241475 done
Copying blob 8cfba8022d3f done
Copying blob 34214738da46 done
Copying blob bea1ef732da4 done
Copying blob 2653d992f4ef done
Copying config f65d0bb9ee done
Writing manifest to image destination
Storing signatures

Dockerfile
&#x62C9;&#x53D6;&#x81EA;&#x5DF1;&#x7684;&#x955C;&#x50CF;&#x8FD0;&#x884C;&#x5BB9;&#x5668;&#xFF1A;
[root@localhost ~]# podman run -d --name web -P docker.io/lvnanhai66/httpd:v3.0
Trying to pull docker.io/lvnanhai66/httpd:v3.0...

Getting image source signatures
Copying blob 929704506730 skipped: already exists
Copying blob 26ad906b0de6 done
Copying blob c63ffcca07e0 done
Copying blob 45fdf32689c6 done
Copying blob 0b040dd24d11 done
Copying blob 841f6f1ffedd done
Copying config f65d0bb9ee done
Writing manifest to image destination
Storing signatures
4d770508e2552c6ab554079fccf9e72ed79ed43ffcad5d6564d2cac3a07527d6
[root@localhost ~]# podman ps -a
CONTAINER ID  IMAGE                            COMMAND               CREATED        STATUS            PORTS                  NAMES
4d770508e255  docker.io/lvnanhai66/httpd:v3.0  /usr/local/apache...  8 seconds ago  Up 8 seconds ago  0.0.0.0:40607->80/tcp  web
[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                            COMMAND               CREATED         STATUS             PORTS                  NAMES
4d770508e255  docker.io/lvnanhai66/httpd:v3.0  /usr/local/apache...  19 seconds ago  Up 19 seconds ago  0.0.0.0:40607->80/tcp  web
//&#x5982;&#x679C;&#x53D1;&#x73B0;&#x62C9;&#x53D6;&#x4E0D;&#x4E0B;&#x6765;&#x53EF;&#x4EE5;&#x52A0;&#x52A0;&#x901F;&#x5668;&#x8FDB;&#x884C;&#x6CE8;&#x91CA;&#xFF08;cd /etc/containers/&#x7136;&#x540E;vim registries.conf&#xFF09;

创建镜像(alpine版)

用podman进行dockefile做一个httpd编译安装的镜像,要求要控制版本号,要用到dockerfile中大部分指令及用脚本方式启动程序,上传到官方镜像仓库,基于alpine镜像进行dockerfile,使其大小在70M以内
Alpinel 编译软件注意事项

[root@localhost ~]# mkdir httpd
[root@localhost ~]# cd httpd/
[root@localhost httpd]# touch Dockerfile
[root@localhost httpd]# mkdir files
[root@localhost httpd]# ls
Dockerfile  files
//&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x76EE;&#x5F55;&#x4E0B;&#x9762;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Dockerfile&#x6587;&#x4EF6;&#x548C;&#x5B58;&#x653E;&#x5B89;&#x88C5;&#x5305;&#x7684;&#x76EE;&#x5F55;files
[root@localhost httpd]# cd files/
[root@localhost files]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz https://downloads.apache.org/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
//&#x4E0B;&#x8F7D;&#x5B89;&#x88C5;&#x5305;
[root@localhost files]# vim entrypoint.sh
[root@localhost files]# cat entrypoint.sh
#!/bin/bash

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

exec "$@"
[root@localhost files]# chmod +x entrypoint.sh //&#x8D4B;&#x4E88;&#x811A;&#x672C;&#x6267;&#x884C;&#x6743;&#x9650;
[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      88 Aug 31 22:38 entrypoint.sh
-rw-r--r--. 1 root root 9743277 Jun  8 16:42 httpd-2.4.54.tar.gz
//&#x5199;&#x4E00;&#x4E2A;&#x811A;&#x672C;&#x4F5C;&#x4E3A;&#x542F;&#x52A8;&#x7A0B;&#x5E8F;
[root@localhost files]# cd ..

[root@localhost httpd]# ls
Dockerfile  files
&#x7F16;&#x5199;dockerfile:
[root@localhost httpd]# vim Dockerfile
[root@localhost httpd]# cat  Dockerfile
FROM alpine

LABEL MANTAINER "lvnanhai66 1@36.com"

ENV apr_version=1.7.0 apr_util_version=1.6.1 httpd_version=2.4.54

ADD files/* /tmp/

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk update && \
    adduser -SHs /sbin/nologin apache && \
    apk add --no-cache -U gcc libc-dev make expat-dev pcre-dev openssl-dev libtool && \
    cd /tmp/apr-${apr_version} && \
    sed -i '/$RM "$cfgfile"/d' configure && \
    ./configure --prefix=/usr/local/apr && \
    make && make install && \
    cd /tmp/apr-util-${apr_util_version} && \
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
    make && make install && \
    cd /tmp/httpd-${httpd_version} && \
    ./configure --prefix=/usr/local/apache \
    --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 && \
    mv /tmp/entrypoint.sh / && \
    apk del gcc make && \
    rm -rf /tmp/* /var/cache/*

EXPOSE 80
WORKDIR /usr/local/apache
CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]
ENTRYPOINT ["/bin/sh","/entrypoint.sh"]
[root@localhost httpd]# podman build -t httpd:v4.0 .
....

STEP 9/9: ENTRYPOINT ["/bin/sh","/entrypoint.sh"]
COMMIT httpd:v6.0
--> eb89d86d65e
Successfully tagged localhost/httpd:v6.0
eb89d86d65ef414d288a662331a44740015472d6539b25f95bc8abee8ce8addb
[root@localhost httpd]# podman images  //&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x5230;&#x955C;&#x50CF;&#x5DF2;&#x7ECF;&#x6BD4;&#x8F83;&#x5C0F;&#x4E86;
REPOSITORY                TAG         IMAGE ID      CREATED         SIZE
localhost/httpd           v6.0        eb89d86d65ef  8 seconds ago   120 MB
docker.io/library/alpine  latest      9c6f07244728  3 weeks ago         5.83 MB
[root@localhost httpd]# cd
[root@localhost ~]# mkdir -p /srv/web
[root@localhost ~]# cd /srv/web/
[root@localhost web]# ls
[root@localhost web]# echo "hello lnh" >index.html
[root@localhost web]# cat index.html
hello lnh
[root@localhost web]# cd
[root@localhost ~]# cd httpd/
[root@localhost httpd]# podman images
REPOSITORY                TAG         IMAGE ID      CREATED         SIZE
localhost/httpd           v6.0        eb89d86d65ef  8 seconds ago   120 MB
docker.io/library/alpine  latest      9c6f07244728  3 weeks ago    5.83 MB
[root@localhost httpd]# podman run -d --name web -p 80:80 -v /srv/web/:/usr/local/apache/htdocs/:Z httpd:v6.0   //&#x7528;&#x5236;&#x4F5C;&#x7684;&#x955C;&#x50CF;&#x8FD0;&#x884C;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;
a7d9d8c071e80360e3ad40ff7933337c8d9ecf24ef0fa81c72a2a85e7dec86af
[root@localhost httpd]# podman ps     //&#x67E5;&#x770B;&#x8FDB;&#x7A0B;&#x72B6;&#x6001;
CONTAINER ID  IMAGE                 COMMAND               CREATED        STATUS            PORTS               NAMES
a7d9d8c071e8  localhost/httpd:v6.0  /usr/local/apache...  4 seconds ago  Up 4 seconds ago  0.0.0.0:80->80/tcp  web
[root@localhost httpd]# podman inspect -l |grep -i ipaddr  //&#x67E5;&#x770B;ip&#x5730;&#x5740;
            "IPAddress": "10.88.0.10",
                    "IPAddress": "10.88.0.10",
[root@localhost httpd]# curl 10.88.0.10      //&#x8BBF;&#x95EE;&#x6210;&#x529F;
hello lnh

进行访问:

Dockerfile
&#x4E0A;&#x4F20;&#x955C;&#x50CF;&#xFF1A;
[root@localhost httpd]# podman images
REPOSITORY                TAG         IMAGE ID      CREATED        SIZE
localhost/httpd           v6.0        eb89d86d65ef  9 minutes ago  120 MB
docker.io/library/alpine  latest      9c6f07244728  3 weeks ago    5.83 MB
[root@localhost httpd]# podman tag httpd:v6.0 docker.io/lvnanhai66/httpd:v6.0    //&#x505A;&#x6807;&#x7B7E;&#x4F7F;&#x5176;&#x548C;&#x5B98;&#x65B9;&#x7F51;&#x7AD9;&#x4E0A;&#x9762;&#x7684;&#x4E00;&#x81F4;(&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x81EA;&#x5DF1;&#x7684;&#x7528;&#x6237;&#x540D;)
[root@localhost httpd]# podman images
REPOSITORY                  TAG         IMAGE ID      CREATED         SIZE
localhost/httpd             v6.0        eb89d86d65ef  11 minutes ago  120 MB
docker.io/lvnanhai66/httpd  v6.0        eb89d86d65ef  11 minutes ago  120 MB
docker.io/library/alpine    latest      9c6f07244728  3 weeks ago     5.83 MB
[root@localhost ~]# podman save docker.io/lvnanhai66/httpd:v6.0 -o myhttpd.tar     //&#x5C06;&#x5176;&#x6253;&#x5305;&#x6210;&#x4E3A;&#x6587;&#x4EF6;
Getting image source signatures
Copying blob 994393dc58e7 done
Copying blob ff52d230cd11 done
Copying blob 7395d1756e8f done
Copying config eb89d86d65 done
Writing manifest to image destination
Storing signatures
[root@localhost ~]# ls
anaconda-ks.cfg  httpd  myhttpd.tar
[root@localhost ~]# scp  myhttpd.tar 192.168.222.251:/root
The authenticity of host '192.168.222.251 (192.168.222.251)' can't be established.

ECDSA key fingerprint is SHA256:y11UDaNXs3AnvVUnZQfAim2VHAplF09YOvQp2NemHyk.

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.222.251' (ECDSA) to the list of known hosts.

root@192.168.222.251's password:
myhttpd.tar                                                          100%  114MB  92.2MB/s   00:01
//&#x4F20;&#x7ED9;&#x5B89;&#x88C5;docker&#x7684;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#x4E0A;&#x9762;

docker的安装方法
在安装了docker的虚拟主机上面进行上传镜像

[root@localhost ~]# ls
anaconda-ks.cfg  myhttpd.tar
[root@localhost ~]# docker load -i myhttpd.tar    //&#x5C06;&#x955C;&#x50CF;&#x5BFC;&#x5165;
994393dc58e7: Loading layer  5.827MB/5.827MB
ff52d230cd11: Loading layer  53.77MB/53.77MB
7395d1756e8f: Loading layer  60.25MB/60.25MB
Loaded image: lvnanhai66/httpd:v6.0
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
lvnanhai66/httpd   v6.0      eb89d86d65ef   26 minutes ago   115MB
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.

Username: lvnanhai66
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.

Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]# docker push lvnanhai66/httpd:v6.0
The push refers to repository [docker.io/lvnanhai66/httpd]
7395d1756e8f: Pushed
ff52d230cd11: Pushed
994393dc58e7: Pushed
v6.0: digest: sha256:b01a151e9b89e7cd994c1a6acb207183c9097bd140098bee0b63f0da35a05c02 size: 952

可以查看到上传成功,并且只有31M左右,比较小

Dockerfile

测试:

[root@localhost ~]# docker pull lvnanhai66/httpd:v6.0
//&#x62C9;&#x53D6;&#x81EA;&#x5DF1;&#x5236;&#x4F5C;&#x7684;&#x955C;&#x50CF;
v6.0: Pulling from lvnanhai66/httpd
213ec9aee27d: Pull complete
c220467fa6e0: Pull complete
f3a6cb94f126: Pull complete
Digest: sha256:b01a151e9b89e7cd994c1a6acb207183c9097bd140098bee0b63f0da35a05c02
Status: Downloaded newer image for lvnanhai66/httpd:v6.0
docker.io/lvnanhai66/httpd:v6.0
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
lvnanhai66/httpd   v6.0      eb89d86d65ef   54 minutes ago   115MB
[root@localhost ~]# docker run -d lvnanhai66/httpd:v6.0
b7f7f743e574f1054afb559fada840361d4ca046e9698b8e4ccb9c2124949416
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED         STATUS         PORTS     NAMES
b7f7f743e574   lvnanhai66/httpd:v6.0   "/bin/sh /entrypoint&#x2026;"   5 seconds ago   Up 4 seconds   80/tcp    strange_hofstadter
//&#x5229;&#x7528;&#x81EA;&#x5DF1;&#x5236;&#x4F5C;&#x955C;&#x50CF;&#x6210;&#x529F;&#x8FD0;&#x884C;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;

Original: https://www.cnblogs.com/tushanbu/p/16640875.html
Author: 涂山布
Title: Dockerfile

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

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

(0)

大家都在看

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