Start a service in docker container failed,with error: Failed to get D-Bus connection: No connection to service manager

前端 未结 2 869
面向向阳花
面向向阳花 2021-02-04 12:27

I installed docker image and built a image successfully.

When I ssh to the container and run the command service xxx start, an error popped:

2条回答
  •  再見小時候
    2021-02-04 12:59

    I've managed to fix this issue in a CentOS:7 Docker container. I've followed mainly the Guide on CentOS Docker image project.

    FROM centos:7
    
    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/*;
    
    # Install anything. The service you want to start must be a SystemD service.
    
    CMD ["/usr/sbin/init"]
    

    Now, build the image, and run it using at least the following arguments to docker run command: -v /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro

    Then main point is that /usr/sbin/init must be the first process inside the Docker container.

    So if you want to use a custom script that executes some commands before running /usr/sbin/init, launch it at the end of your script using exec /usr/sbin/init (in a bash script).

    Here is an example:

    ADD cmd.sh /usr/local/bin/
    RUN chmod +x /usr/local/bin/cmd.sh
    
    CMD ["/usr/local/bin/cmd.sh"]
    

    And here is the content of cmd.sh:

    #!/bin/bash
    
    # Do some stuffs
    
    exec /usr/sbin/init # To correctly start D-Bus thanks to https://forums.docker.com/t/any-simple-and-safe-way-to-start-services-on-centos7-systemd/5695/8
    

    You could have System is booting up. See pam_nologin(8) if your using the PAM system, in that case, delete /usr/lib/tmpfiles.d/systemd-nologin.conf in your Dockerfile because it creates the file /var/run/nologin which generates this specific error.

提交回复
热议问题