按照笔记4中的方法docker 容器ubuntu已经配置好ssh服务,可是每次重启ubuntu之后,直接ssh提示下面错误,进去之后发现sshd服务没有开机启动,手动起来之后就可以了,但是重启之后还是not running的,忧桑!
root@instance-1:~# ssh s1
ssh: connect to host s1 port 22: Connection refused
root@instance-1:~# docker exec -it 84b3ba091fe2 /bin/bash
root@ubuntu01:/# service ssh status
* sshd is not running
root@84b3ba091fe2:/# systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
root@84b3ba091fe2:/# systemctl status ssh
System has not been booted with systemd as init system (PID 1). Can't operate.
由于默认的ubuntu:latest版本是18.04,没有/etc/rc.local,想要设置为开机启动的话,就需要使用systemctl enable ssh指令,但是docker并没有给ubuntu systemctl权限,只能enable/disable,没办法~~~
不过从网上找到了一个ubuntu16.04的版本,经过修改sshd_config,之后做了我自己的镜像,开机后sshd可以自动起来。
如果需要只需要 docker pull elaizqu/ubuntu16.04:sshd 即可下载使用。
https://blog.csdn.net/qq_27068845/article/details/77015432
#ubuntu18.04 sshd 服务指令
# 开机自动启动ssh命令
sudo systemctl enable ssh
# 关闭ssh开机自动启动命令
sudo systemctl disable ssh
# 单次开启ssh
sudo systemctl start ssh
# 单次关闭ssh
sudo systemctl stop ssh
# 设置好后重启系统
reboot
#查看ssh是否启动,看到Active: active (running)即表示成功
sudo systemctl status ssh
另外,试了一下centos,开始也是不行,因为不具有systemctl权限,但是通过添加权限之后,即可。参考链接如下,只有在生成容器的时候添加--privileged=true和/user/sbin/init,其他设置和之前都一样,进入到容器后执行systemctl enable ssh 即可。
ubuntu18.04也尝试了这种方法,但不好使,有大神解决的话麻烦留言告知!!
https://blog.csdn.net/song121/article/details/83022557blog.csdn.netroot@instance-1:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
elaizqu/ubuntu16.04 sshd 108daf4f6324 15 minutes ago 336MB
elaiz/ubuntu ssh d4cbb2e50830 20 hours ago 242MB
elaizqu/ubuntu ssh a3d45c79340a 2 days ago 245MB
elaizqu/centos git ccba7fafa06f 2 days ago 624MB
elaizqu/centos ssh abcd4764f162 2 days ago 506MB
elaizqu/centos screen 270f1e9aad7a 2 days ago 308MB
ubuntu latest 3556258649b2 5 days ago 64.2MB
centos latest 9f38484d220f 4 months ago 202MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
hub.c.163.com/public/ubuntu 16.04-tools 1196ea15dad6 2 years ago 336MB
root@instance-1:~# docker run -itd --hostname ubuntu16_04 --name ubuntu16.04 elaizqu/ubuntu16.04:sshd
0ad75b6b5d5504c1ef9d2dc8eee3a8ac80920dcc4b3a69c0467b7afaa5cc72fa
root@instance-1:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ad75b6b5d55 elaizqu/ubuntu16.04:sshd "/usr/bin/supervisord" 26 seconds ago Up 15 seconds 22/tcp ubuntu16.04
728f8421b392 abcd4764f162 "/usr/sbin/init" 29 hours ago Up 6 hours centos1
44c1b1ee0143 a3d45c79340a "/bin/bash" 47 hours ago Up 6 hours ubuntu04
f7711f26887f a3d45c79340a "/bin/bash" 47 hours ago Up 6 hours ubuntu03
0efb28e952d0 a3d45c79340a "/bin/bash" 47 hours ago Up 6 hours ubuntu02
84b3ba091fe2 a3d45c79340a "/bin/bash" 47 hours ago Up 5 hours ubuntu01
root@instance-1:~# docker exec -it 0ad75b6b5d55 /bin/bash
root@ubuntu16_04:/# service ssh status
* sshd is running
root@ubuntu16_04:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:07
inet addr:172.17.0.7 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:788 (788.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@ubuntu16_04:/# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ubuntu16_04:/# read escape sequence
root@instance-1:~# ssh 172.17.0.7
root@172.17.0.7's password:
Welcome to Ubuntu Xenial Xerus (development branch) (GNU/Linux 4.9.0-9-amd64 x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Mon Jul 29 03:02:34 2019 from 172.17.0.1
root@ubuntu16_04:~# logout
Connection to 172.17.0.7 closed.
root@instance-1:~# docker restart 0ad75b6b5d55
0ad75b6b5d55
root@instance-1:~# ssh 172.17.0.7
root@172.17.0.7's password:
Welcome to Ubuntu Xenial Xerus (development branch) (GNU/Linux 4.9.0-9-amd64 x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Mon Jul 29 03:27:49 2019 from 172.17.0.1
root@ubuntu16_04:~#
ps:至于docker attach 与docker exec进入容器有何不同还需要研究,比如上面这个ubuntu16.04就不可以用attach进入,只能用exec。。。