Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mount a volume in docker-compose conditionally

I have a docker-compose.yml configuration. In one of the containers there is a Tomcat server and it has some default .war file deployed in webapps directory.

I want to have an ability to pass (override) the war archive to be deployed by some which resides on the host machine. I think the best would be to have ability somehow switch / override starting docker-compose: as a default, I want to run the webapp (war file) which is inside the container, but I want to have a possibility to mount a directory from my host (for example during development / debugging) if need be.

Currently, I have the following line in my docker-compose.yml, which is commented out if I need the default.

volumes: # By default, there is the latest version of the application already present in the container # If you want to provider the container with your own .war file, uncomment the following line # - ./application/webapps:/usr/local/tomcat/webapps 

Is there a better way how to achieve that?

like image 852
fxmasa Avatar asked Jan 21 '17 09:01

fxmasa


People also ask

Can you mount a volume while building your Docker image?

When building an image, you can't mount a volume. However, you can copy data from another image! By combining this, with a multi-stage build, you can pre-compute an expensive operation once, and re-use the resulting state as a starting point for future iterations.

How do I mount a volume to an existing container?

But, if you do need to add a volume to a running container, you can use docker commit to make a new image based on that container, and then clone it with the new volume. Then, you can run the new image, replacing the old image with the cloned one.

What is difference between volume and bind mount Docker?

The most notable difference between the two options is that --mount is more verbose and explicit, whereas -v is more of a shorthand for --mount . It combines all the options you pass to --mount into one field. On the surface, both commands create a PostgreSQL container and set a volume to persist data.


1 Answers

Since extend has been removed in version 3 there's a new way of overriding settings by providing multiple -f params where the next file extends the previous config

The docs are mixed between versions https://docs.docker.com/compose/extends/#multiple-compose-files

i.e. to run in development mode

docker-compose -f docker-compose.yml -f docker-compose.dev.yml 

Where docker-compose.yml contains the full config and docker-compose.dev.yml only adds a volume

services:   serviceA:     volumes:       -.:/usr/local/abc/service 
like image 69
Pawel Avatar answered Sep 20 '22 06:09

Pawel