起始: 使用docker run –name test命令启动并创建容器发现容器一启动就自动退出 , 使用 docker start test 命令也没用
[root@localhost vagrant]# docker run --name test -d centos /bin/bash 956977ec4620bf6882dc80a6bfc4e42b94533c30ba3a487281b230b4aa47d41c [root@localhost vagrant]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 956977ec4620 centos "/bin/bash" 31 seconds ago Exited (0) 30 seconds ago test e6de62885f52 mysql:5.7 "docker-entrypoint.s…" 2 days ago Up 6 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
原因:docker容器运行时,如果没有前台进程运行,容器处于空闲状态就会自动退出
解决方法: 使用docker run test2 -dit centos /bin/bash 命令可以让容器一直在后台运行
[root@localhost vagrant]# docker run --name test2 -dit centos /bin/bash 822b658eb582f3652df9e52d04ddf271f7d382ed21a2e8fab1483a5c46be30da [root@localhost vagrant]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 822b658eb582 centos "/bin/bash" 40 seconds ago Up 39 seconds test2 956977ec4620 centos "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago test e6de62885f52 mysql:5.7 "docker-entrypoint.s…" 2 days ago Up 8 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
这时关闭后再启动也可以了
[root@localhost vagrant]# docker stop test2 test2 [root@localhost vagrant]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 822b658eb582 centos "/bin/bash" 3 minutes ago Exited (0) 6 seconds ago test2 956977ec4620 centos "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago test e6de62885f52 mysql:5.7 "docker-entrypoint.s…" 2 days ago Up 45 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql [root@localhost vagrant]# docker start test2 test2 [root@localhost vagrant]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 822b658eb582 centos "/bin/bash" 3 minutes ago Up 1 second test2 956977ec4620 centos "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago test
e6de62885f52 mysql:5.7 "docker-entrypoint.s…" 2 days ago Up 52 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql