问题:执行docker run ubuntu 后,容器处于 exited 状态,希望能够出入up状态,可以exec进去查看
原因: docker容器执行任务完成后就会处于exited状态
操作系统:centos7
[root@mapleaves k8s-test]# docker run -d --name ubuntu ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
5b7339215d1d: Pull complete
14ca88e9f672: Pull complete
a31c3b1caad4: Pull complete
b054a26005b7: Pull complete
Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
Status: Downloaded newer image for ubuntu:latest
0f790e3bc43855d97347d91f2c54a4f47d4c4aa2e87ac2d5fe03c451374b34eb
[root@mapleaves k8s-test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@mapleaves k8s-test]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f790e3bc438 ubuntu "/bin/bash" 12 seconds ago Exited (0) 11 seconds ago ubuntu
方法1: 使用 docker run -dit
[root@mapleaves k8s-test]# docker run -dit --name ubuntu ubuntu
faa5fcdcf5256382f4b739e4da5dc212bed5d5b7e93428ae22bd89866853077e
[root@mapleaves k8s-test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
faa5fcdcf525 ubuntu "/bin/bash" 3 seconds ago Up 2 seconds ubuntu
方法2:执行一个循环或者不会段的任务
[root@mapleaves ~]# docker run -d --name ubuntu2 ubuntu sleep infinity
[root@mapleaves ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbfe1b787584 ubuntu "sleep infinity" 57 seconds ago Up 56 seconds ubuntu2
faa5fcdcf525 ubuntu "/bin/bash" 10 minutes ago Up 10 minutes ubuntu
参考链接:
https://stackoverflow.com/questions/28212380/why-docker-container-exits-immediately