Search code examples
bashdockerscriptingcontainerssystemd

docker systemd: how i can automatically start a container while setting up a service in systemd (BASH)


I will run a docker container with the command docker run -ti --rm -p 8080:80 -v $(pwd)/my/path/to/config myimage:latest but the plan is to write a function in a script, that start these image, while starting the VM in systemd. Do I have to set up a service in /lib/systemd/system ?


Solution

  • From the docker website's documentation,

    https://docs.docker.com/config/containers/start-containers-automatically/

    You can use a restart policy, and parameters are,

    Use a restart policy To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:

    Flag
    Description

    no
    Do not automatically restart the container. (the default)

    on-failure
    Restart the container if it exits due to an error, which manifests as a non-zero exit code.

    always
    Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)

    unless-stopped
    Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.

    If these do not work for your requirements, then there is information about you can use a process manager such as upstart, systemd, or supervisor lower down the webpage.