How to enable systemd on Dockerfile with Ubuntu18.04











up vote
0
down vote

favorite












I know Systemd is not recommended on Docker containers but is it possible?



I have staging/prod environments on Ubuntu 18.04 cloud VMs deployed with Ansible;



My current dev environment is a Ubuntu 18.04 Vagrantfile that uses the same Ansible playbook.yml of staging/prod



Now I'm trying to replace the Vagrantfile with a Dockerfile for development but the Ansible playbook.yml fails when applying systemd modules. I would like to have systemd on my dev environment as well so that I can test changes on my playbook.yml local. Any idea how I can do it?



If I try to build with Dockerfile and playbook.yml as below, I get an error Failed to find required executable systemctl in paths.



If I add RUN apt-get install systemd to Dockerfile nd try to build, I get an error System has not been booted with systemd as init system



Sample Dockerfile:



FROM ubuntu:18.04

ADD . /app
WORKDIR /app

# Install Python3 pip used to install Ansible
RUN apt-get update && apt-get install -y
python3-pip

# Install Ansible
RUN pip3 install --trusted-host pypi.python.org ansible
RUN ansible-playbook playbook.yml -i inventory

EXPOSE 80


Sample playbook.yml:



---
- name: Ansible playbook to setup dev environment
hosts: all
vars:
ansible_python_interpreter: "/usr/bin/python3"
debug: True
become: yes
become_method: sudo
tasks:
- name: Copy App Gunicorn systemd config
template:
src: app_gunicorn.service
dest: /etc/systemd/system/

- name: Enable App Gunicorn on systemd
systemd: state=started name=app_gunicorn


Sample inventory:



docker-dev ansible_host=localhost ansible_connection=local









