方法1:

在启动容器的时候,加上/usr/sbin/init







[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                           PORTS                  NAMES
0a075194063e        sw-centos-ssh:v1.0.1   "/usr/sbin/sshd -D"      31 minutes ago      Up 30 minutes                    0.0.0.0:8031->22/tcp   swtest1
ddcbaa6743ab        10c90a13948a           "/bin/sh -c 'wget -O "   38 minutes ago      Exited (127) 38 minutes ago                             furious_mirzakhani
faacb6e9dccb        centos                 "/bin/bash"              49 minutes ago      Up 49 minutes                                           thirsty_mcclintock
fa96798c7e09        centos-ssh             "/usr/sbin/sshd -D"      About an hour ago   Up About an hour                 0.0.0.0:8022->22/tcp   mytest1
a6c9e8d025f9        78ba52340d5a           "/bin/sh -c 'yum inst"   About an hour ago   Exited (1) About an hour ago                            grave_wozniak
953c5f38db4f        centos                 "/bin/bash"              3 hours ago         Exited (137) About an hour ago                          big_lumiere
9e9de5840f43        78ba52340d5a           "/bin/sh -c 'yum inst"   3 hours ago         Exited (1) 3 hours ago                                  thirsty_hugle
[root@localhost ~]# docker commit 0a075194063e sw-centos-ssh:v1.0.2
sha256:65f98d48a23598d5c2f952a245c0ac66924ac2e856b6d3a2dad13f99aec7dadc
[root@localhost ~]# 
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sw-centos-ssh       v1.0.2              65f98d48a235        3 minutes ago       479.1 MB
[root@localhost ~]# docker run -d -p 8023:22 --name=swtest2 sw-centos-ssh:v1.0.2 /usr/sbin/init
3f6a3283f006b86e4dfaeba59488b30e27f986f393f2e942a3443c441bde153f
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                           PORTS                  NAMES
3f6a3283f006        sw-centos-ssh:v1.0.2   "/usr/sbin/init"         14 seconds ago      Up 12 seconds                    0.0.0.0:8023->22/tcp   swtest2
[root@localhost ~]# docker inspect 3f6a3283f006 | grep "IPA"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.5",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.5",
[root@localhost ~]# ssh -l root 172.17.0.5
[root@3f6a3283f006 ~]# systemctl start httpd
[root@3f6a3283f006 ~]# systemctl enable httpd
[root@3f6a3283f006 ~]# 


方法2

 还有一种解决办法,就是在通过Dockerfile生成镜像文件的时候,通过CMD来执行/usr/sbin/init这条命令,即:CMD [ "/usr/sbin/init"];






Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