Docker退出容器不關(guān)閉容器的方法

進(jìn)入docker容器后如果退出容器,容器就會(huì)變成Exited的狀態(tài),那么如何退出容器讓容器不關(guān)閉呢?
如果要正常退出不關(guān)閉容器,請(qǐng)按Ctrl+P+Q進(jìn)行退出容器,這一點(diǎn)很重要,請(qǐng)牢記!

以下示例為退出容器但不關(guān)閉容器

[root@localhost ~]# docker attach c600c4519fc8
[root@c600c4519fc8 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
c600c4519fc8        centos              "/bin/bash"         3 hours ago         Exited (0) 1 second ago                       pensive_jackson
5a7a0d694651        busybox             "sh"                20 hours ago        Exited (0) 20 hours ago                       hungry_vaughan
4b0296d18849        hello-world         "/hello"            46 hours ago        Exited (0) 46 hours ago                       hopeful_yonath
[root@localhost ~]# docker start pensive_jackson
pensive_jackson
[root@localhost ~]# docker attach c600c4519fc8

Ctrl + P + Q 

[root@c600c4519fc8 /]# read escape sequence
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
c600c4519fc8        centos              "/bin/bash"         3 hours ago         Up 22 seconds                                 pensive_jackson
5a7a0d694651        busybox             "sh"                20 hours ago        Exited (0) 20 hours ago                       hungry_vaughan
4b0296d18849        hello-world         "/hello"            46 hours ago        Exited (0) 46 hours ago                       hopeful_yonath

事實(shí)上我們可以在啟動(dòng)容器的時(shí)候就進(jìn)行配置,加入-d參數(shù)來(lái)啟動(dòng)容器,當(dāng)然,這條命令僅限于啟動(dòng)全新的容器,啟動(dòng)關(guān)閉的容器是不可以的。

Tips 1
docker run -d: 后臺(tái)運(yùn)行容器,并返回容器ID

以下示例為使用docker -d啟動(dòng)容器并退出

[root@localhost ~]# docker run -i -t -d centos /bin/bash
8521b11d5d99535d4cb0080adc5a58a4dd018ecd0751d9945f7da7ab01bec330
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
8521b11d5d99        centos              "/bin/bash"         4 seconds ago       Up 4 seconds                                    eager_goldwasser
c600c4519fc8        centos              "/bin/bash"         3 hours ago         Exited (0) 28 seconds ago                       pensive_jackson
5a7a0d694651        busybox             "sh"                20 hours ago        Exited (0) 20 hours ago                         hungry_vaughan
4b0296d18849        hello-world         "/hello"            46 hours ago        Exited (0) 46 hours ago                         hopeful_yonath
[root@localhost ~]# docker attach 8
[root@8521b11d5d99 /]# uname -r
3.10.0-514.el7.x86_64
[root@8521b11d5d99 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
8521b11d5d99        centos              "/bin/bash"         2 minutes ago       Exited (0) 2 seconds ago                       eager_goldwasser
c600c4519fc8        centos              "/bin/bash"         3 hours ago         Exited (0) 2 minutes ago                       pensive_jackson
5a7a0d694651        busybox             "sh"                20 hours ago        Exited (0) 20 hours ago                        hungry_vaughan
4b0296d18849        hello-world         "/hello"            46 hours ago        Exited (0) 46 hours ago                        hopeful_yonath

在這里你可能會(huì)發(fā)現(xiàn),使用了-d的命令退出后容器依然還是死了,動(dòng)手型的朋友可能會(huì)發(fā)現(xiàn)只是用docker run -d去啟動(dòng)容器也一樣是死的
這里其實(shí)需要了解的是容器的運(yùn)行機(jī)制,Docker容器在后臺(tái)運(yùn)行,必須要有一個(gè)前臺(tái)進(jìn)程,這里我們讓容器有前臺(tái)程序運(yùn)行,就可以實(shí)現(xiàn)容器的-d 啟動(dòng)后存活

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
c600c4519fc8        centos              "/bin/bash"         3 hours ago         Exited (0) 4 minutes ago                       pensive_jackson
5a7a0d694651        busybox             "sh"                21 hours ago        Exited (0) 21 hours ago                        hungry_vaughan
4b0296d18849        hello-world         "/hello"            47 hours ago        Exited (0) 47 hours ago                        hopeful_yonath
[root@localhost ~]# docker run -d centos /bin/bash -c "nohup ping -i 1000 www.baidu.com"
8aa19c9604382bc019797ccda831ae1bcebd81d86380b6040d636e03000b440a
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
8aa19c960438        centos              "/bin/bash -c 'nohup…"   2 seconds ago       Up 2 seconds                                   adoring_wing
c600c4519fc8        centos              "/bin/bash"              3 hours ago         Exited (0) 5 minutes ago                       pensive_jackson
5a7a0d694651        busybox             "sh"                     21 hours ago        Exited (0) 21 hours ago                        hungry_vaughan
4b0296d18849        hello-world         "/hello"                 47 hours ago        Exited (0) 47 hours ago                        hopeful_yonath

我這里使用nohup在后臺(tái)運(yùn)行一個(gè)每1000秒ping一次百度的進(jìn)程,另外你也可以使用"while true; do echo hello world; sleep 1; done",無(wú)限輸出hello world。

另外即便是有進(jìn)程在后臺(tái)運(yùn)行,你進(jìn)入了容器,輸入exit退出,依然會(huì)終止容器的運(yùn)行,請(qǐng)謹(jǐn)記。

Ctrl+P+Q依然是我認(rèn)為的最佳用法。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容