一、问题
我们可以在一个容器中运行应用程序的所有服务,以运行wordpress为例,你想知道一个容器中同时运行MySQL和HTTPD服务。由于Docker运行的是前台进程,所以你需要找到一种运行多个“前台”进程的方式
这种方法有很多,最简单的就是把多个启动命令放到一个启动脚本里面,启动的时候直接启动这个脚本,另外就是安装进程管理工具
二、解决方案
本节我们将使用进程管理工具supervisor来管理容器中的多个进程。使用supervision可以更好的控制、管理重启我们希望运行的进程。
首先下载本书的示例GitHub示例这些文件包含启动虚拟机Vagrantfile,Docker运行在虚拟机中还包含一个Dockerfile来定义要创建的镜像,此外还有一个supervisor的配置文件和一个WordPress的配置文件
Dockerfile:
FROM ubuntu:14.04RUN apt-get update && apt-get -y install \apache2 \php5 \FROM ubuntu:14.04RUN apt-get update && apt-get -y install \apache2 \php5 \php5-mysql \supervisor \wgetRUN echo 'mysql-server mysql-server/root_password password root' | debconf-set-selections && \echo 'mysql-server mysql-server/root_password_again password root' | debconf-set-selectionsRUN apt-get install -qqy mysql-serverRUN wget http://wordpress.org/latest.tar.gz && \tar xzvf latest.tar.gz && \cp -R ./wordpress/* /var/www/html && \rm /var/www/html/index.htmlRUN (/usr/bin/mysqld_safe &); sleep 5; mysqladmin -u root -proot create wordpressCOPY wp-config.php /var/www/html/wp-config.php
Vgrantfile:
# -*- mode: ruby -*-# vi: set ft=ruby :# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!VAGRANTFILE_API_VERSION = "2"Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|# Every Vagrant virtual environment requires a box to build off of.config.vm.box = "ubuntu/trusty64"# Create a forwarded port mapping which allows access to a specific port# within the machine from a port on the host machine. In the example below,# accessing "localhost:8080" will access port 80 on the guest machine.# config.vm.network "forwarded_port", guest: 80, host: 8080# Create a private network, which allows host-only access to the machine# using a specific IP.config.vm.network "private_network", ip: "192.168.33.10"config.vm.provider "virtualbox" do |vb|vb.customize ["modifyvm", :id, "--memory", "2048"]end
[supervisord]nodaemon=true[program:mysqld]command=/usr/bin/mysqld_safeautostart=trueautorestart=trueuser=root[program:httpd]command=/bin/bash -c "rm -rf /run/httpd/* && /usr/sbin/apachectl -D FOREGROUND"
这里定义了两个被监控和运行的服务:mysqld和httpd每个程序都可以使用诸如autorestart和autostart等选项。最重要的指令是command,其定义了如何运行每个程序。
在docker主机上构建该镜像并且启动一个后台程序如果使用了Vagrant的虚拟机,可以执行如下命令。
$ cd /vagrant$ docker build -t wordpress .$ docker run -d -p 80:80 wordpress
因为我没有使用Vagrant的方式所以我就不进行测试了,当容器运行起来以后,游览器中打开http://
三、讨论
尽管通过supervisor在一个容器中运行多个应用服务看起来很完美但是,最好还是使用多个容器来运行不同的服务。这能充分利用容器的隔离优势,也能帮助你创建基于微服务设计思想的应用。最终这也将会使你的应用更具有弹性和拓展性。
