dockerfile编译安装httpd
指令
FROM
格式为 FROM <image> 或 FROM <image>:<tag>
LABEL MAINTAINER
格式为 LABEL MAINTAINER <name email_address> ,指定维护者
信息
RUN
格式为
RUN <command> ,RUN ["/bin/bash","-c","echo hello"]
在shell终端中运⾏命令,即/bin/sh -c
或
RUN ["executable","param1","param2"],RUN echo "hello world\nhello tom" > /tmp/abc && \
cat /tmp/abc
使⽤exec执⾏,每条RUN指令将在当前镜像基础上执⾏指定命令,并提交为新的镜像。当命令较⻓时可以使⽤ \ 来换⾏
CMD
CMD ["executable","param1","param2"] 使⽤exec执⾏,推荐⽅式
CMD command param1 param2 在/bin/sh中执⾏,提供给需要交互的应⽤
CMD ["param1","param2"] 提供给ENTRYPOINT的默认参数
EXPOSE
格式为 EXPOSE <port> [<port>...]
EXPOSE 22 80 8443
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
ENTRYPOINT
ENTRYPOINT command param1 param2(在shell中执⾏)
每个Dockerfile中只能有⼀个ENTRYPOINT
ENTRYPOINT ["/usr/sbin/nginx"]
ADD
ADD <src> <dest>
将复制指定的<src>到容器中的<dest>。
其中<src>可以是Dockerfile所在目录的一个相对路径(文件或目录)
ADD files/* /usr/src/
可以是一个URL
ADD https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz /usr/src/
可以是一个tar文件(会自动解压为目录)
ADD apr-1.7.0.tar.gz /usr/src/
COPY
COPY <src> <dest>
复制本地主机的<src>(为Dockerfile所在目录的相对路径,文件或目录)为容器中的<dest>。目标路径不存在时会自动创建
COPY files /usr/src/
VOLUME
VOLUME ["/data"]
创建一个可以从本地主机或其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据
USER
USER
指定运行容器时的用户名或UID,后续的RUN也会使用指定用户
RUN groupadd -r postgres && useradd -r -g postgres postgres
WORKDIR
指令配置⼯作⽬录
ONBUILD
ONBUILD [INSTRUCTION]
配置当所创建的镜像作为其他镜像的基础镜像时,所执⾏的操作指令
ONBUILD ADD files /usr/src/
目录结构
#现在网上把httpd的三个包下载下来然后创建一个httpd的目录。在httpd的目录下面创建一个files目录放三个安装包。
[root@localhost ~]# mkdir httpd
[root@localhost ~]# cd httpd
[root@localhost httpd]# mkdir files
[root@localhost httpd]# cd
[root@localhost ~]# mv apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.gz httpd/files/
[root@localhost ~]# cd httpd/
[root@localhost httpd]# ls
files
[root@localhost httpd]# ls files/
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.gz
[root@localhost httpd]#
写dockerfile配置文件
[root@localhost httpd]# ls
Dockerfile files
[root@localhost httpd]# vim Dockerfile
[root@localhost httpd]# cat Dockerfile
FROM centos
LABEL MAINTAINER "kang 3102907818@qq.com"
EXPOSE 80 443
ADD files/* /usr/src/
RUN useradd -r -M -s /sbin/nologin apache && \
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 && \
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && \
yum clean all && \
yum -y install openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ && \
cd /usr/src/apr-1.7.0 && \
./configure --prefix=/usr/local/apr && \
make && \
make install && \
cd && \
cd /usr/src/apr-util-1.6.1/ && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && \
make install && \
cd && \
cd /usr/src/httpd-2.4.54/ && \
./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
WORKDIR /usr/local/apache
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/local/apache/bin/httpd"]
[root@localhost httpd]#
运行看有没有问题
[root@localhost httpd]# podman build -t httpd:2002.17 .
[root@localhost httpd]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/httpd 2002.17 51a7685515b5 2 minutes ago 705 MB
[root@localhost httpd]# podman run -d --name web6 -p 8000:80 localhost/httpd:2002.17
b68c55e4b1080db6474b92b55df86ed22406d01a909dddb90c4d30808fbc60b5
[root@localhost httpd]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
上传到官方仓库
[root@localhost httpd]# podman tag httpd:2002.17 nuanchun/httpd:2002.17
[root@localhost httpd]# podman push nuanchun/httpd:2002.17
Getting image source signatures
Copying blob 181c6ff9d4e8 [------------------------------] 8.0b / 51.3MiB
Copying blob 2653d992f4ef [>--------------------------] 8.0MiB / 206.5MiB
Copying blob 2054c8e1e8fd done
^C[root@localhost httpd]# podman push nuanchun/httpd:2002.17
Getting image source signatures
Copying blob 181c6ff9d4e8 done
Copying blob 2653d992f4ef done
Copying blob 2054c8e1e8fd skipped: already exists
Copying config 51a7685515 done
Writing manifest to image destination
Storing signatures
[root@localhost httpd]#