//创建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
2 directories, 5 files
//编写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下,传输过程中会自动解压
ADD files/* /usr/src/
RUN 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/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 &&
sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
#暴露端口号
EXPOSE 80 443
#设置存储卷
VOLUME ["/usr/local/apache/htdocs/"]
#传递给ENTRYPOINT的参数
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/local/apache/bin/apachectl"]
//制作镜像
[root@localhost ~]# docker build -t httpd:v0.3 /root/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
---> b996e3b1926b
Step 4/12 : ENV apr_util_version 1.6.1
---> Using cache
---> a6751f2cc3cd
Step 5/12 : ENV httpd_version 2.4.51
---> Using cache
---> d7c69860ee41
Step 6/12 : ENV PATH /usr/local/apache/bin:$PATH
---> Using cache
---> fa93773a3c9a
Step 7/12 : ADD files/* /usr/src/
---> Using cache
---> a6014b41dcee
Step 8/12 : RUN 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/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 && sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
---> Using cache
---> bac20eb00705
Step 9/12 : EXPOSE 80 443
---> Using cache
---> 8353f3ed0b33
Step 10/12 : VOLUME ["/usr/local/apache/htdocs/"]
---> Using cache
---> 79b011cbcff4
Step 11/12 : CMD ["-D","FOREGROUND"]
---> Using cache
---> ee8122f47f9c
Step 12/12 : ENTRYPOINT ["/usr/local/apache/bin/apachectl"]
---> Using cache
---> 58c58250f4e7
Successfully built 58c58250f4e7
Successfully tagged httpd:v0.3
//查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
1314444/httpd v0.2 0e0d1f4bd106 10 seconds ago 703MB
//基于新镜像创建容器
[root@localhost ~]# docker run --name httpd02 -dit -p 80:80 1314444/httpd:v0.2
be35164fcf4676117558f96c164980f188246167ee4e70d949e91f391ae71ad2
[root@localhost ~]# docker ps
ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be35164fcf46 1314444/httpd:v0.2 "/usr/local/apache/b…" 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.2 0e0d1f4bd106 4 minutes ago 703MB [root@localhost ~]# docker push 1314444/httpd:v0.2 The push refers to repository [docker.io/1314444/httpd] 0afddf7e7a57: Pushed 49d8e5c1af37: Pushed 74ddd0ec08fa: Layer already exists v0.2: digest: sha256:20337cdfd55de2cd1b87f796661535d6c97e6b837c03cb1390efe01a43cca342 size: 954



