Vadim  Kovrizhkin
Vadim Kovrizhkin

Reputation: 1781

Cannot create directory. Permission denied inside docker container

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

Upvotes: 112

Views: 307000

Answers (5)

GCUGreyArea
GCUGreyArea

Reputation: 151

This is an old question, but I've just encountered the same issue. One thing to remember is that before switching to the created user, everything is running as root. However, I still can't get past the permission problem. My solution seems like a bit of a hack, but it works...

FROM ubuntu:latest
RUN apt-get update \
  && apt-get install -y python3 build-essential make sudo git cmake

RUN useradd -ms /bin/bash barry && echo '%sudo ALL=(ALL:ALL) ALL' >> /etc/sudoers
WORKDIR /home/barry/rotate
RUN cd /home/barry \
    && git clone https://github.com/google/googletest.git \
    && cd googletest \
    && mkdir -p build \
    && cd build \
    && cmake .. \
    && cmake --build . \
    && cmake --install .

COPY . /home/barry/rotate

RUN cd /home/barry/rotate && make && make test
RUN find /home/barry -name "*" -exec chown barry {} \;
USER barry
ENV PATH=$PATH:/home/barry/rotate/build:/home/barry/rotate/test/build
...

Upvotes: 0

lator
lator

Reputation: 325

You can enter the shell of the docker as a root user and change the folder ownership:

docker exec -u root -t -i <container-id> /bin/sh

Create a new folder:

mkdir -p /newfolder

Change ownership and permissions:

chown new-user:new-user /newfolder
chmod 755 /newfolder

Upvotes: 0

luke Wiskeos
luke Wiskeos

Reputation: 1

What worked for me is running chmod 777 on the directory that the docker container is in. Since your new container is a new user, it does not have permission to make sub directories on what would also be your local machine, so chmod 777 gives that permission

Upvotes: -10

Kahitarich
Kahitarich

Reputation: 495

Here is a process that worked for me to create folder as with non-user permissions

FROM solr:8
USER root
RUN mkdir /searchVolume
RUN chown solr:solr /searchVolume
USER solr

The last line drops the login back to solr (or whatever user you have).

Upvotes: 30

larsks
larsks

Reputation: 311238

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.

Upvotes: 100

Related Questions