无法创建目录.权限在Docker容器内被拒绝 [英] Cannot create directory. Permission denied inside docker container
问题描述
在将非root用户添加到sudoers组的映像构建过程中,无法创建文件夹.这是我的Dockerfile:
Can not create folder during image building with non root user added to sudoers group. Here is my Dockerfile:
FROM ubuntu:16.04
RUN apt-get update && \
apt-get -y install sudo
RUN adduser --disabled-password --gecos '' newuser \
&& adduser newuser sudo \
&& echo '%sudo ALL=(ALL:ALL) ALL' >> /etc/sudoers
USER newuser
RUN mkdir -p /newfolder
WORKDIR /newfolder
我收到错误消息:mkdir: cannot create directory '/newfolder': Permission denied
推荐答案
Docker容器内的文件系统就像Docker容器外的文件系统一样工作:如果要创建文件或目录,则需要适当的权限.在这种情况下,您试图以非root用户身份创建/newfolder
(因为USER
指令更改了用于运行其后所有命令的UID).这是行不通的,因为/
由root
拥有并且具有模式dr-xr-xr-x
.
Filesystems inside a Docker container work just like filesytems outside a Docker container: you need appropriate permissions if you are going to create files or directories. In this case, you're trying to create /newfolder
as a non-root user (because the USER
directive changes the UID used to run any commands that follow it). That won't work because /
is owned by root
and has mode dr-xr-xr-x
.
请尝试:
RUN mkdir -p /newfolder
RUN chown newuser /newfolder
USER newuser
WORKDIR /newfolder
这会将目录创建为root
,然后将其创建为chown
.
This will create the directory as root
, and then chown
it.
这篇关于无法创建目录.权限在Docker容器内被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!