Docker Community Forums

Share and learn in the Docker community.

Volume is empty even if there should be files


(Prames) #1

I am using Docker Toolbox for Windows. Everything works fine. I am now trying to run an image which clones some github repos into the local container folders.
What I want now is to mount that container folder (containing the files) to a folder on my host machine. I am trying to do it like this:

docker run --name testcontainer -v /c/Users/username/Desktop/testvolume:/var/www/testsite -d -p 80:80 

It is correctly creating the folder on my host, but it is empty and its overwriting the contents of the directory in the container, so it gets emptied as well.

Why does it not work as expected, and how can I achieve this?

Thanks alot


(Martin Terp) #2

Hi :slight_smile:

Its because the host is master, so if you mount from the host, it will be the hosts data that will be available in the container.

So, either put the data in the image ( as i guess you did there ), and for every change, create a new version of the image.
Or you could create the image as a application only, and mount the webdata to the container ( as you did ), so the data always is on the host.


(Prames) #3

Hi,

yes, the data is in the image. I am cloning a git repo into /var/www/testsite when building the image. This is the Code I need to available in my development environment. So I want to mount it to my host system. How can I achieve this?


(Martin Terp) #4

If you want to do it this way, you could replace the CMD/RUN in your image with a script.

Example steps in your Dockerfile:
1- Clone your repo into /var/tmp/
2- Create a bash script, that copies the files from /var/tmp to /var/www
3- At the end of the script, put your previous CMD
4- Use this new script as the start script for the container.

If, you want me to elaborate, you need to provide your Dockerfile :slight_smile:


(Prames) #5

This is my current Dockerfile:

FROM php:7.2.8-fpm-stretch

ADD .docker /opt/shopware
RUN /opt/shopware/install-shopware.sh

EXPOSE 22 80 3306 9000
CMD ["supervisord", "-n"]

In “install-shopware.sh” i am cloning the repo into /var/www/testsite, the directory I want to be available to my host with all of its contents.


(Martin Terp) #6

Okay, then try and extract to a different folder, in that script, like /var/tmp, and then make:

startScript.sh:

#!/bin/bash
mv /var/tmp/* /var/www/

supervisord -n

And edit your Dockerfile:

FROM php:7.2.8-fpm-stretch

ADD .docker /opt/shopware
RUN /opt/shopware/install-shopware.sh

EXPOSE 22 80 3306 9000
CMD ["bash", "/opt/shopware/startScript.sh"]

(Prames) #7

Tested it and built a fresh image with the code like you proposed. Sadly with the same result. The folder is empty on host and container. When not mounting the folder to the host, the folder in the container has all the files and works correctly.


(Tgeliot) #8

Did you modify install-shopware.sh to clone into /var/tmp instead of /var/www?