0

Say, I have a local dir structure of /var/lib/jenkins and on the resultant container i have the same dir stucture. Now, i need that the jenkins user created through Dockerfile should be able to access the /var/lib/jenkins/.m2/repository existing on local machine. I have mapped the container jenkins userid to the jenkins userid on local using usermod and groupmod in Dockefile. However, I need that /var/lib/jenkins/.m2/repository on local should be accessible inside the container for all processes on it, in a way that local machines's repository maps to container path

Effectively, i need to achieve below cmd inside the dockerfile

docker run -it --name my_container -v /var/lib/jenkins/.m2/repository:/var/lib/jenkins/.m2/repository my_image 

Any guidance on this will be helpful.

Thanks!

2 Answers 2

0

You cannot define a volume source in a Dockerfile, because images may be run on different environments where the source would change. So while you can tell Docker the image has a volume at /var/lib/jenkins/.m2/repository, you cannot tell the image to map that volume to the host directory /var/lib/jenkins/.m2/repository. To specify runtime configurations, you can specify this in a docker-compose.yml file with a volume section like:

version: '3' services: svcname: image: my_image volumes: - /var/lib/jenkins/.m2/repository:/var/lib/jenkins/.m2/repository 

I personally recommend against defining a volume inside the Dockerfile. It will generate anonymous volumes if you don't also specify a volume source at runtime. And if you want to make changes to this directory with a RUN command in your Dockerfile after the volume is defined, those changes will not be saved to your image and will be lost when the RUN line completes.

3
  • Thanks, @BMitch, for explaining so well. Cleared all doubts. I also do not favor defining volume inside Dockerfiles as it defeats the purpose of Docker portability. However, I was trying to use the mvn repos already downloaded on host to use in the container......But yes, docker compose is the right way to go for this usecase so far. Commented Aug 23, 2018 at 11:12
  • I could only accept your answer @BMitch. I am unable to upvote due to StackExchange reputation criteria. Commented Aug 23, 2018 at 11:15
  • @ValenciaSerrao if you're looking for the option to have a cache directory on the build host, that functionality is coming with buildkit: github.com/moby/moby/issues/32507#issuecomment-409092581 Commented Aug 23, 2018 at 11:23
0

Yes, it is, you can use VOLUME directive, in your example line will looks like

VOLUME /var/lib/jenkins/.m2/repository

detailed description in documentation

1
  • Thanks, @Quantim for responding. Yes. I had tried this instruction earlier too, it only creates an empty directory in the container. It still needs to use 'docker run -it -v ' to be able to access the host path. This is confirmed in another post here (see accepted answer: stackoverflow.com/questions/27735706/docker-add-vs-volume) Commented Aug 23, 2018 at 11:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.