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
If running elevated Docker commands does not fix the permission denied error, verify that your Docker Engine is running. Similar to running a docker command without the sudo command, a stopped Docker Engine triggers the permission denied error. How do you fix the error? By restarting your Docker engine.
Overview. If you receive an error telling you that you do not have permissions to create a directory or to write a file to a directory, this is likely an indication that your script is attempting to write to a directory that you do not own.
The command RUN mkdir -p /var/www/new_directory allows you to create a directory named new_directory inside the Docker file system that we will eventually build using an image built using the above Docker file.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With