在使用Docker的时候常常会遇到单个Docker容器启动多个服务时非常困难,比如我需要在单个Docker容器中启动SSH和Mysql,会遇到mysql启动成功但是没有SSH功能或有SSH功能mysql无法启动成功。那么我们用什么方式来解决这种问题呢?
supervisor完美的解决了我们问题,接下来看一下如何利用supervisor启动带有SSH功能的Mysql。
1.cetnos基础镜像 构建ssh-supervisor
下载地址:
docker pull aarongo/centos-ssh-supervisor
另附dockerfile:
FROM centos:centos6 MAINTAINER aaron "aaron.docker@gmail.com" #Install supervisor RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm RUN yum -y update && yum -y install python-pip && /usr/bin/pip install supervisor RUN mkdir -p /etc/supervisor/conf.d && mkdir -p /var/log/supervisor COPY supervisord.conf /etc/supervisord.conf VOLUME /var/log/supervisor #install sshd RUN yum install -y openssh-server && sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config # select root pasword RUN echo "root:pasword" | chpasswd && echo "root ALL=(ALL) ALL" >> /etc/sudoers RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key RUN mkdir /var/run/sshd EXPOSE 22 CMD ["/usr/bin/supervisord"]
运行方式:
docker run -d -p <localhostport>:<Containerport> aarongo/centos-ssh-supervisor #or docker run -it -p <localhostport>:<Containerport> aarongo/centos-ssh-supervisor
2.构建以ssh-supervisor为基础的mysql
下载地址:
docker pull aarongo/centos-mysql-supervisor
Dockerfile:
FROM aarongo/centos-ssh-supervisor MAINTAINER aaron "aaron.docker@gmail.com" RUN yum install -y mysql-server mysql RUN /etc/init.d/mysqld start &&\ mysql -e "grant all privileges on *.* to 'user'@'%' identified by 'password';"&&\ mysql -e "grant all privileges on *.* to 'user'@'localhost' identified by 'password';"&&\ mysql -u user -ppassword -e "show databases;" COPY supervisord.conf /etc/supervisord.conf VOLUME ["/mysqldata"] EXPOSE 22 3306 CMD ["/usr/bin/supervisord"] #VOLUME 选项是将本地的目录挂在到容器中 此处要注意:当你运行-v <hostdir>:<Containerdir> 时要确保目录内容相同否则会出现数据丢失
运行方式:
docker run -p <localport>:<container 22> -p <localprot>:<container 3306> -it -v /mysqldata:/var/lib/mysql aarongo/centos-mysql-supervisor #or docker run -d -p <localport>:<container 22> -p <localprot>:<container 3306> -v /mysqldata:/var/lib/mysql aarongo/centos-mysql-supervisor
如果运行成功:
2014-11-26 03:55:27,487 INFO success: sshd entered RUNNING state, process has stayed up for > than 1 stsecs) 2014-11-26 03:55:27,487 INFO success: mysqld entered RUNNING state, process has stayed up for > than 1artsecs)