1. 基于supervisord的sshd和httpd
获取最新的centos镜像
# docker pull centos:latest
编写Dockerfile(支持apache+ssh服务)
# mkdir /home/Dockerfile/webserver
# cd /home/Dockerfile/webserver
# vim Dockerfile
Dockerfile内容如下:
# This is a base comment
FROM centos:latest
MAINTAINER renguijiayi
#yum install Package
RUN yum -y install net-tools
RUN yum -y install iputils iproute man vim-minimal openssh-server openssh-clients
RUN yum -y install httpd
RUN yum -y install python-setuptools
RUN easy_install supervisor
#set sshd
RUN ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
RUN echo 'root:qwe123' | chpasswd
#set supervisor
RUN mkdir -p /var/log/supervisor
ADD supervisord.conf /etc/supervisord.conf
#set port
EXPOSE 22
EXPOSE 80
#set ENV
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
#run supervisor
CMD ["/usr/bin/supervisord -c /etc/supervisord.conf"]
通过supervisord来维护Docker容器中服务进程,编写supervisord.conf:
# vi supervisord.conf
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
[program:httpd]
command=/usr/sbin/httpd -DFOREGROUND
创建镜像,运行:
# docker build -t test/webserver:v1 .
2. 基于supervisord的sshd和mysql
直接上Dockerfile:
FROM centos:centos6
MAINTAINER renguijiayi
RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN yum install -y openssh-server sudo mysql-server mysql supervisor
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN useradd admin
RUN echo "admin:admin" | chpasswd
RUN echo "admin ALL=(ALL) ALL" >> /etc/sudoers
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN mkdir /var/run/sshd
RUN /etc/init.d/mysqld start &&\
mysql -e "grant all privileges on *.* to 'root'@'%' identified by 'letmein';"&&\
mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by 'letmein';"&&\
mysql -u root -pletmein -e "show databases;"
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisord.conf
EXPOSE 22 3306
CMD ["/usr/bin/supervisord"]
在Dockerfile所在目录下创建supervisord.conf文件,内容如下:
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
[program:mysqld]
command=/usr/bin/mysqld_safe
在Dockerfile所在目录下运行build命令来生成image文件,这里使用mysql_server作为image文件名:
sudo docker build -t myserver .
首先使用下面的命令来启动容器:
sudo docker run --name=myserver -d -P myserver
启动完容器后,可以使用“sudo docker ps”来查看,此时可以看PORTS列内容为“0.0.0.0:49171->22/tcp, 0.0.0.0:49172->3306/tcp”容器的22端口和3306端口会被映射到宿主机器的49171和49172端口。
可以通过下面的命令来访问ssh和mysql服务:
ssh admin@<宿主机器> -p <宿主机器端口>
mysql -h <宿主机器> -u root -pletmein -P 49172
也可以使用“sudo docker inspect myserver | grep IPAddress”来查看容器IP地址,然后通过下面命令访问ssh和mysql服务:
ssh admin@<容器机器IP>
mysql -h <容器机器IP> -u root -pletmein
总结:sshd在大多数情况下对于开发人员来说都是基本要求。如果还有其它进程需要运行的话,利用supervisord来管理多进程的启动是十分方便的。所以sshd和supervisord是制作基础模板的基本要求。