New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --rm=dead or --rm=always to ease systemd integration #7245
Comments
@virtuald The issue here is --rm is a feature of the CLI. |
Hm, the underlying daemon doesn't implement --rm? That explains why -d and --rm aren't compatible (I haven't dived into docker's source code yet). Still, having that type of functionality available from the CLI would be desirable, as that's how one writes unit files at the moment. Each one of the actions above have equivalent shell script implementations using various cli commands. For things already interacting with docker directly via socket, I imagine it's less of a hassle to add the extra code to do these types of things. |
+1 on this. I was trying to manage docker containers with supervisord, and it's like a pain in ass since it's so easy to leave dead container behind. Even you always run docker with --rm, it doesn't help at all. For example:
however, since 5050 port is already in use, so you got an error messages like this:
It looks like docker run client code didn't clean the dead container for you, simply because it encountered an error. And then supervisord tries to start the process again, it would never succeed since the dead container is not removed. You need to remove the container manually to keep the system running. This breaks my ansible automatic deployment, I always need to clean dead container manually, this is super annoying. In the mean time, a possible workaround is to have a script runs I think @virtuald is right, the
It does not only breaks the promise when starting a docker with error, it also does this when you reboot the machine, and I believe there are just way too many cases |
hmmmm, I just realized that it looks like cid files will disappear after reboot. I think I have to make my script more complex, looking into running containers for solving this issue :/ |
@victorlin we are running into the same frustrations managing docker containers with supervisord: https://gist.github.com/rgarcia/dfdf41844668d4cc13bc Going to try using your script. |
The way I resolve this is by having an |
Why did all work on this stall out? I followed all the links and there is a lot of discussion, a lot of PRs, etc... and it all ends with comments like "So why was this closed?" I also share the massive frustration of |
@cjbottaro have a look at #20848, which moves the |
Ahh, thank you. Sometimes it's hard to navigate all the issues/pr links. Here's my workaround in case anyone is interested in the meantime. It's a #!/usr/bin/env python
import os
import sys
import subprocess
try:
i = sys.argv.index("--name")
name = sys.argv[i+1]
except ValueError:
name == None
if "--rm" in sys.argv and name:
with open(os.devnull, "w") as devnull:
subprocess.call(["docker", "rm", name], stdout=devnull, stderr=devnull)
# sys.argv includes the program name (this script), so we copy and replace it.
# Note that the args must include "docker" as the first arg, despite it
# being redundant in the execvp call.
args = ["docker", "run"] + sys.argv[1:]
# docker run ...
os.execvp("docker", args) Note that it only works when you use |
I'm tentatively closing as it should be solved by #20848. Please confirm and ping us to reopen if that's not the case! |
AFAIK, running a docker container 'properly' from systemd is very tricky, because of things like --rm and start/run/environment and other things. Various people have complained about this a bit, and various solutions exist -- all of which involve running a large shell script in your systemd unit file. I think I would really want the preferred way to run a container from systemd to be simple and not require shell scripting.
What about adding additional options to run --rm?
--rm=true/false
: same as before--rm=dead
: if the name doesn't exist, do same as--rm=true
. If a container with the name exists, removes the prior container if it was dead/not running. Raises an error if the current container is already running. Would make Remove container after failure with --force-rm #6786 largely unnecessary--rm=always
: runs the equivalent ofdocker kill C; docker rm C; docker run --rm C
. See Option to override container name #4212. Honestly, I don't know if this should be added, you could get yourself into trouble with this.The reason something like
--rm=dead
is required is because I cannot always guarantee that my container will be killed on reboot, such as during a power failure. Lacking an --rm=dead functionality, I have to write a shell script to do things like stop/rm/etc before I start my container.The 'proper' way of integrating throwaway containers with systemd would be greatly simplified, and look something like this:
The text was updated successfully, but these errors were encountered: