I am new to docker, hence may be missing a simple piece. Here is my scenario. I started a container with command 'docker run -it ubuntu:14.04'. Then with Ctrl+P+Q, I exited such that the container keeps running. I verified with docker ps, and saw the container running. Then I again entered the container with 'docker exec -it bash. This took me inside the container again. Now on typing 'exit' command, I come out of the container, but the container is still in running mode. Normally with exit command, the container stops. Any idea why this is happening?
The container's running status is tied to the initial process that it was created for/with.
If you do docker run then this will create a new container with some inital process. When that process terminates, the whole container is stopped. If that initial process was bash, and you exit it, then this terminates the container itself.
docker exec starts a new process inside of the running container. When that process terminates, the container still keeps running.
Typing exit into an interactive bash shell will just exit that shell. It will not affect other processes running inside the same container (just like closing one terminal window in your host OS does not affect any other processes).
With the exit command in your case, the container stops only the /bin/bash/ executable. Probably some other application like NGINX or Apache is running inside the container and does not let it shut down.
Related
I am running docker container Nginx, there are some errors in it,
I cannot exec into the container because it is stopped, how can I exec into the stopped container.
How to avoid the container stopping, if there exists an error in the container
Can someone help me by answering the above?
It seems like the normal execution within your container causes it to stop. So what you need to do is, create a container with an overridden entrypoint (the procedure/command that is executed on container startup).
A good place to start is by creating a shell instance where you can look around, and maybe even execute the same command manually for debugging purposes.
So let's say I have an image testimage:latest that on startup executes /bin/my_script.sh, which fails.
I can then start a container with a shell instance
$ docker run --entrypoint sh -it testimage:latest
And within that container I can run the script, and check the output
in_container$ /bin/my_script.sh
I cannot exec into the container because it is stopped, how can I exec into the stopped container.
No, you cannot exec onto a stopped container, you'd need to start the
container up again before being able to exec onto it.
How to avoid the container stopping, if there exists an error in the container
As far as I am aware there is nothing to prevent a container stopping when there are errors, however I have found How to prevent a container from shutting down during an error? which might help you with what you need (please give them credit if it does work).
For example I run
docker run --rm --name mycontainer -p 8080:8080 myrepo/myimage
then I see an output of my application, everything is OK. Then I press the Ctrl+C but the container is still running and I'm forced to explicitly stop and remove it:
docker rm -f <container_id>
Or even worse:
docker stop <container_id>
docker rm <container_id>
Is there any way to do it automatically? If not it's OK.
PS: What is the purpose of all that stopped containers still kept on the harddrive?!
What is the purpose of all that stopped containers still kept on the harddrive?!
Running containers include the process you are running along with the namespaced environment to run that process inside of (networking, pid, filesystem, etc).
A stopped container has the container specific read/write filesystem layer, any configuration you included to run the container (e.g. environment variables), and logs if you are using the json logging driver (default).
Removing a container deletes that RW filesystem layer, json logs, and the configuration, which also removes the container from the list of stopped containers. This is a permanent operation, so do not remove containers you may want to later inspect or restart.
I press the Ctrl+C but the container is still running and I'm forced to explicitly stop and remove it
First, make sure you are running a current version of docker. I believe somewhere around 1.13 they migrated the processing of the --rm option from the client to the server. Next, make sure your application handles the Ctrl+C command. In a shell script, this would be a handler for a SIGTERM. You also need run the container interactively so that the keyboard input is sent to the container, that is the -it flag. With all three of those done, you should see containers automatically cleaned up with:
docker run --rm -it --name mycontainer -p 8080:8080 myrepo/myimage
followed by a Ctrl+C. The -it will pass the SIGTERM to the container which should then stop the process, which stops the running container. And the --rm will result in the container being automatically removed.
If for some reason you cannot get your container to handle the SIGTERM, then you can send a SIGKILL with a docker kill command, which cannot be trapped and ignored by the application.
Note that if you run a docker stop on your container and see a 10 second delay before it is stopped, then your application is ignoring the SIGTERM. One common cause for this is a /bin/sh running as pid 1. A shell will ignore this signal when it's running as pid 1 by default, on the assumption that you are in signal user mode.
Per default, docker runs the image command as pid 1. pid 1 is handled special by the kernel as it normally is used for the system init process. For this reason, CTRL+C / SIGTERM does not work on pid 1.
Actual docker versions provide option --init to run a minimal init system (tini) as pid 1. Your image command runs as a child of tini. As no longer being pid 1, your image command will accept CTRL+C again.
Add --init to your sample command, and you can stop with CTRL+C
docker run --rm --init --name mycontainer -p 8080:8080 myrepo/myimage
Not needed in your case, just additional info: You can change the signal from docker stop with --stop-signal SIGNAL with SIGNAL being one of the many signals shown by kill -L, for example SIGHUP or SIGINT.
I am running a docker container which is trying to access a port in another docker container. Both of these are running are configured together to run on the same network. But as soon as I start this container it gets killed and doesn't throw any error. There are no error logs. I also tried using docker inspect but couldn't find much.
PS: I am a newbie docker user.
Following from OP comment w/ ENTRYPOINT
ENTRYPOINT /configure.sh && bash
Answer
Given your ENTRYPOINT the container will always exit since the process is bash. You need to have a continuously running process in the foreground for the container to stay running i.e. an application daemon.
I would like to run docker exec on some, possibly stopped docker container.
By possibly I mean that it could be running just fine but in some cases, namely server reboot and so on, container that I want run docker exec would be stopped.
Is there any good way to make sure that docker exec would execute without error in both cases (container running, container stopped). And in case of stopped wouldn't return:
Error response from daemon: Container is not running
From docker exec --help
you can find, among other things
Run a command in a running container
I do not know what you want to do with a stopped container?
Maybe try to restart it?
You know you can start a container with a restart policy to always
see the doc
https://docs.docker.com/engine/reference/run/
Extract
Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
You can't run docker exec against a stopped container. From docker help exec:
$ docker help exec
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
So if your target container has been stopped by some reason, you need to start it by docker start <your_container> before you can do docker exec ....
BTW, docker run command has an option called --restart to let you to specify a restart policy for the container, you can find more details on docker run --restart docs. There're 4 policies available:
no: Do not automatically restart the container when it exits. This is the default.
on-failure[:max-retries]: Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
always: Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
unless-stopped: Always restart the container regardless of the exit status, but do not start it on daemon startup if the container has been put to a stopped state before.
By default it's no, you could choose another one based on your requirement. For example if you choose non-stopped, your container will got restarted automatically when docker daemon is ready after your server reboot.
I want to interactively execute a Docker container.
I have created it from an image like this
docker create 80597e5353eb
Which outputs an ID:
7372c2d60d513431026a40c50d6f1433e1bc62c57bca4086587193b24c329535
Then I do docker start on that ID:
docker start 7372c2d60d51
But then, when I try to execute the container:
docker exec -it 7372c2d60d51 /bin/bash
I get:
Error response from daemon: Container 7372c2d60d513431026a40c50d6f1433e1bc62c57bca4086587193b24c329535 is not running
Why? I just started the container and it didn't throw any error.
Why is it not started?
1. Why does the Docker Container Exit if there is No Error
There does not need to be an error for a docker container to exit.
The docker container will exit (stop running) if the code running inside finishes executing.
For example, you could have made a container that printed "hello world". As soon as the container prints the hello world phrase, it will exit.
An example of a continuously running application would be a web server listening on a port.
2. Why Does the container Not Start
The container is probably starting. However, the container is stopping quickly.
You use the following command to view all the created containers and what their running states.
docker ps -a
You may see something similar to the below (notice the "Exited" Status):