How to properly handle a Docker container as a systemd service?












2















I handle many services (databases, web servers...) implemented by docker containers via systemd units.
It works, but the system takes a very long time to shut down, apparently waiting for docker services to shut down.



Here is an example of a systemd unit I made in a virtual machine under Debian Stretch with Docker CE :



/lib/systemd/system/mariadb.service



[Unit]
Description=MariaDB
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStart=/usr/bin/docker run --rm
--name=mariadb
-p 3306:3306
-e MYSQL_ROOT_PASSWORD=root
-e MYSQL_DATABASE=test
-e MYSQL_USER=user
-e MYSQL_PASSWORD=user
-v /var/lib/mysql:/var/lib/mysql
mariadb:latest
ExecStop=-/usr/bin/docker stop mariadb # See UPDATEs 1 & 2

[Install]
WantedBy=multi-user.target


The service starts and stops properly in seconds when I use systemctl command. But when I shut the system down, it shows :



VM screenshot 1VM screenshot 2VM screenshot 3



It lasts more than 3 minutes. It can be longer if there are many containers running via systemd.



The problem is not specific to MariaDB.



Is there a better method to handle docker containers via systemd, avoiding extra shutdown time ?




  • UPDATE 1 : As Bennett Hardwick suggested in comment, I tried removing the ExecStop directive from the definition of the service. It seems better since the service stops after 1min 30 and not 3min.


  • UPDATE 2 : I tried to add the option -t to docker stop command in ExecStop directive (https://docs.docker.com/engine/reference/commandline/stop/). It has no effect.











share|improve this question




















  • 1





    Does leaving ExecStop blank speed it up at all? I imagine the docker service already handles stopping running containers.

    – Bennett Hardwick
    Feb 16 at 14:57











  • Yes it seems to wait only 1min30 instead of 3min to stop the container.

    – berty
    Feb 16 at 15:02













  • Please add to the post the Dockerfile.

    – harrymc
    Feb 22 at 16:42











  • @harrymc I have no specific Dockerfile to post because 1) my example is based on a docker image which is available via Docker Hub 2) as I mentioned in my question, the problem is not specific to this image. I think you can reproduce it with any container.

    – berty
    Feb 22 at 17:59











  • Have you looked at systemd-docker?

    – harrymc
    Feb 22 at 18:27
















2















I handle many services (databases, web servers...) implemented by docker containers via systemd units.
It works, but the system takes a very long time to shut down, apparently waiting for docker services to shut down.



Here is an example of a systemd unit I made in a virtual machine under Debian Stretch with Docker CE :



/lib/systemd/system/mariadb.service



[Unit]
Description=MariaDB
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStart=/usr/bin/docker run --rm
--name=mariadb
-p 3306:3306
-e MYSQL_ROOT_PASSWORD=root
-e MYSQL_DATABASE=test
-e MYSQL_USER=user
-e MYSQL_PASSWORD=user
-v /var/lib/mysql:/var/lib/mysql
mariadb:latest
ExecStop=-/usr/bin/docker stop mariadb # See UPDATEs 1 & 2

[Install]
WantedBy=multi-user.target


The service starts and stops properly in seconds when I use systemctl command. But when I shut the system down, it shows :



VM screenshot 1VM screenshot 2VM screenshot 3



It lasts more than 3 minutes. It can be longer if there are many containers running via systemd.



The problem is not specific to MariaDB.



Is there a better method to handle docker containers via systemd, avoiding extra shutdown time ?




  • UPDATE 1 : As Bennett Hardwick suggested in comment, I tried removing the ExecStop directive from the definition of the service. It seems better since the service stops after 1min 30 and not 3min.


  • UPDATE 2 : I tried to add the option -t to docker stop command in ExecStop directive (https://docs.docker.com/engine/reference/commandline/stop/). It has no effect.











share|improve this question




















  • 1





    Does leaving ExecStop blank speed it up at all? I imagine the docker service already handles stopping running containers.

    – Bennett Hardwick
    Feb 16 at 14:57











  • Yes it seems to wait only 1min30 instead of 3min to stop the container.

    – berty
    Feb 16 at 15:02













  • Please add to the post the Dockerfile.

    – harrymc
    Feb 22 at 16:42











  • @harrymc I have no specific Dockerfile to post because 1) my example is based on a docker image which is available via Docker Hub 2) as I mentioned in my question, the problem is not specific to this image. I think you can reproduce it with any container.

    – berty
    Feb 22 at 17:59











  • Have you looked at systemd-docker?

    – harrymc
    Feb 22 at 18:27














