# 构建镜像
$ 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.