標籤:

Docker中使用Dockerfile生成ssh服務

(1)第一步:

本機上使用「ssh-keygen -t rsa」,將「/root/.ssh/id_rsa.key「裡面的內容複製到authorized_keys中,並拷貝至當前目錄下。

(2)第二步:

書寫Dockerfile:

FROM ubuntu:14.04nnMAINTAINER from sonnynn#RUN echo "deb http://mirrors.163.com/ubuntu/ wily main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb http://mirrors.163.com/ubuntu/ wily-security main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb http://mirrors.163.com/ubuntu/ wily-updates main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb http://mirrors.163.com/ubuntu/ wily-proposed main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb http://mirrors.163.com/ubuntu/ wily-backports main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb-src http://mirrors.163.com/ubuntu/ wily main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb-src http://mirrors.163.com/ubuntu/ wily-security main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb-src http://mirrors.163.com/ubuntu/ wily-updates main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb-src http://mirrors.163.com/ubuntu/ wily-proposed main restricted universe multiverse" > /etc/apt/sources.listn#RUN echo "deb-src http://mirrors.163.com/ubuntu/ wily-backports main restricted universe multiverse" > /etc/apt/sources.listnnRUN apt-get updatennRUN apt-get install -y openssh-servernnRUN mkdir -p /var/run/sshdnnRUN mkdir -p /root/.sshnnRUN sed -ri s/session required pam_loginuid.so/#session required pam_loginuid.so/g /etc/pam.d/sshdnnADD authorized_keys /root/.ssh/authorized_keysn#ADD id_rsa /root/.ssh/id_rsan#ADD id_rsa.pub /root/.ssh/id_rsa.pubnADD known_hosts /root/.ssh/known_hostsnnADD run.sh /run.shnRUN chmod 755 /run.shnnEXPOSE 22nCMD ["/run.sh"]n

上面的指令,不多說了!

run.sh代碼內容:

#!/bin/bashnn/usr/sbin/sshd -Dn

使用docker build -t sshd:dockerfile . 指令執行後生成docker鏡像。

在後台啟動docker鏡像,」docker run -d -p 10022:22 sshd:dockerfile「,如果正常啟動時,則可以看到

現在測試一下:

使用:」ssh 127.0.0.1 -p 10022「,如果出現下列,則啟動成功:

推薦閱讀:

DaoCloud和雲雀到底誰家的技術比較強一些?VMware和微軟系的比較?
Docker 容器與鏡像的儲存
教程 | 使用 Docker 安裝深度學習環境
Docker的作用的主要作用是什麼?

TAG:Docker |