Cannot create directory. Permission denied inside docker container

Viewed 171789

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

4 Answers

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).

I actually had this problem after I had created a [root] user and done xyz. When I went to re-build my docker (even with force recreate), it was giving me all sorts of permission denied errors. Security wasn't an issue for me so simply adding

FROM ubuntu:latest

USER root

ENV TZ=Europe/Kiev
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update && apt-get install...

Then doing my new user code

RUN useradd...

I know this is not the perfect answer but I hope it helps!

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

Related