解決Docker中CentOS鏡像無法使用systemd的問題:Failed to get D-Bus connection: Operation not permitted

解決Docker中CentOS鏡像無法使用systemd的問題

  1. 創建DockerFile

    mkdir /root/centos-systemd
    cd /root/centos-systemd
    vim DockerFile
    
    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/*;
    VOLUME [ "/sys/fs/cgroup" ]
    CMD ["/usr/sbin/init"]
    
    ESC
    :wq
  2. 下載鏡像並命名爲centos-systemd:7

    cd /root/centos-systemd
    docker build --rm -t centos-systemd:7 .
  3. 通過centos-systemd:7鏡像生成一個名爲centos-systemd的容器

    docker run -d -i -t --privileged=true --name centos-systemd -v /sys/fs/cgroup:/sys/fs/cgroup:ro centos-systemd:7
  4. 連接到名爲centos-systemd的容器

    docker exec -i -t centos-systemd /bin/bash
  5. 測試systemd啓動是否成功

    systemctl --help
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章