基于dockerfile制作apache镜像

准备工作

//dockerfile基本结构
[root@localhost ~]# tree
.
├── anaconda-ks.cfg
└── httpd_dockerfile
    ├── dockerfile
    └── files
        ├── apr-1.7.0.tar.gz
        ├── apr-util-1.6.1.tar.gz
        ├── httpd-2.4.54.tar.gz
        └── install.sh

写dockerfile文件

[root@localhost httpd_dockerfile]# cat dockerfile 
FROM centos

LABEL MAINTAINER "qzl 1@2.com"

EXPOSE 80 443

ENV httpd_edition 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 https://downloads.apache.org/httpd/httpd-${httpd_edition}.tar.gz /usr/src/

ADD files/install.sh /tmp/
RUN /bin/bash /tmp/install.sh    //部署apache的动作,填在脚本,可以使结构清晰,减少使用空间

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

[root@localhost httpd_dockerfile]# cat files/install.sh 
#!/bin/bash

useradd -r -M -s /sbin/nologin apache && \	
#//这里没有删除/etc/yum.repos.d/下的本来的源,是因为sentos的本源,有我要的资源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* && \
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* && \
yum update -y && \
yum clean all && \
dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make && \
cd /usr/src/ && \
tar xf httpd-${httpd_edition}.tar.gz && \
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-${httpd_edition}/ && \
./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 && \
dnf -y remove gcc gcc-c++ make && \
sed -i "/#ServerName www.example.com:80/c ServerName www.example.com:80" /usr/local/apache/conf/httpd.conf && \
rm -rf /var/log/* /var/cashe/* /usr/src/* /tmp/install.sh 

构建镜像

//先停止在运行的容器,在删除测试用的容器
[root@localhost httpd_dockerfile]# docker stop $(docker ps -q)
[root@localhost httpd_dockerfile]# docker rm -f $(docker ps -aq)
//删除前面的测试构建的所有镜像
[root@localhost httpd_dockerfile]# docker rmi $(docker images -q)
//强制删除所有
[root@localhost httpd_dockerfile]# docker rmi -f $(docker images -q)
//登录docker仓库
[root@localhost httpd_dockerfile]# 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
//构建镜像
[root@localhost httpd_dockerfile]# docker build -t httpd_centos/v0.1 .

创建容器测试

//创建容器测试
//--restart always      创建容器重启
[root@localhost files]# echo "hello world" > web/index.html 
[root@localhost files]# docker run -d --name web --restart always -p 80:80 -v /root/httpd_dockerfile/files/web/:/usr/local/apache/htdocs/ httpd_centos/v0.1
e3151ac78b2dc75180cac9c184d72b5e3fa0acaad5798de5a4faa89b6071fa36
//查看容器id
[root@localhost httpd_dockerfile]# docker inspect web2
//访问容器id
[root@localhost files]# curl 172.17.0.3
hello world

访问服务器IP

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值