须要提早下载好官方的ubuntu镜像,我这里使用的是ubuntu:14.04版本,这里安装了一些基础的工具ssh,curl,wget,vim等等,包括后续的Docker镜像须要启动多个服务,因此提早先装好supervisor。html
Dockerfile文件git
############################################ # version : birdben/tools:v1 # desc : 当前版本安装的ssh,wget,curl,supervisor ############################################ # 设置继承自ubuntu官方镜像 FROM ubuntu:14.04 # 下面是一些建立者的基本信息 MAINTAINER birdben (191654006@163.com) # 注意这里要更改系统的时区设置,由于在 web 应用中常常会用到时区这个系统变量,默认的 ubuntu 会让你的应用程序发生难以想象的效果哦 ENV DEBIAN_FRONTEND noninteractive # 清空ubuntu更新包 RUN sudo rm -rf /var/lib/apt/lists/* # 一次性安装vim,wget,curl,ssh server等必备软件 # RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list RUN sudo apt-get update RUN sudo apt-get install -y vim wget curl openssh-server sudo RUN sudo mkdir -p /var/run/sshd # 将sshd的UsePAM参数设置成no RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config # 添加测试用户admin,密码admin,而且将此用户添加到sudoers里 RUN useradd admin RUN echo "admin:admin" | chpasswd RUN echo "admin ALL=(ALL) ALL" >> /etc/sudoers # 把admin用户的shell改为bash,不然SSH登陆Ubuntu服务器,命令行不显示用户名和目录 RUN usermod -s /bin/bash admin # 安装supervisor工具 RUN sudo apt-get install -y supervisor RUN sudo mkdir -p /var/log/supervisor # 添加 supervisord 的配置文件,并复制配置文件到对应目录下面。(supervisord.conf文件和Dockerfile文件在同一路径) COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf # 容器须要开放SSH 22端口 EXPOSE 22 # 执行supervisord来同时执行多个命令,使用 supervisord 的可执行路径启动服务。 CMD ["/usr/bin/supervisord"]
Dockerfile源文件连接:github
supervisor配置文件内容web
# 配置文件包含目录和进程 # 第一段 supervsord 配置软件自己,使用 nodaemon 参数来运行。 # 第二段包含要控制的 2 个服务。每一段包含一个服务的目录和启动这个服务的命令。 [supervisord] nodaemon=true [program:sshd] command=/usr/sbin/sshd -D
supervisor源文件连接:docker
控制台终端shell
# 构建镜像 $ docker build -t="birdben/tools:v1" . # 运行已经构件好的镜像,由于我使用的ubuntu虚拟机安装的Docker,而个人虚拟机也安装了ssh服务,因此这里指定了宿主机的端口为9999对应Docker容器的22端口 $ docker run -p 9999:22 -t -i "birdben/tools:v1" # 此时查看宿主机的9999端口,已经处于监听状态: $ netstat -aunpt (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN - tcp6 0 0 :::9999 :::* LISTEN - tcp6 0 0 :::22 :::* LISTEN - # 再查看一下宿主机的IP地址,我这里的IP地址是10.211.55.4 $ ifconfig # 此时能够经过ssh远程链接Docker容器了 $ ssh root@10.211.55.4 -p 9999 # 输入密码应该就能够链接到Docker容器了 # 若是遇到下面的问题,这是Linux重装或则openssh-server重装引发的,执行如下命令便可 $ ssh-keygen -R 10.211.55.4 # 若是上述方式很差用,进入此目录,删除的10.211.55.4相关rsa的信息便可 $ vi ~/.ssh/known_hosts @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is 8c:4b:88:88:53:4a:b1:f0:e2:da:9a:dc:aa:67:46:df. Please contact your system administrator. Add correct host key in /Users/ben/.ssh/known_hosts to get rid of this message. Offending RSA key in /Users/ben/.ssh/known_hosts:18 RSA host key for [10.211.55.4]:9999 has changed and you have requested strict checking. Host key verification failed.
遇到的问题和解决办法ubuntu
Q:ssh登陆后,命令行不显示用户名和目录 A:把用户的shell改为bash,不然SSH登陆Ubuntu服务器,命令行不显示用户名和目录 RUN usermod -s /bin/bash admin 参考: http://bbs.csdn.net/topics/390188284 Q:ssh建立admin登陆用户,不使用root登陆 A:这里使用ssh不建议直接使用root用户登陆,建议建立一个新的用户例如admin登陆 RUN useradd admin RUN echo "admin:admin" | chpasswd RUN echo "admin ALL=(ALL) ALL" >> /etc/sudoers 参考: http://blog.csdn.net/kongxx/article/details/38395305 http://blog.csdn.net/kongxx/article/details/38412119 Q:如何修改ssh服务相关配置 A:能够直接修改sshd_config配置文件 vi /etc/ssh/sshd_config 须要修改以下 # 设置不容许root用户登陆 PermitRootLogin yes # 利用 PAM 管理使用者认证有不少好处,能够记录与管理。 # 因此这里咱们建议你使用 UsePAM 且 ChallengeResponseAuthentication 设定为 no,可是咱们这里为了简单设置为密码认证,ChallengeResponseAuthentication设定为yes,UsePAM设置为no ChallengeResponseAuthentication yes UsePAM no 参考: http://my.oschina.net/fsmwhx/blog/143354 http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646032.html