1

I made a docker image called myImage, there is a folder: /data I want to let the user edit it by themselves. I read that -v flag can mount the volume, so I used it like following:

I run the container with this command:

docker run -v /my_local_path:/data -it myImage /bin/bash

But surprisingly, docker cleared all the files in /data in the container. But this is not I want... I want actually the host can get all the files from /data... :(

How can I do that?

danday74
  • 52,471
  • 49
  • 232
  • 283
xirururu
  • 5,028
  • 9
  • 35
  • 64
  • You can't. When you mount host machine directory to container. At first time data from host machine overide all data in your container directory. Then everything change in directory inside container will be sync with you host mount directory... You maybe wanna try mount your host machine to another directory and copy from /data to that. – Truong Dang Nov 03 '18 at 02:31
  • @TruongDang Actually, I saw the bitname/wordpress did that. They can let the user edit some of the data... – xirururu Nov 03 '18 at 02:39

1 Answers1

1

When you share a volume like this, the volume on the host overwrites the volume in the container, so the files in the container's folder will be removed.

What you need to do is put the files in the container in folder A (a folder in the container). Mount folder B (another folder in the container). Then AFTER the volume is mounted, move the files from folder A to folder B. Then these files will be both available to the host and inside the container.

You can achieve this 'move files' operation using a RUN or an ENTRYPOINT script in your Dockerfile.

See Run a script in Dockerfile

Sorry, I forget if you need RUN or ENTRYPOINT (or if either will work) but one of these will definitely do it.

I think you want ENTRYPOINT because an ENTRYPOINT script runs AFTER the container is created. Thus it will run after the volume is mounted.

danday74
  • 52,471
  • 49
  • 232
  • 283