基于Alpine镜像编写Dockerfile

本文详细介绍了如何使用Alpine Linux为基础,通过Dockerfile构建Httpd镜像,并进行本地测试与上传到Docker Hub。首先,展示了Dockerfile的内容,包括安装依赖、编译与安装Httpd,然后创建并运行镜像,最后将镜像推送到Docker Hub。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于Alpine镜像编写Dockerfile

想了解Alpine请移步至👉Alpine Linux详解

项目目录

[root@localhost alpine_httpd]# tree .
.
├── Dockerfile
└── files
    ├── apr-1.6.5.tar.gz
    ├── apr-util-1.6.1.tar.gz
    └── httpd-2.4.54.tar.gz

编写Dockerfile

[root@localhost alpine_httpd]# vim Dockerfile
# This is a Httpd image based on alpine
# Author yefeng


from alpine

LABEL MAINTAINER='yefeng yf021121@163.com'

ENV apr_version=1.6.5 apr_util_version=1.6.1 httpd_version=2.4.54
ENV PATH /usr/local/apache/bin:$PATH

ADD files/* /tmp/

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk update && \
    apk add --no-cache -U gcc libc-dev expat-dev pcre-dev openssl-dev make && \
    cd /tmp && \
    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 && \
    apk del gcc make && \
    rm -rf /var/cache/* && \
    rm -rf /tmp/*

WORKDIR /usr/local/apache

EXPOSE 80

CMD ["httpd","-D","FOREGROUND"]

创建&上传镜像

创建镜像

[root@localhost ~]# docker build -t httpd:v4 .

测试镜像

[root@localhost ~]# docker run -d --name web1 -P httpd:v4
[root@localhost ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>

上传镜像

[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: guguniao
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 tag httpd:v5 guguniao/httpd:v4
[root@localhost ~]# docker push guguniao/httpd:v4
The push refers to repository [docker.io/guguniao/httpd]
6318eddfaf8c: Pushed
d6b343796082: Pushed
8d3ac3489996: Pushed
v4: digest: sha256:32cdfb207bf8c833e26c0d868a071b613f1f86cc5c10b3b8e6cad6322e3f8578 size: 952
[root@localhost ~]# docker logout
Removing login credentials for https://index.docker.io/v1/

部署网站(拓展)

利用上面创建好的镜像运行一个容器,在该容器里部署网站

项目目录

[root@localhost alpine_httpd]# tree
.
├── Dockerfile
└── files
    ├── apr-1.6.5.tar.gz
    ├── apr-util-1.6.1.tar.gz
    ├── chishenme.tar.gz		 #这个是网站的源码包
    └── httpd-2.4.54.tar.gz

编写Dockerfile

[root@localhost alpine_httpd]# cat Dockerfile
# This is a Httpd image based on alpine
# Author yefeng


from alpine

LABEL MAINTAINER='yefeng yf021121@163.com'

ENV apr_version=1.6.5 apr_util_version=1.6.1 httpd_version=2.4.54
ENV PATH /usr/local/apache/bin:$PATH

ADD files/* /tmp/

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk update && \
    apk add --no-cache -U gcc libc-dev expat-dev pcre-dev openssl-dev make && \
    cd /tmp && \
    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 && \
    sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf && \
    rm -f /usr/local/apache/htdocs/index.html && \
    mv /tmp/chishenme/* /usr/local/apache/htdocs/ && \
    apk del gcc make && \
    rm -rf /var/cache/* && \
    rm -rf /tmp/*

WORKDIR /usr/local/apache

EXPOSE 80

CMD ["httpd","-D","FOREGROUND"]

创建镜像

[root@localhost ~]# docker build -t httpd:v4.2 .

测试

[root@localhost ~]# docker run -d --name web1 -P httpd:v4.2

浏览器访问

image-20220831212649934

上传镜像至Docker Hub

[root@localhost ~]# docker tag httpd:v4.2 guguniao/httpd:v4.2
[root@localhost ~]# docker login
[root@localhost ~]# docker push guguniao/httpd:v4.2
[root@localhost ~]# docker logout
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值