How to create directory using Dockerfile

Hi Team,

I am trying to build Jenkins images using Dockerfile. I need to create a “/maven” directory under existing “/var” directory for my requirement. Hence, I have added the following command in the Dockerfile. When I run the docker build command, I am getting “Permission Denied” error.

RUN mkdir -p /var/maven/

output:
=> ERROR [5/6] RUN mkdir -p /var/maven/ 0.4s

[5/6] RUN mkdir -p /var/maven/:
#9 0.359 mkdir: cannot create directory ‘/var/maven/’: Permission denied


executor failed running [/bin/sh -c mkdir -p /var/maven/]: exit code: 1

I have tried multiple combinations like remove “/” at the end or making “/var” as Working directory but still the issue exists.

May I know what am I doing wrong?

Thanks,
Bala

If the default user was changed in the base image, you will not have permission to write /var. In that case you need to set the user to be “root” write files and change to user back again.

FROM baseimage

USER 0

RUN mkdir -p /var/maven

USER $CONTAINER_USER_ID

In the above example “0” means “root” and $CONTAINER_USER_ID can be replaced with the original ID. If you don’t know what it is, check the base image’s Dockerfile (if you have access to it) or run

docker image history baseimagename | grep USER

If you don’t know the ID of the user, you can use its name too.