Hi,
This is more of a question rather than issue. I run some containers the only job of those containers is to keep running and people can exec into it and execute some commands. To keep the containers running as of now i use below
command: top -b
This works great and keeps container running but also floods the log. I wanted to know if there is a better way to do this?
Regards,
Tarun
You could use tail -f /dev/null
, or sleep infinity
if the container's OS supports it.
The only problem is that neither of those commands handles SIGINT properly, so they won't respond to stop
in a timely manner.
So probably a shell script with abort trap and sleep combination could do better?
Yes. Something might already exist on the Hub for this - I'm not sure.
Sounds like this was resolved.
Why do I get the error:
/entrypoint.sh: line 101: /tail -f /dev/null: No such file or directory
when I use (as suggested):
tail -f /dev/null
Looks like your image has an entrypoint
script that is doing something strange. Try setting the entrypoint to sh -c
.
Add -ti to command.
Example : docker run -ti
Or add -tty to docker compose file . Reference:https://docs.docker.com/compose/compose-file/
For people coming here later (probably me), here's how you get the container to stop gracefully. In your entrypoint script:
#!/bin/sh
trap : TERM INT
tail -f /dev/null & wait
The tail
goes at the end of your script, the trap
can go anywhere, but usually is one of the first lines.
@blopker wha you send tail
to background and wait
for it, why not just call tail
I don't think the trap will work unless you're in wait
.
I just did
docker service create --name my_app {configs} image:tag tail -f /dev/null
After this I was able to bash in to debug why my container was not coming up
@blopker For those of us who aren't crazy bash wizards could you elaborate on the solution you provided? It works for me but I'd like to understand what exactly is going on.
From what I understand trap : TERM INT
basically means "catch"(or trap) all TERM
and INT
signals(both are commands that gracefully kill the process) and do nothing(the :
is a "do nothing" command). Do we add this to prevent docker from stopping the container immediately?
Then as for the second line tail -f /dev/null & wait
, tail
looks at the end of a file and the -f
flag will watch it for changes. Then the file option is null
? This doesn't make a whole lot of sense. I assume it's a placeholder command for us to execute and then wait
after it executes?
So I found this a while ago, but it made sense at the time. If I remember correctly, it's not supposed to be used on its own so there's some built-in safe guards for other traps that may have been set. The :
in the trap is treated as true
in all POSIX shells. It's there in case something else, another script or program, set up its own trap before this one. The empty trap clears out the other old ones and ensures the signals belong to this script.
After it ensures the signals aren't going to trigger something else, the tail
just there so we can use wait
, which is a small program that is known to handle signals well and generally does the right thing as its signal handling behavior is defined in POSIX: http://man7.org/linux/man-pages/man2/waitpid.2.html
tail
will never finish as it's following an empty device that never returns anything throws away anything it gets (/dev/null
).
Anyway, it's likely something else will work just fine for your use case. I think this script is just trying to account for unclean environments and be POSIX compatible.
tail will still causes some file operations from time to time.
I use the following one-liner to keep a container alive
# Ah, ha, ha, ha, stayin' alive...
while :; do :; done & kill -STOP $! && wait $!
Most helpful comment
You could use
tail -f /dev/null
, orsleep infinity
if the container's OS supports it.The only problem is that neither of those commands handles SIGINT properly, so they won't respond to
stop
in a timely manner.