本篇基于前面文章Docker In Action<一> 制作可以SSH登录的docker容器服务指北生成的centos65-ssh开始制作web服务器,使用httpd或者Tomcat来提供web服务(使用supervisor和volumn 方式来制作服务)。
构建httpd web服务器
一、构建http web服务容器
1 2
| mkdir -p ~/docker/webserver cd ~/docker/webserver
|
Dockerfile
1 2 3 4 5 6 7 8 9
| cat >> ~/docker/webserver/Dockerfile << EOF FROM centos65-ssh MAINTAINER hzchenkj RUN yum install -y httpd RUN yum clean all RUN echo "A simple Docker webserver" > /var/www/html/index.html ADD supervisord.conf /etc/supervisord.conf EXPOSE 80 EOF
|
supervisord.conf 使用supervisord服务
1 2 3 4 5 6 7 8
| cat >> ~/docker/webserver/supervisord.conf << EOF [supervisord] nodaemon=true [program:sshd] command=/usr/sbin/sshd -D [program:httpd] command=/usr/sbin/httpd -D FOREGROUND EOF
|
构建容器
1 2 3 4 5 6 7 8 9 10 11 12
| [root@master webserver] Sending build context to Docker daemon 3.584 kB Sending build context to Docker daemon Step 0 : FROM centos65-ssh ---> 394fac76f4b2 Step 1 : MAINTAINER hzchenkj ---> Running in 1ccb987bae63 ---> ec16a945cba3 Removing intermediate container 1ccb987bae63 Step 2 : RUN yum install -y httpd ---> Running in ee586632d936 Loaded plugins: fastestmirror
|
查看镜像
1 2 3 4 5
| [root@master webserver] REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos65-webserver latest 66b7f5ed174f About a minute ago 449.1 MB centos65-ssh latest 394fac76f4b2 3 hours ago 366.4 MB centos65-base latest 0b1acae7bfea 11 hours ago 292.3 MB
|
启动webserver容器,端口80映射到本机5001端口:
1 2
| [root@master webserver] 0636d5bf9e9b85ffc804012a2d6bd7af78080e85e674bd5975d64a1f126376ac
|
查看启动的webserver容器
1 2 3 4 5 6 7 8 9 10 11
| [root@master webserver] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0636d5bf9e9b centos65-webserver:latest /bin/sh -c 'supervis About a minute ago Up About a minute 22/tcp, 8080/tcp, 0.0.0.0:5001->80/tcp berserk_kirch [root@master webserver]# docker logs 0636d5bf9e9b 2014-10-29 00:08:10,977 CRIT Supervisor running as root (no user in config file) 2014-10-29 00:08:10,986 INFO supervisord started with pid 1 2014-10-29 00:08:10,988 INFO spawned: 'httpd' with pid 8 2014-10-29 00:08:10,989 INFO spawned: 'sshd' with pid 9 2014-10-29 00:08:11,987 INFO success: httpd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2014-10-29 00:08:11,988 INFO success: sshd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) [root@master webserver]#
|
访问wenb服务
1 2 3
| [root@master webserver] A simple Docker webserver [root@master webserver]
|
构建Tomcat web服务器
可以用yum来安装Tomcat服务也可以自己从Tomcat官方网站下载tomcat包和jdk文件进行发布。
本处使用存手工方式来安装JDK,设置环境变量并安装tomcat服务。
使用volumn方式来发布web服务
1 2 3 4 5 6 7
| mkdir -p ~/docker/tomcat cd ~/docker/tomcat touch ~/docker/tomcat/Dockerfile cd /usr/local/src wget http://**/jdk1.6.0_38.tar.gz wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-6/v6.0.41/bin/apache-tomcat-6.0.41.tar.gz mkdir -p /Docker_Volume/webapps
|
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| FROM centos65-ssh MAINTAINER chenkj <hzchenkj@163.com> ADD /usr/local/src/jdk1.6.0_38.tar.gz /tmp/jdk1.6.0_38.tar.gz RUN mv /tmp/jdk1.6.0_38.tar.gz/jdk1.6.0_38 /opt RUN rm -rf /tmp/jdk1.6.0_38.tar.gz ADD /usr/local/src/apache-tomcat-6.0.41.tar.gz /tmp/apache-tomcat-6.0.41.tar.gz RUN mv /tmp/apache-tomcat-6.0.41.tar.gz/apache-tomcat-6.0.41 /opt RUN rm -rf /tmp/apache-tomcat-6.0.41.tar.gz ENV JAVA_HOME /opt/jdk1.6.0_38 ENV PATH $JAVA_HOME/bin:$PATH VOLUME ["/Docker_Volume"] ADD supervisord.conf /etc/supervisord.conf EXPOSE 8080
|
supervisord.conf
1 2 3 4 5 6 7 8
| [supervisord] nodaemon=true [program:sshd] command=/usr/sbin/sshd -D [program:tomcat] command= /opt/apache-tomcat-6.0.41/bin/startup.sh && tail -f /opt/apache-tomcat-6.0.41/logs/catalina.out
|
构建tomcat服务容器
运行tomcat容器