Dockerfile httpd命令及编译安装镜像

指令

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

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

FROM

格式为FROM 或FROM :。

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

LABEL MAINTAINER

格式为LABEL MAINTAINER ,指定维护者信息

RUN

格式为RUN 或RUN [“executable”,“param1”,“param2”]。

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

RUN [“/bin/bash”,“-c”,“echo hello”]

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

RUN echo “hello world\nhello 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 […]。
例如:

EXPOSE 22 80 8443

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

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

ENV

格式为ENV 。指定一个环境变量,会被后续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

1.先创建一个目录在目录里创建一个存放apache相关软件包

[root@192 ~]# mkdir httpd
[root@192 ~]# cd httpd/
[root@192 httpd]# touch Dockerfile
[root@192 ~]# mkdir files

2.给Dockerfile文件添加命令

[root@192 httpd]# vim Dockerfile 
FROM busybox
ADD files /tmp/

3.然后再弄一个镜像

[root@192 httpd]# docker build -t test:0.2 .
Sending build context to Docker daemon   11.4MB
Step 1/2 : FROM busybox
 ---> beae173ccac6
Step 2/2 : ADD files /tmp/
 ---> b3a66ce25a81
Successfully built b3a66ce25a81
Successfully tagged test:0.2
[root@192 httpd]# 

4.进入镜像里查看,可以看到写入进去了

[root@192 httpd]# docker run -it --rm test:0.2 /bin/sh
/ # cd /tmp/
/tmp # ls
apr-1.7.0.tar.gz       apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
/tmp # 

5.这种用法是可以直接下载的,但不会解压

[root@192 httpd]# vim Dockerfile 
FROM busybox
ADD https://downloads.apache.org/apr/apr-iconv-1.2.2.tar.gz /tmp/
[root@192 httpd]# docker build -t test:0.3 .
Sending build context to Docker daemon   11.4MB
Step 1/2 : FROM busybox
 ---> beae173ccac6
Step 2/2 : ADD https://downloads.apache.org/apr/apr-iconv-1.2.2.tar.gz /tmp/
Downloading  1.248MB/1.248MB
 ---> 93f087c81c61
Successfully built 93f087c81c61
Successfully tagged test:0.3
[root@192 httpd]# 
[root@192 httpd]# docker run -it --rm test:0.3 /bin/sh
/ # cd /tmp/
/tmp # ls
apr-iconv-1.2.2.tar.gz
/tmp # 

COPY

复制的也只是下载过去

[root@192 httpd]# vim Dockerfile 
FROM busybox
COPY files/apr-1.7.0.tar.gz /tmp/
[root@192 httpd]# docker build -t test:0.5 .
Sending build context to Docker daemon   11.4MB
Step 1/2 : FROM busybox
 ---> beae173ccac6
Step 2/2 : COPY files/apr-1.7.0.tar.gz /tmp/
 ---> 339cf3fc9a16
Successfully built 339cf3fc9a16
Successfully tagged test:0.5
[root@192 httpd]# 
[root@192 httpd]# docker run -it --rm test:0.5 /bin/sh
Unable to find image 'test:0.5' locally
^C
[root@192 httpd]# docker run -it --rm test:0.5 /bin/sh
/ # ls /tmp/
apr-1.7.0.tar.gz

编译安装镜像

1.先拉取centos,httpd

[root@192 httpd]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
nginx                v1        76f9a850ab32   13 hours ago    170MB
0b8d572d1c7d/httpd   v0.4      6e9d871d0138   24 hours ago    390MB
busybox              latest    beae173ccac6   8 months ago    1.24MB
nginx                latest    605c77e624dd   8 months ago    141MB
httpd                latest    dabbfbe0c57b   8 months ago    144MB
ubuntu               latest    ba6acccedd29   10 months ago   72.8MB
centos               latest    5d0da3dc9764   11 months ago   231MB

2.配置文件,写入编译代码

FROM centos

LABEL MANTAINER "seancheng sean1002@126.com"
ENV apache_version 2.4.54
ENV PATH /usr/local/apache/bin:$PATH

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/

