3

I have a Docker container and I am trying to make it so that all of the files in /var/www/ on the container will be saved on the host system at a location (/home/me), and vise-versa. Is it possible to have this shared space between the two?

Would you accomplish this with mount points, or is there a better method?

Thanks

J. Doe
  • 1,209
  • 5
  • 20
  • 43

2 Answers2

4

You can use volumes for sharing between container and host.

docker run -v /home/me:/var/www <image>

If you have a fixed files/data, you can add to the image using dockerfile or committing after copying into container. If you want to share rw dir between host and container, you need to use the volumes. Your data will also be persisted even if you remove and recreate a new container.

Gangaraju
  • 3,792
  • 7
  • 41
  • 71
  • Is it possible to add this to the dockerfile also? – J. Doe Apr 22 '16 at 18:34
  • @J.Doe, You can add the files to the dockerfile, but it won't be like a shared directory. If you made any changes inside the container later,wont be saved in the host and will be lost if container removed. – Gangaraju Apr 22 '16 at 18:43
  • So when running the command for the container, I can create the volume that I want with the command `docker run -d -P --name web -v /src/webapp:/opt/webapp testing123`. How can I turn that `/src/webapp:/opt/webapp` host directory into an argument inside of the Dockerfile? Can I use `VOLUME` to create this type of storage? – J. Doe Apr 22 '16 at 21:01
  • The reason I am asking is that when I tried to add it in with the arguments `VOLUME ["/home/ec2-user/:/home/ec2-user/" ]`, it creates the directory inside of the container with no files except a :. How would I structure this argument to work with it? – J. Doe Apr 22 '16 at 21:08
  • This link http://stackoverflow.com/questions/25311613/docker-mounting-volumes-on-host answers your question on volumes. – Gangaraju Apr 23 '16 at 06:17
1

There are three ways that you can do this

  1. Use volumes. Official docs
  2. Burn the files in your image. Basically include the creation of the files inside the Dockerfile. This means every container container from that image will have an initial state of sorts.
  3. Use data-only containers. These are containers without a running process that contain the data that you need. This also uses volumes. But instead of mounting to the host, your containers mount on the data-only container (which in turn mounts on the host if you want to). This answer will be useful
Community
  • 1
  • 1
Alkis Kalogeris
  • 14,519
  • 11
  • 50
  • 98