1. 程式人生 > >通過DockerFile創建ssh服務鏡像

通過DockerFile創建ssh服務鏡像

docker

說明:創建一個含有sshd服務的基礎鏡像,再在這個基礎鏡像中創建其它中間件鏡像,再利用中間件鏡像創建應用容器。通過Dockerfile可以創建任意自定義容器,配合supervisord服務完美搭配。

1. 編寫Dockerfile

[[email protected] ~]# vi /root/base_ssh/Dockerfile

-----------------DockerFile-----------------

# This is Dockerfile   
# Author: koumm     
# Base image docker.io/centos    
FROM docker.io/centos    
MAINTAINER KOUMM [email protected]
/* */ RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm && \ yum install -y curl wget tar bzip2 unzip net-tools rsync man openssl openssl-devel && \ yum install -y gcc gcc-c++ git make automake cmake patch logrotate python-devel && \ yum -y install openssh-server passwd python-pip && \ yum clean all && \ /usr/bin/pip install supervisor ADD supervisord.conf /etc/supervisord.conf RUN mkdir -p /etc/supervisor.conf.d ADD sshd.ini /etc/supervisor.d/sshd.ini RUN echo ‘QWER#123456‘ | passwd --stdin root RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ‘‘ RUN ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ‘‘ RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ‘‘ RUN sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config RUN sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config EXPOSE 22 ENTRYPOINT ["/usr/bin/supervisord"]

--------------------------------------------

2. 創建supervisord.conf配置文件

#創建supervisord主配置文件,事先將再成的配置文件復制一份出來即可。

# vi /root/base_ssh/supervisord.conf
[unix_http_server]   
file=/tmp/supervisor.sock   ; (the path to the socket file)    
[supervisord]    
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)    
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)    
logfile_backups=10           ; (num of main logfile rotation backups;default 10)    
loglevel=info                ; (log level;default info; others: debug,warn,trace)    
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)    
nodaemon=true               ; (start in foreground if true;default false)    
minfds=1024                  ; (min. avail startup file descriptors;default 1024)    
minprocs=200                 ; (min. avail process descriptors;default 200)    
[rpcinterface:supervisor]    
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface    
[supervisorctl]    
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket    
[include]    
files=/etc/supervisor.d/*.ini


# 創建sshd服務啟動文件

# vi /root/base_ssh/sshd.ini[program:sshd]   
command=/usr/sbin/sshd -D


3. 通過Dockerfile創建鏡像

[[email protected] ~]# docker build -t centos:ssh_v2 /root/base_ssh/

過程略..

base_ssh為準備相關資源的目錄,包括Dockerfile文件的位置。

[[email protected] ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
centos ssh_v2 71fb8b60cf9f 6 minutes ago 407.1 MB
docker.io/centos latest 8140d0c64310 2 weeks ago 192.5 MB


4. 通過鏡像創建容器

[[email protected] ~]# docker run -d -p 2223:22 --name sshtest1 centos:ssh_v2

說明: -p 2223:22 映射要機2223端口到容器22端口

技術分享

然後可以通過ssh登錄主機的2223端口訪問容器ssh服務。

本文出自 “koumm的linux技術博客” 博客,請務必保留此出處http://koumm.blog.51cto.com/703525/1936534

通過DockerFile創建ssh服務鏡像