2












2








2


1






I handle many services (databases, web servers...) implemented by docker containers via systemd units.
It works, but the system takes a very long time to shut down, apparently waiting for docker services to shut down.



Here is an example of a systemd unit I made in a virtual machine under Debian Stretch with Docker CE :



/lib/systemd/system/mariadb.service



[Unit]
Description=MariaDB
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStart=/usr/bin/docker run --rm
--name=mariadb
-p 3306:3306
-e MYSQL_ROOT_PASSWORD=root
-e MYSQL_DATABASE=test
-e MYSQL_USER=user
-e MYSQL_PASSWORD=user
-v /var/lib/mysql:/var/lib/mysql
mariadb:latest
ExecStop=-/usr/bin/docker stop mariadb # See UPDATEs 1 & 2

[Install]
WantedBy=multi-user.target


The service starts and stops properly in seconds when I use systemctl command. But when I shut the system down, it shows :



VM screenshot 1VM screenshot 2VM screenshot 3



It lasts more than 3 minutes. It can be longer if there are many containers running via systemd.



The problem is not specific to MariaDB.



Is there a better method to handle docker containers via systemd, avoiding extra shutdown time ?




  • UPDATE 1 : As Bennett Hardwick suggested in comment, I tried removing the ExecStop directive from the definition of the service. It seems better since the service stops after 1min 30 and not 3min.


  • UPDATE 2 : I tried to add the option -t to docker stop command in ExecStop directive (https://docs.docker.com/engine/reference/commandline/stop/). It has no effect.











share|improve this question
















I handle many services (databases, web servers...) implemented by docker containers via systemd units.
It works, but the system takes a very long time to shut down, apparently waiting for docker services to shut down.



Here is an example of a systemd unit I made in a virtual machine under Debian Stretch with Docker CE :



/lib/systemd/system/mariadb.service



[Unit]
Description=MariaDB
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStart=/usr/bin/docker run --rm
--name=mariadb
-p 3306:3306
-e MYSQL_ROOT_PASSWORD=root
-e MYSQL_DATABASE=test
-e MYSQL_USER=user
-e MYSQL_PASSWORD=user
-v /var/lib/mysql:/var/lib/mysql
mariadb:latest
ExecStop=-/usr/bin/docker stop mariadb # See UPDATEs 1 & 2

[Install]
WantedBy=multi-user.target


The service starts and stops properly in seconds when I use systemctl command. But when I shut the system down, it shows :



VM screenshot 1VM screenshot 2VM screenshot 3



It lasts more than 3 minutes. It can be longer if there are many containers running via systemd.



The problem is not specific to MariaDB.



Is there a better method to handle docker containers via systemd, avoiding extra shutdown time ?




  • UPDATE 1 : As Bennett Hardwick suggested in comment, I tried removing the ExecStop directive from the definition of the service. It seems better since the service stops after 1min 30 and not 3min.


  • UPDATE 2 : I tried to add the option -t to docker stop command in ExecStop directive (https://docs.docker.com/engine/reference/commandline/stop/). It has no effect.








debian docker systemd debian-stretch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 16 at 16:44







berty

















asked Feb 16 at 11:37









bertyberty

113




113








  • 1





    Does leaving ExecStop blank speed it up at all? I imagine the docker service already handles stopping running containers.

    – Bennett Hardwick
    Feb 16 at 14:57











  • Yes it seems to wait only 1min30 instead of 3min to stop the container.

    – berty
    Feb 16 at 15:02













  • Please add to the post the Dockerfile.

    – harrymc
    Feb 22 at 16:42











  • @harrymc I have no specific Dockerfile to post because 1) my example is based on a docker image which is available via Docker Hub 2) as I mentioned in my question, the problem is not specific to this image. I think you can reproduce it with any container.

    – berty
    Feb 22 at 17:59











  • Have you looked at systemd-docker?

    – harrymc
    Feb 22 at 18:27














  • 1





    Does leaving ExecStop blank speed it up at all? I imagine the docker service already handles stopping running containers.

    – Bennett Hardwick
    Feb 16 at 14:57











  • Yes it seems to wait only 1min30 instead of 3min to stop the container.

    – berty
    Feb 16 at 15:02













  • Please add to the post the Dockerfile.

    – harrymc
    Feb 22 at 16:42











  • @harrymc I have no specific Dockerfile to post because 1) my example is based on a docker image which is available via Docker Hub 2) as I mentioned in my question, the problem is not specific to this image. I think you can reproduce it with any container.

    – berty
    Feb 22 at 17:59











  • Have you looked at systemd-docker?

    – harrymc
    Feb 22 at 18:27








