Docker 用Dockerfile部署Apache网站

基于Centos编写Apache的Dockerfile

//创建apache服务的dockerfile文件目录
[root@localhost ~]# mkdir httpd/files -p

//上传源码包
[root@localhost ~]# ls httpd/files
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.51.tar.gz

//项目结构
[root@localhost ~]# tree
.
|-- anaconda-ks.cfg
`-- httpd
    |-- Dockerfile
    `-- files
        |-- apr-1.7.0.tar.gz
        |-- apr-util-1.6.1.tar.gz
        |-- httpd-2.4.51.tar.gz
        `-- install.sh

2 directories, 6 files

//添加脚本权限
[root@localhost ~]# touch httpd/files/install.sh 
[root@localhost ~]# chmod +x httpd/files/install.sh 
[root@localhost ~]# ll httpd/files/install.sh 
-rwxr-xr-x. 1 root root 1444 Dec 10 18:37 httpd/files/install.sh

//编写dockerfile
[root@localhost ~]# vim httpd/Dockerfile  
[root@localhost ~]# cat httpd/Dockerfile 
FROM centos  
LABEL MAINTAINER='1314444 123@qq.com'

#定义变量
ENV apr_version 1.7.0  
ENV apr_util_version 1.6.1 
ENV httpd_version 2.4.51 

#环境变量
ENV PATH /usr/local/apache/bin:$PATH    

#把需要安装apache的源码包传输到/usr/src下
COPY files /usr/src/

#运行安装脚本
RUN ["/bin/bash","-c","/usr/src/install.sh"]

#暴露端口号
EXPOSE 80 443

#安装路径
WORKDIR /usr/local/apache

#传递给ENTRYPOINT的参数
CMD ["-D","FOREGROUND"]    

#主程序
ENTRYPOINT ["httpd"]

//脚本内容
[root@localhost ~]# cat httpd/files/install.sh 
rm -rf /etc/yum.repos.d/* 
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F'"' 'NR==5{print $2}' /etc/os-release).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
echo "alias ls='ls --color'" >> ~/.bashrc
source ~/.bashrc
yum -y install openssl-devel pcre-devel pcre  expat-devel libtool gcc gcc-c++ make                         
useradd -r -M -s /sbin/nologin apache 
cd /usr/src/ 
tar xf apr-${apr_version}.tar.gz 
tar xf apr-util-${apr_util_version}.tar.gz 
tar xf httpd-${httpd_version}.tar.gz 
cd apr-${apr_version} 
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr && \
make -j $(nproc)&& \
make install && \
cd ../apr-util-${apr_util_version} && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make -j $(nproc) && \
make install && \
cd ../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 -j $(nproc)&& \
make install && \
sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
yum -y remove gcc gcc-c++ make
rm -rf /usr/src/* /var/cache/*

//制作镜像
[root@localhost ~]# docker build -t httpd:v0.1  httpd
Sending build context to Docker daemon  11.53MB
Step 1/12 : FROM centos
 ---> 5d0da3dc9764
Step 2/12 : LABEL MAINTAINER='1314444 123@qq.com'
 ---> Using cache
 ---> a8b67caa2102
Step 3/12 : ENV apr_version 1.7.0
 ---> Using cache
 ---> 0e34b832cb4f
Step 4/12 : ENV apr_util_version 1.6.1
 ---> Using cache
 ---> afb0ed0d7b75
Step 5/12 : ENV httpd_version 2.4.51
 ---> Using cache
 ---> 5c75f5c66090
Step 6/12 : ENV PATH /usr/local/apache/bin:$PATH
 ---> Using cache
 ---> 42d9a10cd81f
Step 7/12 : COPY files /usr/src/
 ---> Using cache
 ---> a872f04851c7
Step 8/12 : RUN ["/bin/bash","-c","/usr/src/install.sh"]
 ---> Using cache
 ---> 0e66941d63f0
Step 9/12 : EXPOSE 80 443
 ---> Using cache
 ---> e42572634c04
Step 10/12 : WORKDIR /usr/local/apache
 ---> Using cache
 ---> 75e478ea8d96
Step 11/12 : CMD ["-D","FOREGROUND"]
 ---> Using cache
 ---> b647e6a08699
Step 12/12 : ENTRYPOINT ["httpd"]
 ---> Using cache
 ---> 3eee02b930f6
Successfully built 3eee02b930f6
Successfully tagged httpd:v0.1


//查看镜像
[root@localhost ~]# docker images 
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
1314444/httpd   v0.1      0e0d1f4bd106   10 seconds ago   372MB

//基于新镜像创建容器
[root@localhost ~]# docker run  --name httpd02 -dit -p 80:80 1314444/httpd:v0.1
be35164fcf4676117558f96c164980f188246167ee4e70d949e91f391ae71ad2
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS         PORTS                                        NAMES
be35164fcf46   1314444/httpd:v0.1   "httpd -D FOREGROUND"   5 seconds ago   Up 4 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp   httpd02
[root@localhost ~]# docker exec -it httpd02 /bin/bash
[root@be35164fcf46 /]# ss -anlt
State          Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN         0               128                            0.0.0.0:80                          0.0.0.0:*                            

在这里插入图片描述

上传镜像仓库

[root@localhost ~]# 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 ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
1314444/httpd   v0.1      0e0d1f4bd106   4 minutes ago    703MB

[root@localhost ~]# docker push 1314444/httpd:v0.1
The push refers to repository [docker.io/1314444/httpd]
0afddf7e7a57: Pushed 
49d8e5c1af37: Pushed 
74ddd0ec08fa: Layer already exists 
v0.1: digest: sha256:20337cdfd55de2cd1b87f796661535d6c97e6b837c03cb1390efe01a43cca342 size: 954

基于Alpine 编写Apache的Dockerfile(精简版)

//创建apache服务的dockerfile文件目录
[root@localhost ~]# mkdir httpd/files -p

//上传源码包
[root@localhost ~]# ls httpd/files
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.51.tar.gz

//项目结构
[root@localhost ~]# tree
.
|-- anaconda-ks.cfg
`-- httpd
    |-- Dockerfile
    `-- files
        |-- apr-1.7.0.tar.gz
        |-- apr-util-1.6.1.tar.gz
        |-- httpd-2.4.51.tar.gz
        `-- install.sh

