Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 235
Vadim Kovrizhkin Avatar asked Aug 07 '17 18:08

Vadim Kovrizhkin


People also ask

How do I fix permission denied Docker?

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.

Why I Cannot create a directory permission denied?

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.

How do I create a directory in Docker?

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.


2 Answers

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.

like image 118
larsks Avatar answered Sep 21 '22 13:09

larsks


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

like image 21
Kahitarich Avatar answered Sep 21 '22 13:09

Kahitarich