With the docker-compose version 3 and above out and about, one of the things that have been dropped is the
volumes_from
which was kind useful to share the volumes from one service with another. It was a nice feature
for local development
So what can one do?
Well, now you can use bind mounts so your docker compose file will look along the lines of this:
|
|
Point of interest is line 6, which reads, form the host relative path ./web-app/application
mount to container to /application
.
Seems kind of ok, but lets add some more services and see what happens.
|
|
So yeah, guess what happens if you need to either move your source or destination paths… What is the chance that you will miss a setting when the pressure is on? I’d say quite high.
This can be improved upon by using environment variable substitution
|
|
and you create a .env
file at the same level where your docker compose file is
|
|
Now you have a little more flexibility, but it is still a lot of moving parts that might have to be changed at some point.
One important thing to note when dealing with .env
and docker is the way Docker processes the keys and values, namely there is no special handling of quotation marks. This means that they are part of the VAL.
You can read more about it here.
This might be common knowledge at this point, but I still wanted to write it down.
And this brings us to the actual subject of this post, I wanted to use named volumes to mount the host directory.
It started simple, and then it delved into weirdness.
For starters you need to define the named volume in your docker compose file, lets call the volume web_app
|
|
So what is happening here is web_app
volume is being declared, it should use a local
driver, and now we come to the interesting part which is options, when reading volumes long syntax you would get the impression that the type needs to be set to bind
, but once you execute docker composer you will get the following error
|
|
So um, yeah, no. This is not going to work, after some digging around the net, the correct setup is
|
|
Which is completely intuitive… Riiiight… The type should be none, but the o
should be set to bind
.
OK, that has been sorted now, and lets start it once again
Well, now… There is a different error, but this one actually is completely expected, as volumes, once created can’t be modified, just recreated
|
|
Before executing docker volume rm web_app
make sure you execute docker-compose down
so everything is stopped and released, otherwise you will get another error that the volume is in use
So now when you execute docker-compose up
you are greeted with another error
|
|
Awesome! It turns out that the device
setting only accepts the absolute paths. So what can be done?
Either set the WEB_APP_PATH
in your env vars (file or otherwise) to absolute path OR you use $PWD
which most likely works only on linux and macos YMMV!
|
|
Don’t forget to remove the existing volume by executing docker volume rm web_app
first.
And now when you execute docker-compose up
everything is working.
At this point you should have a named volume with host mounted directory that you can share amongst your services, because why not…
At the time of this writing I am not sure about the downsides of this approach, but as the syntax is very weird, I wanted to have it documented.
Of course this is ok for local development, and should not be used in production.