2 directories, 6 files

//添加脚本权限
[root@localhost ~]# touch httpd/files/install.sh 
[root@localhost ~]# chmod +x httpd/files/install.sh 
[root@localhost ~]# ll httpd/files/install.sh 
-rwxr-xr-x. 1 root root 1444 Dec 10 18:37 httpd/files/install.sh

//编写dockerfile
[root@localhost ~]# vim httpd/Dockerfile  
[root@localhost ~]# cat httpd/Dockerfile 
FROM alpine 
LABEL MAINTAINER='1314444 123@qq.com'

#定义变量
ENV apr_version 1.7.0  
ENV apr_util_version 1.6.1 
ENV httpd_version 2.4.51 

#环境变量
ENV PATH /usr/local/apache/bin:$PATH    

#把需要安装apache的源码包传输到/tmp/下
COPY files /tmp/

#运行安装脚本(alpine这个系统不支持/bin/bash)
RUN /tmp/install.sh

#暴露端口号
EXPOSE 80 443

#安装路径
WORKDIR /usr/local/apache

#传递给ENTRYPOINT的参数
CMD ["-D","FOREGROUND"]    

#主程序
ENTRYPOINT ["httpd"]

//脚本内容
[root@localhost ~]# cat httpd/files/install.sh 
#!/bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories
apk update
apk add --no-cache -U openssl-dev pcre-dev expat-dev libtool libc-dev gcc make                         
adduser -S -H -s /sbin/nologin apache 
cd /tmp/
tar xf apr-${apr_version}.tar.gz 
tar xf apr-util-${apr_util_version}.tar.gz 
tar xf httpd-${httpd_version}.tar.gz 
cd apr-${apr_version} 
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr && \
make -j $(nproc)&& \
make install && \
cd ../apr-util-${apr_util_version} && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make -j $(nproc) && \
make install && \
cd ../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 -j $(nproc)&& \
make install && \
apk del gcc make
sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
rm -rf /tmp/* /var/cache/*


//制作镜像
[root@localhost ~]# docker build -t httpd:v0.1 httpd
Sending build context to Docker daemon  11.53MB
Step 1/12 : FROM alpine
 ---> c059bfaa849c
Step 2/12 : LABEL MAINTAINER='1314444 123@qq.com'
 ---> Using cache
 ---> b3a3cce99055
Step 3/12 : ENV apr_version 1.7.0
 ---> Using cache
 ---> cfef855a83c0
Step 4/12 : ENV apr_util_version 1.6.1
 ---> Using cache
 ---> 52d47a6c077a
Step 5/12 : ENV httpd_version 2.4.51
 ---> Using cache
 ---> ecf2f6b7cc3f
Step 6/12 : ENV PATH /usr/local/apache/bin:$PATH
 ---> Using cache
 ---> e28697ef72aa
Step 7/12 : COPY files /tmp/
 ---> Using cache
 ---> 9776e1fbebeb
Step 8/12 : RUN /tmp/install.sh
 ---> Using cache
 ---> 9338e6abe92a
Step 9/12 : EXPOSE 80 443
 ---> Using cache
 ---> ffde4f04aa9b
Step 10/12 : WORKDIR /usr/local/apache
 ---> Using cache
 ---> b279cd78347c
Step 11/12 : CMD ["-D","FOREGROUND"]
 ---> Using cache
 ---> 047746aa7a4d
Step 12/12 : ENTRYPOINT ["httpd"]
 ---> Using cache
 ---> 052defd35663
Successfully built 052defd35663
Successfully tagged httpd:v0.1


//查看镜像
[root@localhost ~]# docker images 
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
1314444/httpd   v0.2      052defd35663   4 minutes ago    79.8MB
1314444/httpd   v0.1      0e0d1f4bd106   10 seconds ago   372MB

//基于新镜像创建容器
[root@localhost ~]# docker run  --name httpd03 --rm -d -p 80:80 1314444/httpd:v0.2
3ba38bf8857403914f035f0ef6e24ab2200b369de6e825caf6eb2df02e3aafcb
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                COMMAND                 CREATED         STATUS         PORTS                                        NAMES
3ba38bf88574   1314444/httpd:v0.2   "httpd -D FOREGROUND"   5 seconds ago   Up 4 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp   httpd03

[root@localhost ~]# docker exec -it httpd03 /bin/sh
/usr/local/apache # apk add iproute2
/usr/local/apache # ss -anlt
State          Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN         0               128                            0.0.0.0:80                          0.0.0.0:*                            

在这里插入图片描述

上传镜像仓库

[root@localhost ~]# 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 ~]# docker images 
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
1314444/httpd   v0.2      052defd35663   4 minutes ago    79.8MB
1314444/httpd   v0.1      0e0d1f4bd106   10 seconds ago   372MB

[root@localhost ~]# docker push 1314444/httpd:v0.2
The push refers to repository [docker.io/1314444/httpd]
b50b4eb08a17: Pushed 
90442442cbcf: Pushed 
8d3ac3489996: Mounted from library/alpine 
v0.2: digest: sha256:2854a599479f4531c75d0b2d2232ccf93ba217707b638f392b994974a93312d6 size: 952
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值