1




1





Does leaving ExecStop blank speed it up at all? I imagine the docker service already handles stopping running containers.

– Bennett Hardwick
Feb 16 at 14:57





Does leaving ExecStop blank speed it up at all? I imagine the docker service already handles stopping running containers.

– Bennett Hardwick
Feb 16 at 14:57













Yes it seems to wait only 1min30 instead of 3min to stop the container.

– berty
Feb 16 at 15:02







Yes it seems to wait only 1min30 instead of 3min to stop the container.

– berty
Feb 16 at 15:02















Please add to the post the Dockerfile.

– harrymc
Feb 22 at 16:42





Please add to the post the Dockerfile.

– harrymc
Feb 22 at 16:42













@harrymc I have no specific Dockerfile to post because 1) my example is based on a docker image which is available via Docker Hub 2) as I mentioned in my question, the problem is not specific to this image. I think you can reproduce it with any container.

– berty
Feb 22 at 17:59





@harrymc I have no specific Dockerfile to post because 1) my example is based on a docker image which is available via Docker Hub 2) as I mentioned in my question, the problem is not specific to this image. I think you can reproduce it with any container.

– berty
Feb 22 at 17:59













Have you looked at systemd-docker?

– harrymc
Feb 22 at 18:27





Have you looked at systemd-docker?

– harrymc
Feb 22 at 18:27










1 Answer
1






active

oldest

votes


















0














A solution which is no longer maintained is
systemd-docker,
described as:




This is a wrapper for docker run so that you can sanely run Docker
containers under systemd. The key thing that this wrapper does is move
the container process from the cgroups setup by Docker to the service
unit's cgroup. This handles a bunch of other quirks so please read
through documentation to get an understanding of all the implications
of running Docker under systemd.



Using this wrapper you can manage containers through systemctl or the
docker CLI and everything should just stay in sync. Additionally you
can leverage all the cgroup functionality of systemd and
systemd-notify.




The problem of getting systemd-docker to work with recent Linux versions is
discussed in the post
Doesn't work with recent systemd and/or docker releases ,
where a user named james-cxx has reported success:




I was able to get systemd-docker working with Ubuntu 18.04 by:




  • Building systemd-docker from scratch from @agend07 's fork, per @agend07 and @Halfwalker
    discussed above.

  • Adding --cgroups name=systemd just after systemd-docker in the unit file, per
    https://container-solutions.com/running-docker-containers-with-systemd/


My guess is that docker defaults to not using systemd for cgroups
because "the delegate issues still exists and systemd currently does
not support the cgroup feature set required for containers run by
docker" (per the docker.service unit file), and I expect
systemd-docker is expecting systemd for the cgroups, hence the open
/sys/fs/cgroup/system.slice/docker.service/cgroup.procs: no such file
or directory
error. Setting --cgroups name=systemd apparently
overrides the docker default, however, I cannot say what side-effects
this may have, given the ominous note in the docker.service unit file.




