Unable to create directory. Permission denied inside docker container

Unable to create folder during image creation with 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 an error: mkdir: cannot create directory '/newfolder': Permission denied

+17


source to share


2 answers


Filesystems inside a Docker container work the same way as files outside a Docker container: you need appropriate permissions if you are going to create files or directories. In this case, you are trying to create /newfolder

as a non-root user (since the directive USER

changes the UID used to run any subsequent commands). It won't work because it /

belongs root

and has a regime dr-xr-xr-x

.

Try this instead:



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

      

This will create a directory like root

and then chown

it.

+27


source


docker cp some_dir mysql:/opt

      



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

0


source







All Articles