Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount a directory in the docker container to the host?

It's quite easy to mount a host directory in the docker container.

But I need the other way around.

I use a docker container as a development environment for developing WordPress plugins. This docker container contains everything needed to run WordPress (MySQL, Apache, PHP and WordPress). I mount my plugin src folder from the host in the docker container, so that I can test my plugin during development.

For debugging it would be helpful if my IDE running on the host has read access to the WordPress files in the docker container.

I found two ways to solve the problem but both seem really hacky.

  1. Adding a data volume to the docker container, with the path to the WordPress files

    docker run ... -v /usr/share/wordpress/ ...

    Docker adds this directory to the path on the host /var/lib/docker/vfs/dir... But you need to look up the actual path with docker inspect and you need root access rights to see the files.

  2. Mounting a host directory to the docker container and copying the WordPress files in the container to that mounted host directory. A symlink doesn't seem to work.

Is there a better way to do that? Without copying files or changing access rights?

Thank you!

like image 267
Kai Hofstetter Avatar asked Jan 22 '15 08:01

Kai Hofstetter


Video Answer


1 Answers

Copying the WordPress files to the mounted folder was the solution.

I move the files in the container from the original folder to the mounted folder and use symbolic links to link them back to the original folder.

The important part is, the container can follow symbolic links in the container and but the host can't. So just using symbolic links from the original folder to the mounted folder doesn't work, because the host cannot follow symbolic links in the container!

like image 175
Kai Hofstetter Avatar answered Oct 08 '22 09:10

Kai Hofstetter