在Dockerfile中启动Gunicorn服务:无法获得D-Bus连接:不允许操作

时间:2017-08-04 11:26:26

标签: docker docker-compose dockerfile gunicorn systemd

我试图用我的dockerfile启动服务(gunicorn,nginx),但我收到了这个错误。

这是我的dockerfile

FROM centos:centos7

RUN yum -y install epel-release
RUN yum -y --enablerepo=base clean metadata
RUN yum -y install nginx
RUN yum -y install python-pip
RUN pip install --upgrade pip
RUN yum -y install systemd;
RUN yum clean all;

COPY . /

RUN pip install --no-cache-dir -r requirements.txt

RUN ./manage.py makemigrations
RUN ./manage.py migrate

ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;

#Gunicorn
RUN cp gunicorn_systemd  /etc/systemd/system/gunicorn.service
RUN systemctl start gunicorn
RUN systemctl enable gunicorn

这是我的构建命令

  

docker build -t guni ./

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

You are trying to interact with systemd in your build script:

RUN systemctl start gunicorn

There are a number of problems here. First, trying to "start" a service as part of the build process doesn't make any sense: you're building an image, not starting a container.

Secondly, you're trying to interact with systemd, but you're never starting systemd, and it is unlikely that you want to [1]. Since a docker container is typically a "single process" environment, you don't need any init-like process supervisor to start things for you. You just need to arrange to run the necessary command yourself.

Taking Apache httpd as an example, rather than running:

systemctl start httpd

You would run:

httpd -DFOREGROUND

This runs the webserver and ensures that it stays in the foreground (the Docker container will exit when the foreground process exits). You can surely do something similar with gunicorn.

Your container is also missing a CMD or ENTRYPOINT directive, so it's not going to do anything when you run it unless you provide an explicit command, and that's probably not the behavior you want.

[1] If you really think you need systemd, you would need to arrange to start it when the container starts (e.g, CMD /sbin/init), but systemd is not something that runs well in an unprivileged container environment. It's possible but I wouldn't recommend it.