问下docker,如何能用特权的方式启动容器?

是这样的,我在docker里面安装了nginx,然后我启动nginx
systemctl start nginx 报错
Failed to get D-Bus connection: Operation not permitted

后我查了下,是需要用特权的方式启动,但是都是重新run启动镜像重新生成一个容器,但现在我这个容器我已经安装了很多东西了,我不太想再重新生成一个容器又要重新安装软件,问下有没有方式用特权的方式启动容器,或者不用启动,能解决上面的报错也可以?

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

现在大多数 Linux 发行版都使用 Systemd 管理服务的守护进程。而在 Docker Container 中则不需要这种东西来管理 Container 的生命周期!所以在 Container 中,Nginx 必须以非 Daemon 的方式运行!

当然也有使用 Systemd 来管理 Docker Container 的,例如 ETCD 的部署生成器中 的例子:

cat > /tmp/s1.service <<EOF
[Unit]
Description=etcd with Docker
Documentation=https://github.com/coreos/etcd

[Service]
Restart=always
RestartSec=5s
TimeoutStartSec=0
LimitNOFILE=40000

ExecStart=/usr/bin/docker \
  run \
  --rm \
  --net=host \
  --name etcd-v3.3.8 \
  --volume=/tmp/etcd/s1:/etcd-data \
  --volume=${HOME}/certs:/etcd-ssl-certs-dir \
  gcr.io/etcd-development/etcd:v3.3.8 \
  /usr/local/bin/etcd \
  --name s1 \
  --data-dir /etcd-data \
  --listen-client-urls https://localhost:2379 \
  --advertise-client-urls https://localhost:2379 \
  --listen-peer-urls https://localhost:2380 \
  --initial-advertise-peer-urls https://localhost:2380 \
  --initial-cluster s1=https://localhost:2380,s2=https://localhost:22380,s3=https://localhost:32380 \
  --initial-cluster-token tkn \
  --initial-cluster-state new \
  --client-cert-auth \
  --trusted-ca-file /etcd-ssl-certs-dir/etcd-root-ca.pem \
  --cert-file /etcd-ssl-certs-dir/s1.pem \
  --key-file /etcd-ssl-certs-dir/s1-key.pem \
  --peer-client-cert-auth \
  --peer-trusted-ca-file /etcd-ssl-certs-dir/etcd-root-ca.pem \
  --peer-cert-file /etcd-ssl-certs-dir/s1.pem \
  --peer-key-file /etcd-ssl-certs-dir/s1-key.pem

ExecStop=/usr/bin/docker stop etcd-v3.3.8

[Install]
WantedBy=multi-user.target
EOF

如果要使用 Systemd 来管理 Container 中的 Nginx,则启动容器就不要加 --detach 或 -d 选项!

Docker 官方文档中有提到如何在容器中运行多个服务,但是不推荐这么做!

用 Docker Compose 编排你的服务更简单,且更安全!

1年前 评论
讨论数量: 12

好像要把init进程作为主进程来着

1年前 评论
donggan (楼主) 1年前

还有其他需求吗 就是为了启动 nginx?

1年前 评论
donggan (楼主) 1年前
php_yt (作者) 1年前
php_yt (作者) 1年前
donggan (楼主) 1年前
php_yt (作者) 1年前
JaguarJack

启动容器加上 --privileged=true 这个参数试试看

1年前 评论
donggan (楼主) 1年前

github 搜索 dnmp 那个docker 镜像

1年前 评论

现在大多数 Linux 发行版都使用 Systemd 管理服务的守护进程。而在 Docker Container 中则不需要这种东西来管理 Container 的生命周期!所以在 Container 中,Nginx 必须以非 Daemon 的方式运行!

当然也有使用 Systemd 来管理 Docker Container 的,例如 ETCD 的部署生成器中 的例子:

cat > /tmp/s1.service <<EOF
[Unit]
Description=etcd with Docker
Documentation=https://github.com/coreos/etcd

[Service]
Restart=always
RestartSec=5s
TimeoutStartSec=0
LimitNOFILE=40000

ExecStart=/usr/bin/docker \
  run \
  --rm \
  --net=host \
  --name etcd-v3.3.8 \
  --volume=/tmp/etcd/s1:/etcd-data \
  --volume=${HOME}/certs:/etcd-ssl-certs-dir \
  gcr.io/etcd-development/etcd:v3.3.8 \
  /usr/local/bin/etcd \
  --name s1 \
  --data-dir /etcd-data \
  --listen-client-urls https://localhost:2379 \
  --advertise-client-urls https://localhost:2379 \
  --listen-peer-urls https://localhost:2380 \
  --initial-advertise-peer-urls https://localhost:2380 \
  --initial-cluster s1=https://localhost:2380,s2=https://localhost:22380,s3=https://localhost:32380 \
  --initial-cluster-token tkn \
  --initial-cluster-state new \
  --client-cert-auth \
  --trusted-ca-file /etcd-ssl-certs-dir/etcd-root-ca.pem \
  --cert-file /etcd-ssl-certs-dir/s1.pem \
  --key-file /etcd-ssl-certs-dir/s1-key.pem \
  --peer-client-cert-auth \
  --peer-trusted-ca-file /etcd-ssl-certs-dir/etcd-root-ca.pem \
  --peer-cert-file /etcd-ssl-certs-dir/s1.pem \
  --peer-key-file /etcd-ssl-certs-dir/s1-key.pem

ExecStop=/usr/bin/docker stop etcd-v3.3.8

[Install]
WantedBy=multi-user.target
EOF

如果要使用 Systemd 来管理 Container 中的 Nginx,则启动容器就不要加 --detach 或 -d 选项!

Docker 官方文档中有提到如何在容器中运行多个服务,但是不推荐这么做!

用 Docker Compose 编排你的服务更简单,且更安全!

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!