本文使用截至20180711最新的docker 仓库中最新的 centos
系统信息如下:
[[email protected] /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
1.下载原始镜像
搜索最新的centos镜像
docker search centos
下载到本地
docker pull centos
使用进入镜像
docker run -it --rm -v docker:/opt/data --name mycentos centos bash
-rm 创建并在退出后删除该容器
docker:/opt/data : 映射 本地docker目录到镜像中的/opt/data目录
查看镜像系统版本
cat /etc/os-release
基础镜像构建新装组件
Supervisor:进程管理工具
ssh
2.从原始镜像中构建基础镜像
当前目录下构建文件:
dockerFile
etc/services-config/supervisor/supervisord.conf
etc/services-config/supervisor/supervisord.d(建立空文件即可)
dockerFile
----------------------------------------------------------------------------------------------------------------------
FROM centos
MAINTAINER baseCentosImageDy_ssh
RUN yum -y update
#安装openssh-server
RUN yum -y install \
python-setuptools \
openssh-server
RUN easy_install supervisor supervisor-stdout \
&& mkdir -p \
/var/run/sshd \
&& mkdir -p \
/var/log/supervisor
RUN mkdir -p \
/etc/supervisord.d/
COPY etc/services-config/supervisor/supervisord.conf \
/etc/
COPY etc/services-config/supervisor/supervisord.d \
/etc/supervisord.d/
RUN sed -i 's/UsePAM yes/UsePAM no/g' etc/ssh/sshd_config
#将**文件复制到/etc/ssh/目录中
ADD etc/ssh/ssh_host_rsa_key etc/ssh/ssh_host_rsa_key
ADD etc/ssh/ssh_host_rsa_key.pub etc/ssh/ssh_host_rsa_key.pub
RUN mkdir -p /root/.ssh
ADD etc/ssh/authorized_keys /root/.ssh/authorized_keys
#将ssh服务启动脚本复制到/usr/local/sbin目录中,并改变权限为755
# ADD run.sh /usr/local/sbin/run.sh \
# RUN chmod 755 /usr/local/sbin/run.sh \
#变更root密码为test
RUN echo "root:123456"|chpasswd
EXPOSE 22 9001
CMD /usr/bin/supervisord -c /etc/supervisord.conf
----------------------------------------------------------------------------------------------------------------------
supervisord
----------------------------------------------------------------------------------------------------------------------
[supervisord]
logfile = /var/log/supervisor/supervisor.log
logfile_maxbytes = 50MB
logfile_backups = 2
loglevel = info
pidfile = /var/run/supervisor.pid
minfds = 1024
minprocs = 200
nodaemon = true
identifier=supervisor
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
serverurl=http://*:9001
username=user
password=123
prompt=mysupervisor
history_file=~/.sc_history
[unix_http_server]
file=/tmp/supervisor.sock
[inet_http_server]
port=*:9001
username=user
password=123
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[eventlistener:supervisor_stdout]
command = /usr/bin/supervisor_stdout
buffer_size = 100
events = PROCESS_LOG
result_handler = supervisor_stdout:event_handler
startsecs = 0
[program:sshd]
command=/usr/sbin/sshd -D
[include]
files = /etc/supervisor.d/*.conf /etc/supervisor.d/*.ini
----------------------------------------------------------------------------------------------------------------------
注:
supervisord 3.0以后版本需要配置
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
执行构建命令
docker build -f 'Dockerfile' -t centos_ssh:v1 .
-f :指定文件名称,默认为Dockerfile
. :指定为当前目录
打包完成后:
[docker@docker docker]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_ssh v2 ad9327110273 About a minute ago 385MB
negix v3 4092a849b106 8 days ago 109MB
redis latest 71a81cb279e3 2 weeks ago 83.4MBnginx latest 5699ececb21c 2 weeks ago 109MBcentos latest 49f7960eb7e4 5 weeks ago 200MB
启动容器:
[[email protected] docker]$ docker run -p 10022:22 -p 9001:9001 -it ad932 bash
进入容器后测试supervisord
supervisord -c /etc/supervisord.conf
supervisord : 启动supervisor
supervisorctl reload :修改完配置文件后重新启动supervisor
supervisorctl status :查看supervisor监管的进程状态
supervisorctl start 进程名 :启动XXX进程
supervisorctl stop 进程名 :停止XXX进程
supervisorctl stop all:停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
登陆页面
http://127.0.0.1:10022/
![Docker centos7.4 + supervisor + ssh + supervisor web Docker centos7.4 + supervisor + ssh + supervisor web](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzQ3Mi84NmJhNzhjYzlmMTI2ODJiMDkyNTBhZmFkMTljZjU1OC5wbmc=)
http://localhost:9001/
![Docker centos7.4 + supervisor + ssh + supervisor web Docker centos7.4 + supervisor + ssh + supervisor web](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzQ5MS82YjFhY2M1MTEwZTQ2ZjAyMWJlNTI2NmU4ZDViNDBkMy5wbmc=)