Cannot create directory. Permission denied inside docker container

眉间皱痕 提交于 2020-04-07 15:06:14

问题


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

I get error: mkdir: cannot create directory '/newfolder': Permission denied


回答1:


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.

Try instead:

RUN mkdir -p /newfolder
RUN chown newuser /newfolder
USER newuser
WORKDIR /newfolder

This will create the directory as root, and then chown it.




回答2:


docker cp some_dir mysql:/opt

Instead of creating a directory copy a empty directory or file to that path in the container using docker cp.



来源:https://stackoverflow.com/questions/45553074/cannot-create-directory-permission-denied-inside-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!