Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable systemd on Dockerfile with Ubuntu18.04

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
like image 833
vmenezes Avatar asked Jan 01 '23 14:01

vmenezes


2 Answers

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.

like image 175
Guido U. Draheim Avatar answered Jan 04 '23 06:01

Guido U. Draheim


If its an option you can also start from a docker image with systemdalready enabled as this one available for ubuntu 18.04, and see also here.

Here is an example dockerfile where we start from this image and install python3.8 for our app needs:

FROM jrei/systemd-ubuntu


# INSTALL PYTHON
RUN apt-get update -q -y
RUN apt-get install -q -y python3.8 python3-distutils curl libpq-dev build-essential python3.8-dev
RUN rm /usr/bin/python3
RUN ln -s /usr/bin/python3.8 /usr/bin/python3
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
RUN python3.8 get-pip.py
RUN pip3.8 install --upgrade pip
RUN pip3.8 install -q -r requirements.txt

RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 10
ENV PYTHONPATH "${PYTHONPATH}:."

### then setting the app needs and entrypoint
like image 41
SARA E Avatar answered Jan 04 '23 05:01

SARA E