使用dockerfile制作Apache镜像
树形结构
[root@localhost ~]# tree /apache/
/apache/
├── Dockerfile
├── files
│ ├── apr-1.7.0.tar.gz
│ ├── apr-util-1.6.1.tar.gz
│ └── httpd-2.4.49.tar.gz
└── httpd.conf
1 directory, 5 files
创建存放apache服务Dockerfile文件的目录
[root@localhost ~]# mkdir /apache/
[root@localhost apache]# ls
[root@localhost apache]#
[root@localhost apache]# mkdir files
[root@localhost files]# ls
[root@localhost files]#
上传源码包
[root@localhost files]# ls
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.49.tar.gz
提起准备好修改好的配置文件
[root@localhost ~]# cd /apache/
[root@localhost apache]# ls
Dockerfile files httpd.conf
编写Dockerfile文件
[root@localhost ~]# vim /apache/Dockerfile
FROM centos // 基础镜像
ENV apr_version 1.7.0 // 变量
ENV apr_util_version 1.6.1
ENV httpd_version 2.4.49
LABEL MAINTAINER='syblyw0806 1978974056@qq.com' // 作者信息
ADD files/* /usr/src/ // 上传本机上的apache压缩包,会自动解压
WORKDIR /usr/src/ // 切换当前工作目录
// 执行命令进行安装操作
RUN yum -y install openssl-devel pcre-devel pcre expat-devel libtool gcc gcc-c++ make &&
cd apr-$apr_version && sed -i '/$RM "$cfgfile"/d' configure &&
./configure --prefix=/usr/local/apr && make && make install &&
cd ../apr-util-$apr_util_version &&
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &&
make && 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 && make install
// 修改配置文件
COPY httpd.conf /usr/local/apache/conf/httpd.conf
// 暴露容器端口
EXPOSE 80
// 设置数据卷
VOLUME ["/usr/local/apache/htdocs/"]
// 启动时执行的命令
CMD ["/usr/local/apache/bin/apachectl","-D","FOREGROUND"]
构建镜像
[root@localhost ~]# docker build -t syblyw0806/httpd:latest /apache/
Sending build context to Docker daemon 11.1MB
Step 1/12 : FROM centos
---> 5d0da3dc9764
Step 2/12 : ENV apr_version 1.7.0
---> Running in cfa21b4d11b9
Removing intermediate container cfa21b4d11b9
---> 027a71c94acf
Step 3/12 : ENV apr_util_version 1.6.1
---> Running in fe62d998d0b1
Removing intermediate container fe62d998d0b1
---> c7ec91b65ff3
Step 4/12 : ENV httpd_version 2.4.49
---> Running in 7de224cb9c26
Removing intermediate container 7de224cb9c26
---> 000c6358043c
Step 5/12 : LABEL MAINTAINER='syblyw0806 1978974056@qq.com'
---> Running in 5751f853e8a8
Removing intermediate container 5751f853e8a8
---> 83af228b28ce
Step 6/12 : ADD files/* /usr/src/
---> 4cfb8e0d26cb
Step 7/12 : WORKDIR /usr/src/
---> Running in c223ff25a38c
Removing intermediate container c223ff25a38c
---> 697e04b1c686
Step 8/12 : RUN yum -y install openssl-devel pcre-devel pcre expat-devel libtool gcc gcc-c++ make && cd apr-$apr_version && sed -i '/$RM "$cfgfile"/d' configure && ./configure --prefix=/usr/local/apr && make && make install && cd ../apr-util-$apr_util_version && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && 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 && make install
---> Running in 4d8f139b488f
Removing intermediate container 4d8f139b488f
---> 8b8c6b1195d9
Step 9/12 : COPY httpd.conf /usr/local/apache/conf/httpd.conf
---> a9e19a716a72
Step 10/12 : EXPOSE 80
---> Running in 2200cc385e1e
Removing intermediate container 2200cc385e1e
---> a7792f7c572e
Step 11/12 : VOLUME ["/usr/local/apache/htdocs/"]
---> Running in 29716b1595be
Removing intermediate container 29716b1595be
---> 4d2ff0f02931
Step 12/12 : CMD ["/usr/local/apache/bin/apachectl","-D","FOREGROUND"]
---> Running in f3bb034b8e0d
Removing intermediate container f3bb034b8e0d
---> da89d66d3223
Successfully built da89d66d3223
Successfully tagged syblyw0806/httpd:latest
查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
syblyw0806/httpd latest da89d66d3223 about an hour ago 702MB
busybox latest ffe9d497c324 13 hours ago 1.24MB
nginx latest f652ca386ed1 6 days ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
基于新镜像创建容器
[root@localhost apache]# docker run --name httpd -dit -p 80:80 syblyw0806/httpd:latest
4a95c3a809c26d7cf85ae7b41d158cf37869841c6b1871604f5df07560f983d1
[root@localhost apache]# docker ps
ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a95c3a809c2 syblyw0806/httpd:latest "/usr/local/apache/b…" 7 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp httpd
[root@localhost apache]# docker exec -it httpd /bin/bash
[root@4a95c3a809c2 src]# 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:*
访问浏览器