可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm completely a newbie to docker. I tried to start a exited container like follows,
- I listed down all available containers using
docker ps -a
. It listed the following,
I entered the following commands to start the container which is in the exited stage and enter into the terminal of that image.
docker start 79b3fa70b51d
docker exec -it 79b3fa70b51d \bin\sh
It is throwing the following error.
FATA[0000] Error response from daemon: Container 79b3fa70b51d is not running
But when I start the container using docker start 79b3fa70b51d
. It throws the container ID as output which is normal if it have everything work normally. I'm not sure what causes this error. Any idea about the causes and suggestions about this would be greatly helpful for me. Thanks in advance.
回答1:
Container 79b3fa70b51d
seems to only do an echo
.
That means it starts, echo and then exits immediately.
The next docker exec
command wouldn't find it running in order to attach itself to that container and execute any command: it is too late. The container has already exited.
The docker exec
command runs a new command in a running container.
The command started using docker exec
will only run while the container's primary process (PID 1) is running
回答2:
By default, docker container will exit immediately if you do not have any task running on the container.
To keep the container running in the background, try to run it with --detach
(or -d
) argument.
For examples:
docker pull debian
docker run -t -d --name my_debian debian
e7672d54b0c2
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7672d54b0c2 debian "bash" 3 minutes ago Up 3 minutes my_debian
#now you can execute command on the container
docker exec -it my_debian bash
root@e7672d54b0c2:/#
回答3:
If it's not possible to start the main process again (for long enough), there is also the possibility to commit
the container to a new image and run a new container from this image. While this is not the usual best practice workflow, I find it really useful to debug a failing script once in a while.
docker exec -it 6198ef53d943 bash
Error response from daemon: Container 6198ef53d9431a3f38e8b38d7869940f7fb803afac4a2d599812b8e42419c574 is not running
docker commit 6198ef53d943
sha256:ace7ca65e6e3fdb678d9cdfb33a7a165c510e65c3bc28fecb960ac993c37ef33
docker run -it ace7ca65e6e bash
root@72d38a8c787d:/#
回答4:
First of all, we have to start the docker container
ankit@ankit-HP-Notebook:~$ sudo docker start 3a19b39ea021
3a19b39ea021
After that, check the docker container:
ankit@ankit-HP-Notebook:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a19b39ea021 coreapps/ubuntu16.04:latest "bash" 13 hours ago
Up 9 seconds ubuntu1
455b66057060 hello-world "/hello" 4 weeks ago
Exited (0) 4 weeks ago vigorous_bardeen
Then execute by using the command below:
ankit@ankit-HP-Notebook:~$ sudo docker exec -it 3a19b39ea021 bash
root@3a19b39ea021:/#
回答5:
The reason is just what the accepted answer said. I add some extra information, which may provide a further understanding about this issue.
- The status of a container includes
Created
, Running
, Stopped
,
Exited
, Dead
and others as I know.
- When we execute
docker create
, docker daemon will create a
container with its status of Created
.
- When
docker start
, docker daemon will start a existing container
which its status may be Created
or Stopped
.
- When we execute
docker run
, docker daemon will finish it in two
steps: docker create
and docker start
.
- When
docker stop
, obviously docker daemon will stop a container.
Thus container would be in Stopped
status.
- Coming the most important one, a container actually imagine itself
holding a long time process in it. When the process exits, the
container holding process would exit too. Thus the status of this
container would be
Exited
.
When does the process exit? In another word, what’s the process, how did we start it?
The answer is CMD
in a dockerfile or command
in the following expression, which is bash
by default in some images, i.e. ubutu:18.04.
docker run ubuntu:18.04 [command]
回答6:
This happens with images which are exiting immediately, because there is no command that triggers a service waiting for requests, for example.
So you may just run the image in interactive mode. Example below with the node image:
docker run -it node /bin/bash
Output is
root@cacc7897a20c:/# echo $SHELL
/bin/bash
回答7:
In my case , i changed certain file names and directory names of the parent directory of the Dockerfile . Due to which container not finding the required parameters to start it again.
After renaming it back to the original names, container started like butter.