5

I have Jenkins running in a Docker container. The home directory is in a host volume, in order to ensure that the build history is preserved when updates to the container are actioned.

I have updated the container, to create an additional file in the home directory. When the new container is pulled, I cannot see the changed file.

ENV JENKINS_HOME=/var/jenkins_home
RUN mkdir -p ${JENKINS_HOME}/.m2
COPY settings.xml ${JENKINS_HOME}/.m2/settings.xml
RUN chown -R jenkins:jenkins ${JENKINS_HOME}/.m2
VOLUME ["/var/jenkins_home"]

I am running the container like this:

docker run -v /host/directory:/var/jenkins_home -p 80:8080 jenkins

I had previous run Jenkins and so the home directory already exists on the host. When I pull the new container and run it, I see that the file .m2/settings.xml is not created. Why is this please?

John
  • 10,837
  • 17
  • 78
  • 141
  • have you tried to watch inside the container if the directory is successfully build? are you sure you rebuild the container correctly? are there any errors on the build process? e.g. that the file cannot be copied because the directory doesnt exist? – Gabbax0r May 23 '17 at 09:28
  • The `docker build` command runs to completion ok; there are no errors reported during the build. What do you mean watch inside the container? I have run `docker exec -it jenkins /bin/bash` and see that there is no directory called `.m2` inside the container. – John May 23 '17 at 09:43
  • try to cp without the variable dir name. RUN mkdir /.m2 COPY settings.xml /.m2/settings.xml – Gabbax0r May 23 '17 at 09:46
  • No, that doesn't help. However, if the host directory `/var/jenkins_home/.m2` exists prior to running the container then the file `settings.xml` is created inside it. – John May 23 '17 at 09:56
  • OK, the issue is that if a host directory is mounted to the container then the container's directory is overridden by the host directory's contents. The comment I just made is wrong - the reason I was seeing the `settings.xml` file inside the container is because I was using an incorrect `-v` switch, meaning the volume wasn't mounted correctly, thereby ensuring that the container's `settings.xml` file was visible. – John May 23 '17 at 10:44

1 Answers1

2

Basically when you run:

docker run -v /host-src-dir:/container-dest-dir my_image

You will overlay your /container-dest-dir with what is in /host-src-dir

From Docs

$ docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py

This command mounts the host directory, /src/webapp, into the container at /webapp. If the path /webapp already exists inside the container’s image, the /src/webapp mount overlays but does not remove the pre-existing content. Once the mount is removed, the content is accessible again. This is consistent with the expected behavior of the mount command.

This SO question is also relevant docker mounting volumes on host

It seems you want it the other way around (i.e. the container is source and the host is destination).

Here is a workaround:

  1. Create the volume in your Dockerfile

  2. Run it without -v i.e.: docker run --name=my_container my_image

  3. Run docker inspect --format='{{json .Mounts}}' my_container

  4. This will give you output similar to:

[{"Name":"5e2d41896b9b1b0d7bc0b4ad6dfe3f926c73","Source":"/var/lib/docker/volumes/5e2d41896b9b1b0d7bc0b4ad6dfe3f926c73/_data","Destination":"/var/jenkins_home","Driver":"local","Mode":"","RW":true,"Propagation":""}]

Which means your dir as it is on container was mounted into the host directory /var/lib/docker/volumes/5e2d41896b9b1b0d7bc0b4ad6dfe3f926c73/_data

Unfortunately, I do not know a way to make it mount on a specific host directory instead.

Ahmad Abdelghany
  • 11,983
  • 5
  • 41
  • 36