详解使用Dockerfile创建带Apache服务的CentOS Docker镜像
使用Dockerfile创建带Apache服务的CentOSDocker镜像
在宿主机上准备的文件清单:
Dockerfile
#启动ssh和apache服务的角本 run.sh
以上文件都放到/root/apache_centos目录下
mkdir-p/root/apache_centos cd/root/apache_centos
基础镜像:以镜像centos为基础的开放SSH服务的镜像
[root@localhostapache_centos]#dockerimages REPOSITORYTAGIMAGEIDCREATEDVIRTUALSIZE sshddockerfile411d5fb1636623hoursago278MB centoslatest0f73ae75014f5weeksago172.3MB
一、准备run.sh文件
在/root/apache_centos目录新建run.sh
vimrun.sh
内容如下:
#!/bin/bash /usr/sbin/sshd& /usr/local/apache2/bin/httpd-DFOREGROUND
二、准备Dockerfile
在/root/apache_centos目录新建Dockerfile
vimDockerfile
文件内容如下:
#新生成的镜像是基于sshd:dockerfile镜像 FROMsshd:dockerfile MAINTAINERbySteven #安装wget RUNyuminstall-ywget WORKDIR/usr/local/src #下载并解压源码包 RUNwgethttp://apache.fayea.com/httpd/httpd-2.4.17.tar.gz RUNtar-zxvfhttpd-2.4.17.tar.gz WORKDIRhttpd-2.4.17 #编译安装apache RUNyuminstall-ygccmakeapr-develaprapr-utilapr-util-develpcre-devel RUN./configure--prefix=/usr/local/apache2--enable-mods-shared=most--enable-so RUNmake RUNmakeinstall #修改apache配置文件 RUNsed-i's/#ServerNamewww.example.com:80/ServerNamelocalhost:80/g'/usr/local/apache2/conf/httpd.conf #启动apache服务 RUN/usr/local/apache2/bin/httpd #复制服务启动脚本并设置权限 ADDrun.sh/usr/local/sbin/run.sh RUNchmod755/usr/local/sbin/run.sh #开放80端口 EXPOSE80 CMD["/usr/local/sbin/run.sh"]
需要注意的是:在Dockerfile文件中更换当前目录不可以用“cd”命令,而要改用“WORKDIR”.
三、生成镜像
dockerbuild-tapache_dockerfile:centos.
查看生成的镜像:
[root@localhostapache_centos]#dockerimages REPOSITORYTAGIMAGEIDCREATEDVIRTUALSIZE apache_dockerfilecentosf8f30b4a0ee824minutesago440MB apachecentosf92c55dddd0717hoursago423.9MB sshddockerfile411d5fb1636623hoursago278MB centoslatest0f73ae75014f5weeksago172.3MB
四、根据镜像生成的容器并进行测试
1、生成新的容器
dockerrun-d-p2222:22-p8000:80apache_dockerfile:centos/usr/local/sbin/run.sh
将容器的22端口和80端口分别映射到到宿主机上的2222端口和8000端口,并运行服务脚本。
2、查看新生成的容器:
[root@localhostapache_centos]#dockerps-a CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES ae560e497f39apache_dockerfile:centos"/usr/local/sbin/run45secondsagoUp44seconds0.0.0.0:2222->22/tcp,0.0.0.0:8000->80/tcpcondescending_bardeen 6490cd244c10apache:centos"/usr/local/apache2/17hoursagoExited(0)17hoursagoloving_wright 673e946b57e4sshd:dockerfile"/usr/local/sbin/run18hoursagoExited(137)17hoursagoreverent_bell
3、测试
测试apache
[root@localhostapache_centos]#curllocalhost:8000 <html><body><h1>Itworks!</h1></body></html>
成功!
测试ssh
[root@localhostapache_centos]#sshlocalhost-p2222 root@localhost'spassword:
成功!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。