0

I want to run my cloud in a docker container and store the data outside the container in a volume. So that when my container is destroyed or removed, the data is still persistent.

When passing the volume parameter at docker container run -v /path/to/src:/path/to/target imageName:tag it runs fine without any issues.

How can I pass the src and target destinations into the dockerfile?

VOLUME /path/to/target will create a volume which has its source somewhere in var/lib/docker which I do not want.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
xddq
  • 341
  • 3
  • 7
  • 2
    Does this answer your question? [Understanding "VOLUME" instruction in DockerFile](https://stackoverflow.com/questions/41935435/understanding-volume-instruction-in-dockerfile) – Efran Cobisi Aug 27 '20 at 20:15
  • Yeah together with Carsons answer. Thank you sir. – xddq Aug 27 '20 at 20:26

2 Answers2

3

Short answer: You can't.

Long Answer: what you are trying to do here is essentially against the portability of images. The Docker documentation specifically states this is not possible, as seen here: https://docs.docker.com/engine/reference/builder/

"The host directory is declared at container run-time: The host directory (the mountpoint) is, by its nature, host-dependent. This is to preserve image portability, since a given host directory can’t be guaranteed to be available on all hosts. For this reason, you can’t mount a host directory from within the Dockerfile. The VOLUME instruction does not support specifying a host-dir parameter. You must specify the mountpoint when you create or run the container."

Carson
  • 864
  • 1
  • 6
  • 19
  • Dang I should start reading documentation more carefully :( Thanks a lot man! The portability is one decent argument. Although they COULD still have created that option. – xddq Aug 27 '20 at 20:24
  • 1
    docker-compose will probably handle a lot of what you're wanting though. You can target a "build " instead of an image, or once you have the built image you can specify a volume in the compose. If you're doing orchestration, then it will depend on what you're using there, but every option should have some kind of volume scripting somewhere! – Carson Aug 27 '20 at 20:27
  • Are you talking about a host volume with the compose file stuff? That sounds promising. I am learning docker and am "not there yet" since I want to go step by step. I will look forward to that! – xddq Aug 27 '20 at 20:29
  • 1
    I find Docker docs very dense, but the long and short of it is after you build your image, you can use a compose file to create your containers. In that compose file (docker-compose.yml for example) you can specify volumes using the src:dest method. Highly recommend getting into docker-compose once you're comfy with Docker. – Carson Aug 27 '20 at 20:34
1

It sounds like you want default files in your container, which you can place in the container using the COPY directive to copy files from your local machine, into the container image. It is then possible to over-ride the directory you put files in using the -v parameter to replace a directory mount with a local-host one when you start/run the container:

eg

COPY ./files/source/swagger.json /application/resources/destination/swagger.json

see Docker mount to folder overriding content for more

Rob Evans
  • 2,822
  • 1
  • 9
  • 15