docker下将容器按照端口号分配

问题情境:现在有一个服务器主机,安装了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

  1. ps -e | grep ssh 查看ssh进程

9.passwd (设置新的密码,方便登录)

10.一个端口映射的容器完成,现在成员可以通过ip加端口号直接访问容器了。

posted @ 2020-11-13 15:46  沐沐子枫  阅读(755)  评论(0编辑  收藏  举报