在宿主机上准备的文件清单:
1
2
3
<code>Dockerfile</code>
<code>#启动ssh和apache服务的角本</code>
<code>run.sh</code>
以上文件都放到/root/apache_centos目录下
<code>mkdir</code> <code>-p </code><code>/root/apache_centos</code>
<code>cd</code> <code>/root/apache_centos</code>
基础镜像:以镜像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
4
<code>vim run.sh</code>
<code>#!/bin/bash</code>
<code>/usr/sbin/sshd</code> <code>&</code>
<code>/usr/local/apache2/bin/httpd</code> <code>-D FOREGROUND</code>
二、准备Dockerfile
在/root/apache_centos目录新建Dockerfile
vim Dockerfile
文件内容如下:
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code>#新生成的镜像是基于sshd:dockerfile镜像</code>
<code>FROM sshd:dockerfile</code>
<code>MAINTAINER by Steven</code>
<code>#安装wget</code>
<code>RUN yum </code><code>install</code> <code>-y wget</code>
<code>WORKDIR </code><code>/usr/local/src</code>
<code>#下载并解压源码包</code>
<code>RUN wget http:</code><code>//apache</code><code>.fayea.com</code><code>/httpd/httpd-2</code><code>.4.17.</code><code>tar</code><code>.gz</code>
<code>RUN </code><code>tar</code> <code>-zxvf httpd-2.4.17.</code><code>tar</code><code>.gz</code>
<code>WORKDIR httpd-2.4.17</code>
<code>#编译安装apache</code>
<code>RUN yum </code><code>install</code> <code>-y gcc </code><code>make</code> <code>apr-devel apr apr-util apr-util-devel pcre-devel</code>
<code>RUN .</code><code>/configure</code> <code>--prefix=</code><code>/usr/local/apache2</code> <code>--</code><code>enable</code><code>-mods-shared=most --</code><code>enable</code><code>-so</code>
<code>RUN </code><code>make</code>
<code>RUN </code><code>make</code> <code>install</code>
<code>#修改apache配置文件</code>
<code>RUN </code><code>sed</code> <code>-i </code><code>'s/#ServerName www.example.com:80/ServerName localhost:80/g'</code> <code>/usr/local/apache2/conf/httpd</code><code>.conf</code>
<code>#启动apache服务</code>
<code>RUN </code><code>/usr/local/apache2/bin/httpd</code>
<code>#复制服务启动脚本并设置权限</code>
<code>ADD run.sh </code><code>/usr/local/sbin/run</code><code>.sh</code>
<code>RUN </code><code>chmod</code> <code>755 </code><code>/usr/local/sbin/run</code><code>.sh</code>
<code>#开放80端口</code>
<code>EXPOSE 80</code>
<code>CMD [</code><code>"/usr/local/sbin/run.sh"</code><code>]</code>
需要注意的是:在Dockerfile文件中更换当前目录不可以用“cd”命令,而要改用“WORKDIR”.
三、生成镜像
<code>docker build -t apache_dockerfile:centos .</code>
查看生成的镜像:
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、生成新的容器
<code>docker run -d -p 2222:22 -p 8000:80 apache_dockerfile:centos </code><code>/usr/local/sbin/run</code><code>.sh</code>
将容器的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) 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:
成功!
本文转自 Mr_sheng 51CTO博客,原文链接:http://blog.51cto.com/sf1314/1980216