Docker Community Forums

Share and learn in the Docker community.

Files haven't been copied to volume folder during docker run

HI community,
I’m trying to build my own image based on image from Docker Hub
What I want to do is to add some new files to existing file system on image
I created Dockerfile with the following content:

from wordpress
copy . /var/www/html/wp-content/plugins

Here I’m trying to fetch official wordpress image and then copy some files in my current directory to plugins folder. Then I built the image and got new image lets call it newimage. Then I’m trying to run container based on this newimage and also I want to use volume for this contatiner to store data on my host machine. I’m doing this command:
docker run -d --name wp -v wp-volume:/var/www/html newimage
The contatiner starts well but when I’m trying to check the foder var/www/html/wp-content/plugins I can’t see the files that was copied on stage of building this image. But if I do the command:
docker run -d --name wp newimage
without using a volume I can see the files that was copied in the path /var/www/html/wp-content/plugins
so why if i run container WITH volume I can’t see my files but If I run container WITHOUT volume I can see my files in /var/www/html/wp-content/plugins

Is the volume 100% empty? If not: when did you first create/use the volume? Any chance it was used before you added the copy command? Contents of the image are only copied to an empty volume:

Populate a volume using a container

If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory’s contents are copied into the volume. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.

Or, worded differently:

Tips for using bind mounts or volumes

If you use either bind mounts or volumes, keep the following in mind:

  • If you mount an empty volume into a directory in the container in which files or directories exist, these files or directories are propagated (copied) into the volume. Similarly, if you start a container and specify a volume which does not already exist, an empty volume is created for you. This is a good way to pre-populate data that another container needs.
  • If you mount a bind mount or non-empty volume into a directory in the container in which some files or directories exist, these files or directories are obscured by the mount, just as if you saved files into /mnt on a Linux host and then mounted a USB drive into /mnt. The contents of /mnt would be obscured by the contents of the USB drive until the USB drive were unmounted. The obscured files are not removed or altered, but are not accessible while the bind mount or volume is mounted.

Aside, if you also want to see changes from your local file system in WordPress after you created the image, then maybe bind mounts are a better choice than volumes.

1 Like

After some investigations I made it work but only with volumes not with bind mounts.
Now I have an issue that when I try to run container with volume like -v some-volume:/var/www/html I can see the files in this volume (initial files from wordpress image and the files that were added during the build with COPY command) but if I use run with bind mount like -v /my-host-folder:/var/www/html it copies initial image files of wordpress image but NOT the files that were added by me during image build. If I enter the container with exec -it container bash I can see that ALL files are presented - initial wordpress files from image and my files that were added during build. I don’t understand why it happens Why it doesn’t copy ALL the files but only initial files to mounted folder. I also added the same user:group during COPY command it was www-data but the same result

Copies where? If you’re saying that you see the files from the image on your local file system: are you sure about the “it copies initial image files of wordpress image”? I think that should not happen at all for bind mounts. So, if you’re referring to your local file system: are you sure the files you say are being copied were not already on your local file system?

yes I’m sure when I run container I use command -v ~/somedir:/var/www/html and it creates ~/somedir and populates it with content from container folder /var/www/html but It copies only wordpress files not mine

Do you mean that it works only in one direction in case of bind mount ? Something is mounted from host to container ? So I should have some files on my host folder and I want to mount this folder to container. I thought it works in both directions so if i have empty host folder and point it to folder in container that contains some source files it will populate my host folder with content from container. But it is weird that it works that way on my machine I suppose it could be some bug and it depends on filesystem. In case of volumes I understand that it copies all content from container into volume.

Ah, you’re running into some WordPress copy-on-first-run functionality, not some Docker functionality. I indeed see the following:

% docker run --rm -v ~/somedir:/var/www/html wordpress
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html

Note, however, how that says “WordPress not found in…”. That’s not something Docker does, but something the WordPress image is doing for you. And it’s then probably not taking the container’s /var/www/html as the source folder (which WordPress cannot even access access anymore, see the “If you mount a bind mount or non-empty volume into a directory in the container in which some files or directories exist, these files or directories are obscured by the mount” quote from the documentation in my first answer). And that source folder it uses is not the folder to which you added files using COPY.

For any other folder, like docker run --rm -v ~/somedir:/etc/apache2/conf-available wordpress this shows the same message from WordPress, but also you won’t see anything in your local folder, for bind mounts. (Docker only does the copy on first run for empty volumes, not for empty bind mounts.)

1 Like

Thanks for you reply it might be some issue with wordpress image I’ll ask on their github page

I wouldn’t say it’s “an issue”, but a feature. You can see what the entrypoint is doing:

So, it seems it’s taking the content from /usr/src/wordpress.

Also, note that you’re using the Docker Inc. official image of WordPress, which is not maintained by wordpress.org.

1 Like

I’ve changed my Dockerfile to
COPY --chown=www-data:www-data . /usr/src/wordpress/wp-content/plugins
and now IT WORKS like a charm !!! :stuck_out_tongue:
Now I can run
docker run -d -v ~/wordpress:/var/www/html my-wordpress
and it copies all files to my host directory ~/wordpress
Thank you very much !!