Home > Blockchain >  How to create directory in docker image?
How to create directory in docker image?

Time:08-19

I tried mkdir -p it didn't work.

I have the following Dockerfile:

FROM jenkins/jenkins:2.363-jdk11

ENV PLUGIN_DIR /var/jenkins_home/plugins

RUN echo $PLUGIN_DIR

RUN mkdir -p $PLUGIN_DIR

RUN ls $PLUGIN_DIR

# WORKDIR /var/jenkins_home/plugins # Can't use this, as it changes the permission to root
# which breaks the plugin installation step
# # COPY plugins.txt /usr/share/jenkins/plugins.txt
# # RUN jenkins-plugin-cli -f /usr/share/jenkins/plugins.txt --verbose
#
#
# # disable the setup wizard as we will set up jenkins as code 
# ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false
#
# ENV CASC_JENKINS_CONFIG /configs/jcasc.yaml

The build fails!

docker build -t jenkins:test.1 .
Sending build context to Docker daemon   51.2kB
Step 1/5 : FROM jenkins/jenkins:2.363-jdk11
 ---> 90ff7cc5bfd1
Step 2/5 : ENV PLUGIN_DIR /var/jenkins_home/plugins
 ---> Using cache
 ---> 0a158958aab0
Step 3/5 : RUN echo $PLUGIN_DIR
 ---> Running in ce56ef9146fc
/var/jenkins_home/plugins
Step 4/5 : RUN mkdir -p $PLUGIN_DIR
 ---> Using cache
 ---> dbc4e12b9808
Step 5/5 : RUN ls $PLUGIN_DIR
 ---> Running in 9a0edb027862

I need this because Jenkins deprecated old plugin installation method. The new cli installs plugins to /usr/share/jenkins/ref/plugins instead.

Also:

 $ docker run -it --rm --entrypoint /bin/bash --name jenkins jenkins:test.1
jenkins@7ad71925f638:/$ ls /var/jenkins_home/
jenkins@7ad71925f638:/$

CodePudding user response:

The official Jenkins image on dockerhub declare VOLUME /var/jenkins_home, and subsequent changes to that directory (even in derived images) are discarded.

To workaround, you can execute mkdir as ENTRYPOINT. And to verify that its working you can add an sleep to enter into the container and verify. It work !.

FROM jenkins/jenkins:2.363-jdk11

ENV PLUGIN_DIR /var/jenkins_home/plugins

RUN echo $PLUGIN_DIR

USER root 

RUN echo "#!/bin/sh \n mkdir -pv $PLUGIN_DIR && sleep inf" > ./mkdir.sh

RUN chmod a x ./mkdir.sh

USER jenkins

ENTRYPOINT [ "/bin/sh", "-c", "./mkdir.sh"] 

after

  • docker build . -t <image_name>
  • docker run -d <image_name> --name <container_name>
  • docker exec -it <container_name> bash

and you will see your directory

Sources:

  • Related