Dockerfile创建ssh服务

1、创建工作目录
mkdir sshd_ubuntu
cd sshd_ubuntu
touch Dockerfile run.sh
2、编写run.sh的内容
#!/bin/bash
/usr/sbin/sshd -D
在宿主机上生成SSH密钥对,并创建authorized_keys文件
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub > authorized_keys
3、编写Dockerfile

	#设置继承镜像
	FROM ubuntu:14.04
	#提供一些作者信息
	MAINTAINER from www.dockerpool.com by waitfish([email protected])
	#下面开始运行命令。此处更改ubuntu的源为国内163的源
	RUN echo "deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse" > /etc/apt/sources.list
	RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
	RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
	RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse" >> /etc/apt/sources.list
	RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list
	RUN apt-get update

	#安装ssh服务
	RUN apt-get install -y openssh-server
	RUN mkdir -p /var/run/sshd
	RUN mkdir -p /root/.ssh

	#取消pam限制
	RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd

	#复制配置文件到相应的位置,并赋予脚本可执行权限
	ADD authorized_keys /root/.ssh/authorized_keys
	ADD run.sh /run.sh
	RUN chmod 755 /run.sh

	#开放端口
	EXPOSE 22

	#设置自启动命令
	CMD ["/run.sh"]
4、创建景象
	在sshd_ubuntu目录下,使用docker build命令来创建镜像。
	cd sshd_ubuntu
	docker build -t sshd:dockerfile .
5、测试镜像,运行容器,映射容器的22端口到本地的10112端口
	docker run -d -p 10122:22 sshd:dockerfile
	docker ps
	ssh 192.168.1.200 -p 10122

你可能感兴趣的:(docker)