使用Dockerfile创建带Apache服务的CentOS Docker镜像
在宿主机上准备的文件清单:
Dockerfile
#启动ssh和apache服务的角本run.sh
以上文件都放到/root/apache_centos目录下
mkdir -p /root/apache_centoscd /root/apache_centos
基础镜像:以镜像centos为基础的开放SSH服务的镜像
[root@localhost apache_centos]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEsshd dockerfile 411d5fb16366 23 hours ago 278 MBcentos latest 0f73ae75014f 5 weeks ago 172.3 MB
一、准备run.sh文件
在/root/apache_centos目录新建run.sh
vim run.sh
内容如下:
#!/bin/bash/usr/sbin/sshd &/usr/local/apache2/bin/httpd -D FOREGROUND
二、准备Dockerfile
在/root/apache_centos目录新建Dockerfile
vim Dockerfile
文件内容如下:
#新生成的镜像是基于sshd:dockerfile镜像FROM sshd:dockerfileMAINTAINER by Steven#安装wgetRUN yum install -y wgetWORKDIR /usr/local/src#下载并解压源码包RUN wget http://apache.fayea.com/httpd/httpd-2.4.17.tar.gzRUN tar -zxvf httpd-2.4.17.tar.gzWORKDIR httpd-2.4.17#编译安装apacheRUN yum install -y gcc make apr-devel apr apr-util apr-util-devel pcre-develRUN ./configure --prefix=/usr/local/apache2 --enable-mods-shared=most --enable-soRUN makeRUN make install#修改apache配置文件RUN sed -i 's/#ServerName www.example.com:80/ServerName localhost:80/g' /usr/local/apache2/conf/httpd.conf#启动apache服务RUN /usr/local/apache2/bin/httpd#复制服务启动脚本并设置权限ADD run.sh /usr/local/sbin/run.shRUN chmod 755 /usr/local/sbin/run.sh#开放80端口EXPOSE 80CMD ["/usr/local/sbin/run.sh"]
需要注意的是:在Dockerfile文件中更换当前目录不可以用“cd”命令,而要改用“WORKDIR”.
三、生成镜像
docker build -t apache_dockerfile:centos .
查看生成的镜像:
[root@localhost apache_centos]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEapache_dockerfile centos f8f30b4a0ee8 24 minutes ago 440 MBapache centos f92c55dddd07 17 hours ago 423.9 MBsshd dockerfile 411d5fb16366 23 hours ago 278 MBcentos latest 0f73ae75014f 5 weeks ago 172.3 MB
四、根据镜像生成的容器并进行测试
1、生成新的容器
docker run -d -p 2222:22 -p 8000:80 apache_dockerfile:centos /usr/local/sbin/run.sh
将容器的22端口和80端口分别映射到到宿主机上的2222端口和8000端口,并运行服务脚本。
2、查看新生成的容器:
[root@localhost apache_centos]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESae560e497f39 apache_dockerfile:centos "/usr/local/sbin/run 45 seconds ago Up 44 seconds 0.0.0.0:2222->22/tcp, 0.0.0.0:8000->80/tcp condescending_bardeen6490cd244c10 apache:centos "/usr/local/apache2/ 17 hours ago Exited (0) 17 hours ago loving_wright673e946b57e4 sshd:dockerfile "/usr/local/sbin/run 18 hours ago Exited (137) 17 hours ago reverent_bell
新闻热点
疑难解答