繁体   English   中英

如何使用 Ubuntu18.04 在 Dockerfile 上启用 systemd

[英]How to enable systemd on Dockerfile with Ubuntu18.04

我知道不建议在 Docker 容器上使用 Systemd,但这可能吗?

我在使用 Ansible 部署的 Ubuntu 18.04 云虚拟机上有暂存/生产环境;

我当前的开发环境是 Ubuntu 18.04 Vagrantfile ,它使用相同的 Ansible playbook.yml of staging/prod

现在我正在尝试用Vagrantfile替换Dockerfile进行开发,但是 Ansible playbook.yml在应用 systemd 模块时失败。 我也想在我的开发环境中安装systemd ,以便我可以在我的playbook.yml本地测试更改。 知道我该怎么做吗?

如果我尝试使用Dockerfileplaybook.yml进行构建,如下所示,我会收到错误Failed to find required executable systemctl in paths

如果我将RUN apt-get install systemd添加到Dockerfile尝试构建,我会收到错误消息System has not been booted with systemd as init system

示例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

示例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

样品inventory

docker-dev ansible_host=localhost ansible_connection=local

这是应该使用docker-systemctl-replacement脚本的完美示例。

它被开发为允许ansible脚本同时针对虚拟机和docker容器。 您无需启用真实的systemd,只需在否则受systemd控制的操作系统中覆盖/ usr / bin / systemctl。 然后,docker容器将对ansible足够好看,而我更习惯于使用常规的“ service:”模块,而不是特定的“ systemd:”模块。

如果它是一个选项,您还可以从已启用systemd的 docker 映像开始,因为它可用于 ubuntu 18.04另请参见此处。

这是一个示例 dockerfile,我们从这个图像开始并安装 python3.8 以满足我们的应用程序需求:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM