在宿主机上准备的文件清单:

Dockerfile
#启动ssh和apache服务的角本
run.sh

以上文件都放到/root/apache_centos目录下

mkdir -p /root/apache_centos
cd /root/apache_centos

基础镜像:以镜像centos为基础的开放SSH服务的镜像

[root@localhost apache_centos]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sshd                dockerfile          411d5fb16366        23 hours ago        278 MB
centos              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:dockerfile
MAINTAINER by Steven
#安装wget
RUN yum install -y wget
WORKDIR /usr/local/src
#下载并解压源码包
RUN wget http://apache.fayea.com/httpd/httpd-2.4.17.tar.gz
RUN tar -zxvf httpd-2.4.17.tar.gz
WORKDIR httpd-2.4.17
#编译安装apache
RUN yum install -y gcc make apr-devel apr apr-util apr-util-devel pcre-devel
RUN ./configure --prefix=/usr/local/apache2  --enable-mods-shared=most  --enable-so
RUN make
RUN 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.sh
RUN chmod 755 /usr/local/sbin/run.sh
#开放80端口
EXPOSE 80
CMD ["/usr/local/sbin/run.sh"]

需要注意的是:在Dockerfile文件中更换当前目录不可以用“cd”命令,而要改用“WORKDIR”.
三、生成镜像

docker build -t apache_dockerfile:centos .

查看生成的镜像:

[root@localhost apache_centos]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
apache_dockerfile   centos              f8f30b4a0ee8        24 minutes ago      440 MB
apache              centos              f92c55dddd07        17 hours ago        423.9 MB
sshd                dockerfile          411d5fb16366        23 hours ago        278 MB
centos              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 -a
CONTAINER ID        IMAGE                      COMMAND                CREATED             STATUS                      PORTS                                        NAMES
ae560e497f39        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_bardeen
6490cd244c10        apache:centos              "/usr/local/apache2/   17 hours ago        Exited (0) 17 hours ago                                                  loving_wright
673e946b57e4        sshd:dockerfile            "/usr/local/sbin/run   18 hours ago        Exited (137) 17 hours ago                                                reverent_bell

3、测试
测试apache

[root@localhost apache_centos]# curl localhost:8000
<html><body><h1>It works!</h1></body></html>

成功!
测试ssh

[root@localhost apache_centos]# ssh localhost -p 2222
root@localhost's password:

成功!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