1

I have a docker container which keeps restarting;

           Name                         Command                 State                  Ports
--------------------------------------------------------------------------------------------------------
wenotecloudstorage_flask_1   /bin/sh -c /usr/local/bin/ ...   Restarting
wenotecloudstorage_nginx_1   nginx -g daemon off;             Up           0.0.0.0:2083->443/tcp, 80/tcp

I wish I had an easy way to look at the error log on why it is restarting.

I read on Docker: Container keeps on restarting again on again

I try;

docker logs --tail 50 --follow --timestamps wenotecloudstorage_flask_1
error from daemon in stream: Error grabbing logs: EOF

Is there an easy way that I can figure out the reason why a docker container keeps restarting?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • I would say attach to the docker, that will give you information, and of course checking the logs of the docker. – eLRuLL Dec 14 '18 at 16:42
  • Do you mean doing `docker attach wenotecloudstorage_flask_1`? But, that still give me error `You cannot attach to a restarting container, wait until it is running` – Cheok Yan Cheng Dec 14 '18 at 16:45
  • you could stop the container, and they start it without detaching – eLRuLL Dec 14 '18 at 16:46
  • Do you mean first run `docker stop wenotecloudstorage_flask_1`, then followed by `docker start --attach wenotecloudstorage_flask_1`? As, after running `docker start --attach wenotecloudstorage_flask_1`, 0 output is shown in console and the container fall back to Restarting. – Cheok Yan Cheng Dec 14 '18 at 16:51
  • The docker daemon log might provide some insight as well. The location varies based on your system, https://stackoverflow.com/questions/30969435/where-is-the-docker-daemon-log – hellosri Dec 14 '18 at 17:38
  • 1
    If the main container process exits immediately (because of a startup configuration error; because you tried to launch a background process) that could in theory cause this; you might try running it without the `-d` option to get live logs. Seeing the actual `docker-compose.yml` might give some hints. – David Maze Dec 14 '18 at 18:10
  • 1
    @CheokYanCheng also make sure you are not passing the `restart` policy to the containers you run. – eLRuLL Dec 15 '18 at 16:56

3 Answers3

4

Don't use "docker logs". Use "docker-compose logs flask" to see the logs of that restarting containers. With your options:

docker-compose logs -f --tail=50 flask

The error you see is because the normal "docker logs" is trying to read the logs, but the container dies already. Docker-compose handles it better.

If it is not in the logs, well, then you will have to dig deeper in the configuration of your app. But my bet is that you will see a nice log message that will bring you to the right direction.

Moreno
  • 526
  • 2
  • 14
1

Ensure The Command Does Not Exit

Had the same issue, container kept restarting but both docker logs and docker-compose logs just showed me clean logs in a loop, with no clue about the restarts.

Then I realized that indeed there was no error, it was simply exiting because of the command I added to the YML configuration. To verify it, I appended tail -f /dev/null to the last command, e.g.:

command: bash -c "nginx -t  && tail -f /dev/null"

And this time, it stayed up!

Use The Proper Command

With the theory confirmed, I looked up the proper CMD with docker inspect $image_id and used a proper command, e.g.:

command: bash -c "nginx -t && nginx -g 'daemon off;'"

Why Does it Restart

Two possible causes are, there's an error, or the command exits early. In my case, it was the latter. I was previously using an image which already had the correct CMD above. But I wanted to add some extra commands without having to use a separate dockerfile.

So I ditched the dockerfile and used command since the compose file does not have a RUN equivalent (correct me otherwise). However, it "overrides the default command declared by the container image", which was the one preventing the container from exiting early.

How To Identify The Restart Cause

Look at the output of docker ps, I believe that the number in brackets in e.g. "Restarting (1)", is the exit code. You can also run docker-compose in the foreground (not detached, i.e. without -d) so you can see the logs and potential exit codes in real time.

Or run this command:

docker inspect $your_container_name | grep -i state -C 12

Look for the State object:

"State": {
    "Status": "restarting",
    "Running": true,
    "Paused": false,
    "Restarting": true,
    "Dead": false,
    "Pid": 0,
    "ExitCode": 0,
    "Error": "",
    ...
},

The main thing to look for here is the ExitCode. If it's 0, and there's no Error, chances are that the command finishes early, i.e. it's not running as a service. If the code is not 0, there's an actual execution error and you should be able to see it with docker-compose logs or docker logs.

Nagev
  • 10,835
  • 4
  • 58
  • 69
1

This happened to me today and I tried everything but the simplest which solved this.

Just run: docker-compose up --build

Run this without -d If there is a genuine error, it will show you the traceback

Arindam Roychowdhury
  • 5,927
  • 5
  • 55
  • 63