简体   繁体   English

Docker 未启动,卷权限被拒绝错误

[英]Docker not starting, volume permission denied error

i am trying create postgresql container with this command in windows with WSL 2 integration enabled我正在尝试在启用 WSL 2 集成的情况下在 windows 中使用此命令创建 postgresql 容器

docker run -d --name pg-docker --restart=always --publish 5432:5432 \
  -e POSTGRES_PASSWORD=postgres \
  -v "C:\Program Files\Docker\Volumes\pg:/var/lib/postgresql/data" \
 postgres:alpine

but container not starting, keeps restarting and I get permission error:但容器没有启动,不断重启,我得到权限错误:

chmod: /var/lib/postgresql/data: Permission denied. chmod: /var/lib/postgresql/data: 权限被拒绝。

i tried to change chmod permissions but I cant enter bash due to container not running我尝试更改 chmod 权限,但由于容器未运行,我无法输入 bash

Use extreme caution with these options.使用这些选项时要格外小心。 Bind-mounting a system directory such as /home or /usr with the Z option renders your host machine inoperable and you may need to relabel the host machine files by hand.使用 Z 选项绑定安装系统目录(例如 /home 或 /usr)会使您的主机无法操作,您可能需要手动重新标记主机文件。

Use the below command as it worked well for me.使用以下命令,因为它对我来说效果很好。

docker run -d --name pg-docker --restart=always \
  --publish 5432:5432 -e POSTGRES_PASSWORD=postgres \
  -v "C:\Program Files\Docker\Volumes\pg:/var/lib/postgresql/data:Z" \
  postgres:alpine

Reason: Host volume settings are not portable, since they are host-dependent and might not work on any other machine.原因:主机卷设置不可移植,因为它们依赖于主机并且可能无法在任何其他机器上工作。 Also, be aware that the host system has no knowledge of container SELinux policy.另外,请注意主机系统不了解容器 SELinux 策略。 Therefore, if SELinux policy is enforced, the mounted host directory is not writable to the container.因此,如果强制执行 SELinux 策略,则挂载的主机目录不可写入容器。

Work around this by assigning the proper SELinux policy type to the host directory:通过将正确的 SELinux 策略类型分配给主机目录来解决此问题:

chcon -Rt svirt_sandbox_file_t host_dir

Where host_dir is a path to the directory on host system that is mounted to the container.其中 host_dir 是主机系统上挂载到容器的目录的路径。

The above command (with:Z) will automatically do the chcon -Rt svirt_sandbox_file_t /var/lib/postgresql/data上面的命令(with:Z)会自动执行chcon -Rt svirt_sandbox_file_t /var/lib/postgresql/data

See https://docs.docker.com/storage/bind-mounts/#configure-the-selinux-label请参阅https://docs.docker.com/storage/bind-mounts/#configure-the-selinux-label

Source: Permission denied on accessing host directory in Docker来源: 访问 Docker 中主机目录的权限被拒绝

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

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