分享

Using Supervisor with Docker

howtodown 发表于 2014-9-1 13:45:31 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 8644

问题导读
如何创建Dockerfile?
用什么命令Installing Supervisor?







  1. Note: - If you don't like sudo then see Giving non-root access
复制代码

Traditionally a Docker container runs a single process when it is launched, for example an Apache daemon or a SSH server daemon. Often though you want to run more than one process in a container. There are a number of ways you can achieve this ranging from using a simple Bash script as the value of your container's CMD instruction to installing a process management tool
docker 容器在启动的时候开启单个进程,比如,一个ssh或则apache 的daemon服务。但我们经常需要在一个机器上开启多个服务,这可以有很多方法,最简单的就是把多个启动命令方到一个启动脚本里面,启动的时候直接启动这个脚本,另外就是安装进程管理工具。.
In this example we're going to make use of the process management tool, Supervisor, to manage multiple processes in our container. Using Supervisor allows us to better control, manage, and restart the processes we want to run. To demonstrate this we're going to install and manage both an SSH daemon and an Apache daemon.
这个例子里面我们使用进程管理工具supervisor来管理容器中的多个进程。使用Supervisor可以更好的控制、管理、重启我们希望运行的进程。在这里我们演示一下如何同时使用ssh和apache服务。

Creating a Dockerfile
创建一个dockerfile
Let's start by creating a basic Dockerfile for our new image.
从dockerfile创建一个心的image

  1. FROM ubuntu:13.04
  2. MAINTAINER examples@docker.com
  3. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  4. RUN apt-get update
  5. RUN apt-get upgrade -y
复制代码

Installing Supervisor安装supervisor
We can now install our SSH and Apache daemons as well as Supervisor in our container.
安装  ssh apache supervisor
  1. RUN apt-get install -y openssh-server apache2 supervisor
  2. RUN mkdir -p /var/run/sshd
  3. RUN mkdir -p /var/log/supervisor
复制代码

Here we're installing the openssh-server, apache2 and supervisor (which provides the Supervisor daemon) packages. We're also creating two new directories that are needed to run our SSH daemon and Supervisor.
这里安装3个软件,还创建了2个用来允许ssh和supervisor的目录
Adding Supervisor's configuration file添加supervisor‘s的配置文件
Now let's add a configuration file for Supervisor. The default file is called supervisord.conf and is located in /etc/supervisor/conf.d/.
添加配置文件到对应目录下面
  1. COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
复制代码


Let's see what is inside our supervisord.conf file.
配置文件内容
  1. [supervisord]
  2. nodaemon=true
  3. [program:sshd]
  4. command=/usr/sbin/sshd -D
  5. [program:apache2]
  6. command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
复制代码


The supervisord.conf configuration file contains directives that configure Supervisor and the processes it manages. The first block [supervisord] provides configuration for Supervisor itself. We're using one directive, nodaemon which tells Supervisor to run interactively rather than daemonize.
配置文件包含目录和进程,第一段supervsord配置软件本身,使用nodaemon参数来运行。
The next two blocks manage the services we wish to control. Each block controls a separate process. The blocks contain a single directive, command, which specifies what command to run to start each process.
下面2段包含我们要控制的2个服务。每一段包含一个服务的目录和启动这个服务的命令
Exposing ports and running Supervisor映射端口,开启supervisor
Now let's finish our Dockerfile by exposing some required ports and specifying the CMD instruction to start Supervisor when our container launches.
使用dockerfile来映射指定的端口,使用cmd来启动supervisord
  1. EXPOSE 22 80
  2. CMD ["/usr/bin/supervisord"]
复制代码


Here We've exposed ports 22 and 80 on the container and we're running the /usr/bin/supervisord binary when the container launches.
这里我们映射了22 和80端口,使用supervisord的可执行路径启动服务。
Building our image创建我们的image
We can now build our new image.
  1. $ sudo docker build -t <yourname>/supervisord .
复制代码


Running our Supervisor container启动我们的supervisor容器
Once We've got a built image we can launch a container from it.
启动我们创建的容器
  1. $ sudo docker run -p 22 -p 80 -t -i <yourname>/supervisord
  2. 2013-11-25 18:53:22,312 CRIT Supervisor running as root (no user in config file)
  3. 2013-11-25 18:53:22,312 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
  4. 2013-11-25 18:53:22,342 INFO supervisord started with pid 1
  5. 2013-11-25 18:53:23,346 INFO spawned: 'sshd' with pid 6
  6. 2013-11-25 18:53:23,349 INFO spawned: 'apache2' with pid 7
  7. . . .
复制代码


We've launched a new container interactively using the docker run command. That container has run Supervisor and launched the SSH and Apache daemons with it. We've specified the -p flag to expose ports 22 and 80. From here we can now identify the exposed ports and connect to one or both of the SSH and Apache daemons.
使用docker run来启动我们创建的容器。使用多个-p 来映射多个端口,这样我们就能同时访问ssh和apache服务了。




没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条