RUN useradd -r -M -s /sbin/nologin apache && \
    cd /etc/yum.repos.d && rm -rf * && \
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 && \
    sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf && \
    yum -y remove gcc gcc-c++ make && \
    rm -rf /var/cache/* && \
    rm -rf /usr/src/*


EXPOSE 80
WORKDIR /usr/local/apache
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/local/apache/bin/httpd"]

3.创建好了就登陆到docker镜像仓库

.....
.....
Removing intermediate container 0a518d191ceb
 ---> b9a8e8b41cc1
Step 12/12 : ENTRYPOINT ["/usr/local/apache/bin/httpd"]
 ---> Running in 8f2c48d05cf2
Removing intermediate container 8f2c48d05cf2
 ---> 34fb6cfb5894
Successfully built 34fb6cfb5894
Successfully tagged httpd:v0.2

[root@192 httpd]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
httpd                v0.2      34fb6cfb5894   18 seconds ago   412MB
httpd                v0.1      49e8654f7a0c   29 minutes ago   707MB
nginx                v1        76f9a850ab32   14 hours ago     170MB
0b8d572d1c7d/httpd   v0.4      6e9d871d0138   25 hours ago     390MB
busybox              latest    beae173ccac6   8 months ago     1.24MB
nginx                latest    605c77e624dd   8 months ago     141MB
httpd                latest    dabbfbe0c57b   8 months ago     144MB
ubuntu               latest    ba6acccedd29   10 months ago    72.8MB
centos               latest    5d0da3dc9764   11 months ago    231MB

[root@192 httpd]# docker login
Authenticating with existing credentials...
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

4.然后修改镜像名为仓库指定登录名,然后上传

[root@192 httpd]# docker tag httpd:v0.2 0b8d572d1c7d/httpd:v0.2
[root@192 httpd]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
0b8d572d1c7d/httpd   v0.2      34fb6cfb5894   5 minutes ago    412MB
httpd                v0.2      34fb6cfb5894   5 minutes ago    412MB
httpd                v0.1      49e8654f7a0c   34 minutes ago   707MB
nginx                v1        76f9a850ab32   14 hours ago     170MB
0b8d572d1c7d/httpd   v0.4      6e9d871d0138   25 hours ago     390MB
busybox              latest    beae173ccac6   8 months ago     1.24MB
nginx                latest    605c77e624dd   8 months ago     141MB
httpd                latest    dabbfbe0c57b   8 months ago     144MB
ubuntu               latest    ba6acccedd29   10 months ago    72.8MB
centos               latest    5d0da3dc9764   11 months ago    231MB
[root@192 httpd]# docker push 0b8d572d1c7d/httpd:v0.2
The push refers to repository [docker.io/0b8d572d1c7d/httpd]
b59fb0d283b5: Pushed 
8e11a6e4c400: Pushed 
3d26f03d66e0: Pushed 
f05be03cbdc4: Pushed 
74ddd0ec08fa: Layer already exists 
v0.2: digest: sha256:527edc8ad9dbac8324db7d8b8a5216126b4a92f1e7654ecb8031fd13ff903dca size: 1373
[root@192 httpd]# 

5.访问一下ip

[root@192 httpd]# curl 172.17.0.6
<html><body><h1>It works!</h1></body></html>

创建配置一个脚本

[root@192 files]# vim entrypoint.sh
#!/bin/bash

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

exec "$@"

配置好了之后在赋予它执行权限

[root@192 files]# chmod +x entrypoint.sh 
[root@192 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      89 Sep  1 01:06 entrypoint.sh
-rw-r--r--. 1 root root 9743277 Jun  8 16:42 httpd-2.4.54.tar.gz
[root@192 files]# 

然后再去刚开始配置的apache服务的文件添加修改一下文件

ADD files/entrypoint.sh /				//添加

CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]
ENTRYPOINT ["/entrypoint.sh"]		//修改

创建一个镜像,并运行查看

[root@192 httpd]# docker build -t httpd:v1 .
过程省略...
Removing intermediate container 8c504976541b
 ---> a222cfe93c9d
Successfully built a222cfe93c9d
Successfully tagged httpd:v1

[root@192 httpd]# docker run -d httpd:v1
d7e2c04c0c8e3784aa96835df3bb3c93aadba612ede046a91ded0cf7a92071e4
[root@192 httpd]# docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED             STATUS             PORTS                                     NAMES
d7e2c04c0c8e   httpd:v1     "/entrypoint.sh /usr…"   7 seconds ago       Up 6 seconds       80/tcp                                    stupefied_perlman
143ea9ba74c8   nginx        "/docker-entrypoint.…"   58 minutes ago      Up 58 minutes      80/tcp                                    web2
a54d236286eb   httpd:v0.2   "/usr/local/apache/b…"   About an hour ago   Up About an hour   0.0.0.0:49153->80/tcp, :::49153->80/tcp   web3
be953c49fa4e   httpd        "httpd-foreground"       3 hours ago         Up 3 hours         80/tcp                                    web
[root@192 httpd]# docker inspect -l |grep -i ipaddr
unknown shorthand flag: 'l' in -l
See 'docker inspect --help'.
[root@192 httpd]# docker inspect be953c49fa4e |grep -i ipaddr
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.5",
                    "IPAddress": "172.17.0.5",
[root@192 httpd]# curl 172.17.0.5
<html><body><h1>It works!</h1></body></html>
[root@192 httpd]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值