問題情境:現在有一個服務器主機,安裝了docker,想給成員分配各自的容器,但不想成員通過宿主機進入容器。那么成員如何直接訪問容器呢?
成員可以通過ip加端口號訪問
因此,需要生成一個容器,將容器的22端口號映射到宿主機的端口號。
下面為具體操作
1.啟動docker image時可以指定端口映射並設置privilege為true
docker run -ditp 5512:22 --name=xxxx --privileged=true [imageID] /bin/bash
2.進入容器為了之后操作便捷,先進行換源(可選)RedHat系列與Debian系列操作不同,具體問題具體分析
cd /etc/apt/
mv sources.list sources.list.backup
echo "deb http://mirrors.aliyun.com/debian wheezy main contrib non-free" >> sources.list
echo "deb-src http://mirrors.aliyun.com/debian wheezy main contrib non-free" >> sources.list
echo "deb http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free" >> sources.list
echo "deb-src http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free" >> sources.list
echo "deb http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free" >> sources.list
echo "deb-src http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free" >> sources.list
3.apt-get update
4.apt-get install openssh-server (安裝ssh服務)
5.install vim(沒有的話)
6.vim /etc/ssh/sshd_config
將sshd_config文件中PermitRootLogin修改為yes,一般默認的話是前面加了“#”,注釋了,先把#去掉,或者直接復制下面這句
PermitRootLogin yes
7.重啟ssh服務/etc/init.d/ssh restart
- ps -e | grep ssh 查看ssh進程
9.passwd (設置新的密碼,方便登錄)
10.一個端口映射的容器完成,現在成員可以通過ip加端口號直接訪問容器了。
