How to know the reason why a docker container exits?

docker container exits immediately
what happens to docker container when the process it is running exits
docker container stops immediately after starting
docker exit code 137
docker run
docker exit code 0
docker exit codes
docker logs

I have a Docker container running in a host of 1G RAM (there are also other containers running in the same host). The application in this Docker container will decode some images, which may consume memory a lot.

From time to time, this container will exit. I doubt it is due to out of memory but not very sure. I need a method to find the root cause. So is there any way to know what happened for this container's death?

Others have mentioned docker logs $container_id to view the output of the application. This would always be my first thing to check.

Next, you can run a docker inspect $container_id to view details on the state, e.g.:

    "State": {
        "Status": "exited",
        "Running": false,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 0,
        "ExitCode": 2,
        "Error": "",
        "StartedAt": "2016-06-28T21:26:53.477229071Z",
        "FinishedAt": "2016-06-28T21:26:53.478066987Z"
    },

The important line there is "OOMKilled" which will be true if you exceed the container memory limits and Docker kills your app. You may also want to lookup the exit code to see if it identifies a cause for the exit by your app.

Note, this only indicates if docker itself kills your process, and requires that you have set a memory limit on your container. Outside of docker, the Linux kernel can lol your process if the host itself runs out of memory. Linux often writes to a log in /var/log when this happens. With Docker Desktop on Windows and Mac, you can adjust the memory allocated to the embedded Linux VM in the docker settings.

Why docker container exits immediately, A docker container exits when its main process finishes. In this case it will exit when your start-all.sh script ends. I don't know enough about� I think the best way to check where it exits from is first creating a yaml file out of that docker run command and then doing a docker-compose up without the -d. this will continue logging all the messages from the container until it exits and maybe will stop there(sometimes it continues to log for those containers that have a restarting problem).

You can find out whether the process inside the container was OOMkilled by reading the logs. OOMkills are initiated by the kernel so every time it happens there's a bunch of lines in /var/log/kern.log, for example:

python invoked oom-killer: gfp_mask=0x14000c0(GFP_KERNEL), nodemask=(null), order=0, oom_score_adj=995
oom_kill_process+0x22e/0x450
Memory cgroup out of memory: Kill process 31204 (python) score 1994 or sacrifice child
Killed process 31204 (python) total-vm:7350860kB, anon-rss:4182920kB, file-rss:2356kB, shmem-rss:0kB

Find out Why Your Docker Container Keeps Crashing � vsupalov.com, Still, the exit code you keep seeing is 1. And there's no useful output in the terminal. How close are you to stripping the Dockerfile back to Alpine and starting over? The Exit Code. If you haven’t checked it yet, this might be a first hint. You can either try to run your container without the -d flag, or check the exit code of a stopped container with $ docker ps -a and finding the most recent one in the output. Depending on the exit code, you might have something useful to work with. Nope, The Exit Code is not Useful

While the accepted answer is the best option, sometimes it can be useful to inspect from the host the content of the journal too (on linux).

You can do that by typing:

sudo journalctl -u docker

or tailing it

sudo journalctl -u docker -f

or piping the output to less if the it is too long for your terminal buffer

journalctl -xn -u docker | less

How to know the reason why my docker container exits?, I think the best way to check where it exits from is first creating a yaml file out of that docker run command and then doing a docker-compose up without the -d. docker ps -a | grep hello-world. You’ll notice that the container exited and the exit code is 0. This is because the container does not have any foreground process attached, such as a Java

Container exits with non-zero exit code 137, If a container is no longer running, use the following command to find the status of the container: docker container ls -a. This article explains possible reasons for � list stopped containers: docker ps -a. copy the name or the container id of the container you want to attach to, and start the container with: docker start -i <name/id> The -i flag tells docker to attach to the container's stdin. If the container wasn't started with an interactive shell to connect to, you need to do this to run a shell:

What is the difference between dead and exited containers?, Normally a graceful exit is expected, as you would expect when running a short- lived Note: Causes of dead containers are not limited to the above example. The reason it exits is because the shell script is run first as PID 1 and when that's complete, PID 1 is gone, and docker only runs while PID 1 is. You can use supervisor to do everything, if run with the "-n" flag it's told not to daemonize, so it will stay as the first process:

Understanding Docker Container Exit Codes, The most common exit codes, what they mean, and what causes them this question is to identify the exit code for the docker container. restart the Container and perhaps add the parameter--restart always For instance portainer is started like this in my Hyper-V Windows Server 2016 Core: docker run -d --restart always --network=tlan --ip 172.21.31.3 --name portainer -h portainer -v C:\ProgramData\Containers\Portainer:C:\Data portainer/portainer Palle

Comments
  • You can check the logs for that container via docker logs <container-id>.
  • but the container has exited, i guess I can not logs it any more ?
  • Just tried on my machine. You can still access the logs even when the container has exited.
  • Did you at least try?
  • techtabu, yes I did . It doesn't help anyway
  • I don't understand here is since my container is gone, how the "inspect" will work ? From discussion above, once the app dies , the container will dies too. You mean restart the same image then inspect ?
  • @LiBin a container don't get wipe away when it dies, it simply gets to a halt state like status= stopped or exited. 'docker ps -a' and see for yourself
  • I was getting exit 0 every time running a memory intensive operation and OOMKilled was false. Increasing memory made it working again.
  • This can happen if the Linux kernel, rather than the docker engine, kills processes in the container. You will often see that in the OS logs under /var/log on the host.