share|improve this question


























    up vote
    0
    down vote

    favorite












    I know Systemd is not recommended on Docker containers but is it possible?



    I have staging/prod environments on Ubuntu 18.04 cloud VMs deployed with Ansible;



    My current dev environment is a Ubuntu 18.04 Vagrantfile that uses the same Ansible playbook.yml of staging/prod



    Now I'm trying to replace the Vagrantfile with a Dockerfile for development but the Ansible playbook.yml fails when applying systemd modules. I would like to have systemd on my dev environment as well so that I can test changes on my playbook.yml local. Any idea how I can do it?



    If I try to build with Dockerfile and playbook.yml as below, I get an error Failed to find required executable systemctl in paths.



    If I add RUN apt-get install systemd to Dockerfile nd try to build, I get an error System has not been booted with systemd as init system



    Sample Dockerfile:



    FROM ubuntu:18.04

    ADD . /app
    WORKDIR /app

    # Install Python3 pip used to install Ansible
    RUN apt-get update && apt-get install -y
    python3-pip

    # Install Ansible
    RUN pip3 install --trusted-host pypi.python.org ansible
    RUN ansible-playbook playbook.yml -i inventory

    EXPOSE 80


    Sample playbook.yml:



    ---
    - name: Ansible playbook to setup dev environment
    hosts: all
    vars:
    ansible_python_interpreter: "/usr/bin/python3"
    debug: True
    become: yes
    become_method: sudo
    tasks:
    - name: Copy App Gunicorn systemd config
    template:
    src: app_gunicorn.service
    dest: /etc/systemd/system/

    - name: Enable App Gunicorn on systemd
    systemd: state=started name=app_gunicorn


    Sample inventory:



    docker-dev ansible_host=localhost ansible_connection=local









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I know Systemd is not recommended on Docker containers but is it possible?



      I have staging/prod environments on Ubuntu 18.04 cloud VMs deployed with Ansible;



      My current dev environment is a Ubuntu 18.04 Vagrantfile that uses the same Ansible playbook.yml of staging/prod



      Now I'm trying to replace the Vagrantfile with a Dockerfile for development but the Ansible playbook.yml fails when applying systemd modules. I would like to have systemd on my dev environment as well so that I can test changes on my playbook.yml local. Any idea how I can do it?



      If I try to build with Dockerfile and playbook.yml as below, I get an error Failed to find required executable systemctl in paths.



      If I add RUN apt-get install systemd to Dockerfile nd try to build, I get an error System has not been booted with systemd as init system



      Sample Dockerfile:



      FROM ubuntu:18.04

      ADD . /app
      WORKDIR /app

      # Install Python3 pip used to install Ansible
      RUN apt-get update && apt-get install -y
      python3-pip

      # Install Ansible
      RUN pip3 install --trusted-host pypi.python.org ansible
      RUN ansible-playbook playbook.yml -i inventory

      EXPOSE 80


      Sample playbook.yml:



      ---
      - name: Ansible playbook to setup dev environment
      hosts: all
      vars:
      ansible_python_interpreter: "/usr/bin/python3"
      debug: True
      become: yes
      become_method: sudo
      tasks:
      - name: Copy App Gunicorn systemd config
      template:
      src: app_gunicorn.service
      dest: /etc/systemd/system/

      - name: Enable App Gunicorn on systemd
      systemd: state=started name=app_gunicorn


      Sample inventory:



      docker-dev ansible_host=localhost ansible_connection=local









      share|improve this question













      I know Systemd is not recommended on Docker containers but is it possible?



      I have staging/prod environments on Ubuntu 18.04 cloud VMs deployed with Ansible;



      My current dev environment is a Ubuntu 18.04 Vagrantfile that uses the same Ansible playbook.yml of staging/prod



      Now I'm trying to replace the Vagrantfile with a Dockerfile for development but the Ansible playbook.yml fails when applying systemd modules. I would like to have systemd on my dev environment as well so that I can test changes on my playbook.yml local. Any idea how I can do it?



      If I try to build with Dockerfile and playbook.yml as below, I get an error Failed to find required executable systemctl in paths.



      If I add RUN apt-get install systemd to Dockerfile nd try to build, I get an error System has not been booted with systemd as init system



      Sample Dockerfile:



      FROM ubuntu:18.04

      ADD . /app
      WORKDIR /app

      # Install Python3 pip used to install Ansible
      RUN apt-get update && apt-get install -y
      python3-pip

      # Install Ansible
      RUN pip3 install --trusted-host pypi.python.org ansible
      RUN ansible-playbook playbook.yml -i inventory

      EXPOSE 80


      Sample playbook.yml:



      ---
      - name: Ansible playbook to setup dev environment
      hosts: all
      vars:
      ansible_python_interpreter: "/usr/bin/python3"
      debug: True
      become: yes
      become_method: sudo
      tasks:
      - name: Copy App Gunicorn systemd config
      template:
      src: app_gunicorn.service
      dest: /etc/systemd/system/

      - name: Enable App Gunicorn on systemd
      systemd: state=started name=app_gunicorn


      Sample inventory:



      docker-dev ansible_host=localhost ansible_connection=local






      docker ubuntu systemd






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 22:15









      vmenezes

      238214




      238214
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          That's a perfect example where the docker-systemctl-replacement script should be used.



          It has been developed to allow ansible scripts to target both virtual machines and docker containers. You do not need to enable a real systemd, just overwrite /usr/bin/systemctl in operating systems that are otherwise under systemd control. The docker container will then look good enough for ansible, whereas I am more used to use the general 'service:' module instead of the specific 'systemd:' module.






          share|improve this answer





















          • That's why the docker-systemctl-replacement script can be also installed as the docker-entrypoint script - it will then start all enabled services in the container. Upon "docker stop" it will also "systemctl stop" all those services correctly before exiting the container itself. (Effectively, systemctl.py is both a service manager and an init-daemon).
            – Guido U. Draheim
            Nov 20 at 12:17










          • Ah, I hadn't realized from a brief glance that it would actually parse systemd units and emulate the various settings in order to provide the appropriate environment. Neat!
            – larsks
            Nov 20 at 12:19










          • By the way, some working examples can be found at github.com/gdraheim/docker-systemctl-images
            – Guido U. Draheim
            Nov 20 at 12:21










          • The suggested docker-systemctl-replacement worked perfectly! All I had to do was to download systemctl3.py from that repo and add it to my Dockerfile as COPY deploy/environments/dev-docker/files/systemctl3.py /usr/bin/systemctl and RUN test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl
            – vmenezes
            Nov 20 at 19:43













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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',
          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%2fstackoverflow.com%2fquestions%2f53383431%2fhow-to-enable-systemd-on-dockerfile-with-ubuntu18-04%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








          up vote
          1
          down vote



          accepted










          That's a perfect example where the docker-systemctl-replacement script should be used.



          It has been developed to allow ansible scripts to target both virtual machines and docker containers. You do not need to enable a real systemd, just overwrite /usr/bin/systemctl in operating systems that are otherwise under systemd control. The docker container will then look good enough for ansible, whereas I am more used to use the general 'service:' module instead of the specific 'systemd:' module.






          share|improve this answer





















          • That's why the docker-systemctl-replacement script can be also installed as the docker-entrypoint script - it will then start all enabled services in the container. Upon "docker stop" it will also "systemctl stop" all those services correctly before exiting the container itself. (Effectively, systemctl.py is both a service manager and an init-daemon).
            – Guido U. Draheim
            Nov 20 at 12:17










          • Ah, I hadn't realized from a brief glance that it would actually parse systemd units and emulate the various settings in order to provide the appropriate environment. Neat!
            – larsks
            Nov 20 at 12:19










          • By the way, some working examples can be found at github.com/gdraheim/docker-systemctl-images
            – Guido U. Draheim
            Nov 20 at 12:21










          • The suggested docker-systemctl-replacement worked perfectly! All I had to do was to download systemctl3.py from that repo and add it to my Dockerfile as COPY deploy/environments/dev-docker/files/systemctl3.py /usr/bin/systemctl and RUN test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl
            – vmenezes
            Nov 20 at 19:43

















          up vote
          1
          down vote



          accepted










          That's a perfect example where the docker-systemctl-replacement script should be used.



          It has been developed to allow ansible scripts to target both virtual machines and docker containers. You do not need to enable a real systemd, just overwrite /usr/bin/systemctl in operating systems that are otherwise under systemd control. The docker container will then look good enough for ansible, whereas I am more used to use the general 'service:' module instead of the specific 'systemd:' module.






          share|improve this answer





















          • That's why the docker-systemctl-replacement script can be also installed as the docker-entrypoint script - it will then start all enabled services in the container. Upon "docker stop" it will also "systemctl stop" all those services correctly before exiting the container itself. (Effectively, systemctl.py is both a service manager and an init-daemon).
            – Guido U. Draheim
            Nov 20 at 12:17










          • Ah, I hadn't realized from a brief glance that it would actually parse systemd units and emulate the various settings in order to provide the appropriate environment. Neat!
            – larsks
            Nov 20 at 12:19










          • By the way, some working examples can be found at github.com/gdraheim/docker-systemctl-images
            – Guido U. Draheim
            Nov 20 at 12:21










          • The suggested docker-systemctl-replacement worked perfectly! All I had to do was to download systemctl3.py from that repo and add it to my Dockerfile as COPY deploy/environments/dev-docker/files/systemctl3.py /usr/bin/systemctl and RUN test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl
            – vmenezes
            Nov 20 at 19:43















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          That's a perfect example where the docker-systemctl-replacement script should be used.



          It has been developed to allow ansible scripts to target both virtual machines and docker containers. You do not need to enable a real systemd, just overwrite /usr/bin/systemctl in operating systems that are otherwise under systemd control. The docker container will then look good enough for ansible, whereas I am more used to use the general 'service:' module instead of the specific 'systemd:' module.






          share|improve this answer












          That's a perfect example where the docker-systemctl-replacement script should be used.



          It has been developed to allow ansible scripts to target both virtual machines and docker containers. You do not need to enable a real systemd, just overwrite /usr/bin/systemctl in operating systems that are otherwise under systemd control. The docker container will then look good enough for ansible, whereas I am more used to use the general 'service:' module instead of the specific 'systemd:' module.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 12:08









          Guido U. Draheim

          1,169119




          1,169119












          • That's why the docker-systemctl-replacement script can be also installed as the docker-entrypoint script - it will then start all enabled services in the container. Upon "docker stop" it will also "systemctl stop" all those services correctly before exiting the container itself. (Effectively, systemctl.py is both a service manager and an init-daemon).
            – Guido U. Draheim
            Nov 20 at 12:17










          • Ah, I hadn't realized from a brief glance that it would actually parse systemd units and emulate the various settings in order to provide the appropriate environment. Neat!
            – larsks
            Nov 20 at 12:19










          • By the way, some working examples can be found at github.com/gdraheim/docker-systemctl-images
            – Guido U. Draheim
            Nov 20 at 12:21










          • The suggested docker-systemctl-replacement worked perfectly! All I had to do was to download systemctl3.py from that repo and add it to my Dockerfile as COPY deploy/environments/dev-docker/files/systemctl3.py /usr/bin/systemctl and RUN test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl
            – vmenezes
            Nov 20 at 19:43




















          • That's why the docker-systemctl-replacement script can be also installed as the docker-entrypoint script - it will then start all enabled services in the container. Upon "docker stop" it will also "systemctl stop" all those services correctly before exiting the container itself. (Effectively, systemctl.py is both a service manager and an init-daemon).
            – Guido U. Draheim
            Nov 20 at 12:17










          • Ah, I hadn't realized from a brief glance that it would actually parse systemd units and emulate the various settings in order to provide the appropriate environment. Neat!
            – larsks
            Nov 20 at 12:19










          • By the way, some working examples can be found at github.com/gdraheim/docker-systemctl-images
            – Guido U. Draheim
            Nov 20 at 12:21










          • The suggested docker-systemctl-replacement worked perfectly! All I had to do was to download systemctl3.py from that repo and add it to my Dockerfile as COPY deploy/environments/dev-docker/files/systemctl3.py /usr/bin/systemctl and RUN test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl
            – vmenezes
            Nov 20 at 19:43


















          That's why the docker-systemctl-replacement script can be also installed as the docker-entrypoint script - it will then start all enabled services in the container. Upon "docker stop" it will also "systemctl stop" all those services correctly before exiting the container itself. (Effectively, systemctl.py is both a service manager and an init-daemon).
          – Guido U. Draheim
          Nov 20 at 12:17




          That's why the docker-systemctl-replacement script can be also installed as the docker-entrypoint script - it will then start all enabled services in the container. Upon "docker stop" it will also "systemctl stop" all those services correctly before exiting the container itself. (Effectively, systemctl.py is both a service manager and an init-daemon).
          – Guido U. Draheim
          Nov 20 at 12:17












          Ah, I hadn't realized from a brief glance that it would actually parse systemd units and emulate the various settings in order to provide the appropriate environment. Neat!
          – larsks
          Nov 20 at 12:19




          Ah, I hadn't realized from a brief glance that it would actually parse systemd units and emulate the various settings in order to provide the appropriate environment. Neat!
          – larsks
          Nov 20 at 12:19












          By the way, some working examples can be found at github.com/gdraheim/docker-systemctl-images
          – Guido U. Draheim
          Nov 20 at 12:21




          By the way, some working examples can be found at github.com/gdraheim/docker-systemctl-images
          – Guido U. Draheim
          Nov 20 at 12:21












          The suggested docker-systemctl-replacement worked perfectly! All I had to do was to download systemctl3.py from that repo and add it to my Dockerfile as COPY deploy/environments/dev-docker/files/systemctl3.py /usr/bin/systemctl and RUN test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl
          – vmenezes
          Nov 20 at 19:43






          The suggested docker-systemctl-replacement worked perfectly! All I had to do was to download systemctl3.py from that repo and add it to my Dockerfile as COPY deploy/environments/dev-docker/files/systemctl3.py /usr/bin/systemctl and RUN test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl
          – vmenezes
          Nov 20 at 19:43




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fstackoverflow.com%2fquestions%2f53383431%2fhow-to-enable-systemd-on-dockerfile-with-ubuntu18-04%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'