An alternative to systemd-docker might be to use
rkt, described as:




rkt is an application container engine developed for modern production
cloud-native environments. It features a pod-native approach, a
pluggable execution environment, and a well-defined surface area that
makes it ideal for integration with other systems.







share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1406441%2fhow-to-properly-handle-a-docker-container-as-a-systemd-service%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    A solution which is no longer maintained is
    systemd-docker,
    described as:




    This is a wrapper for docker run so that you can sanely run Docker
    containers under systemd. The key thing that this wrapper does is move
    the container process from the cgroups setup by Docker to the service
    unit's cgroup. This handles a bunch of other quirks so please read
    through documentation to get an understanding of all the implications
    of running Docker under systemd.



    Using this wrapper you can manage containers through systemctl or the
    docker CLI and everything should just stay in sync. Additionally you
    can leverage all the cgroup functionality of systemd and
    systemd-notify.




    The problem of getting systemd-docker to work with recent Linux versions is
    discussed in the post
    Doesn't work with recent systemd and/or docker releases ,
    where a user named james-cxx has reported success:




    I was able to get systemd-docker working with Ubuntu 18.04 by:




    • Building systemd-docker from scratch from @agend07 's fork, per @agend07 and @Halfwalker
      discussed above.

    • Adding --cgroups name=systemd just after systemd-docker in the unit file, per
      https://container-solutions.com/running-docker-containers-with-systemd/


    My guess is that docker defaults to not using systemd for cgroups
    because "the delegate issues still exists and systemd currently does
    not support the cgroup feature set required for containers run by
    docker" (per the docker.service unit file), and I expect
    systemd-docker is expecting systemd for the cgroups, hence the open
    /sys/fs/cgroup/system.slice/docker.service/cgroup.procs: no such file
    or directory
    error. Setting --cgroups name=systemd apparently
    overrides the docker default, however, I cannot say what side-effects
    this may have, given the ominous note in the docker.service unit file.




    An alternative to systemd-docker might be to use
    rkt, described as:




    rkt is an application container engine developed for modern production
    cloud-native environments. It features a pod-native approach, a
    pluggable execution environment, and a well-defined surface area that
    makes it ideal for integration with other systems.







    share|improve this answer




























      0














      A solution which is no longer maintained is
      systemd-docker,
      described as:




      This is a wrapper for docker run so that you can sanely run Docker
      containers under systemd. The key thing that this wrapper does is move
      the container process from the cgroups setup by Docker to the service
      unit's cgroup. This handles a bunch of other quirks so please read
      through documentation to get an understanding of all the implications
      of running Docker under systemd.



      Using this wrapper you can manage containers through systemctl or the
      docker CLI and everything should just stay in sync. Additionally you
      can leverage all the cgroup functionality of systemd and
      systemd-notify.




      The problem of getting systemd-docker to work with recent Linux versions is
      discussed in the post
      Doesn't work with recent systemd and/or docker releases ,
      where a user named james-cxx has reported success:




      I was able to get systemd-docker working with Ubuntu 18.04 by:




      • Building systemd-docker from scratch from @agend07 's fork, per @agend07 and @Halfwalker
        discussed above.

      • Adding --cgroups name=systemd just after systemd-docker in the unit file, per
        https://container-solutions.com/running-docker-containers-with-systemd/


      My guess is that docker defaults to not using systemd for cgroups
      because "the delegate issues still exists and systemd currently does
      not support the cgroup feature set required for containers run by
      docker" (per the docker.service unit file), and I expect
      systemd-docker is expecting systemd for the cgroups, hence the open
      /sys/fs/cgroup/system.slice/docker.service/cgroup.procs: no such file
      or directory
      error. Setting --cgroups name=systemd apparently
      overrides the docker default, however, I cannot say what side-effects
      this may have, given the ominous note in the docker.service unit file.




      An alternative to systemd-docker might be to use
      rkt, described as:




      rkt is an application container engine developed for modern production
      cloud-native environments. It features a pod-native approach, a
      pluggable execution environment, and a well-defined surface area that
      makes it ideal for integration with other systems.







      share|improve this answer


























        0












        0








        0







        A solution which is no longer maintained is
        systemd-docker,
        described as:




        This is a wrapper for docker run so that you can sanely run Docker
        containers under systemd. The key thing that this wrapper does is move
        the container process from the cgroups setup by Docker to the service
        unit's cgroup. This handles a bunch of other quirks so please read
        through documentation to get an understanding of all the implications
        of running Docker under systemd.



        Using this wrapper you can manage containers through systemctl or the
        docker CLI and everything should just stay in sync. Additionally you
        can leverage all the cgroup functionality of systemd and
        systemd-notify.




        The problem of getting systemd-docker to work with recent Linux versions is
        discussed in the post
        Doesn't work with recent systemd and/or docker releases ,
        where a user named james-cxx has reported success:




        I was able to get systemd-docker working with Ubuntu 18.04 by:




        • Building systemd-docker from scratch from @agend07 's fork, per @agend07 and @Halfwalker
          discussed above.

        • Adding --cgroups name=systemd just after systemd-docker in the unit file, per
          https://container-solutions.com/running-docker-containers-with-systemd/


        My guess is that docker defaults to not using systemd for cgroups
        because "the delegate issues still exists and systemd currently does
        not support the cgroup feature set required for containers run by
        docker" (per the docker.service unit file), and I expect
        systemd-docker is expecting systemd for the cgroups, hence the open
        /sys/fs/cgroup/system.slice/docker.service/cgroup.procs: no such file
        or directory
        error. Setting --cgroups name=systemd apparently
        overrides the docker default, however, I cannot say what side-effects
        this may have, given the ominous note in the docker.service unit file.




        An alternative to systemd-docker might be to use
        rkt, described as:




        rkt is an application container engine developed for modern production
        cloud-native environments. It features a pod-native approach, a
        pluggable execution environment, and a well-defined surface area that
        makes it ideal for integration with other systems.







        share|improve this answer













        A solution which is no longer maintained is
        systemd-docker,
        described as:




        This is a wrapper for docker run so that you can sanely run Docker
        containers under systemd. The key thing that this wrapper does is move
        the container process from the cgroups setup by Docker to the service
        unit's cgroup. This handles a bunch of other quirks so please read
        through documentation to get an understanding of all the implications
        of running Docker under systemd.



        Using this wrapper you can manage containers through systemctl or the
        docker CLI and everything should just stay in sync. Additionally you
        can leverage all the cgroup functionality of systemd and
        systemd-notify.




        The problem of getting systemd-docker to work with recent Linux versions is
        discussed in the post
        Doesn't work with recent systemd and/or docker releases ,
        where a user named james-cxx has reported success:




        I was able to get systemd-docker working with Ubuntu 18.04 by:




        • Building systemd-docker from scratch from @agend07 's fork, per @agend07 and @Halfwalker
          discussed above.

        • Adding --cgroups name=systemd just after systemd-docker in the unit file, per
          https://container-solutions.com/running-docker-containers-with-systemd/


        My guess is that docker defaults to not using systemd for cgroups
        because "the delegate issues still exists and systemd currently does
        not support the cgroup feature set required for containers run by
        docker" (per the docker.service unit file), and I expect
        systemd-docker is expecting systemd for the cgroups, hence the open
        /sys/fs/cgroup/system.slice/docker.service/cgroup.procs: no such file
        or directory
        error. Setting --cgroups name=systemd apparently
        overrides the docker default, however, I cannot say what side-effects
        this may have, given the ominous note in the docker.service unit file.




        An alternative to systemd-docker might be to use
        rkt, described as:




        rkt is an application container engine developed for modern production
        cloud-native environments. It features a pod-native approach, a
        pluggable execution environment, and a well-defined surface area that
        makes it ideal for integration with other systems.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 24 at 14:33









        harrymcharrymc

        264k14273581




        264k14273581






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1406441%2fhow-to-properly-handle-a-docker-container-as-a-systemd-service%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

            Eduardo VII do Reino Unido