41

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?

fxmasa
  • 714
  • 1
  • 8
  • 14

3 Answers3

31

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
Pawel
  • 16,093
  • 5
  • 70
  • 73
  • This approach works well where I have some differences on host o/s ```#!/bin/bash if [ "$(uname -s)" == "Darwin" ]; then env_file="env/envMacOS" dc_extend="" else env_file="env/envUbuntu" dc_extend="-f docker-compose-linux.yml" fi # files required by docker but are not in git as they are duplicated cp ${env_file} .env cp ../requirements.txt . echo "***** ENV ${env_file} ****" docker-compose up -d --build htpc docker-compose -f docker-compose.yml ${dc_extend} up -d ``` – Rob Raymond May 20 '20 at 07:30
15

Instead of (not) mounting a volume commenting out that line, I would use https://docs.docker.com/compose/extends/#example-use-case to run a service extension that specifies a volume.

I do this to tackle two different problems:

  • I don't specify a volume when the docker image is meant to run in production and has all the necessary files bundled in it.
  • I specify a volume during development, to check live changes.
dgulabs
  • 443
  • 4
  • 16
4

Let's say that the .war filename is "app.war"... you could overwrite it using a env variable like this:

volumes:
 - ./application/webapps/${APPLICATION_ENV}.war:/usr/local/tomcat/webapps/app.war

Then when you need to run a different war file just change the APPPLICATION_ENV value to the one you need to run and restart the container.

I don't think docker-compose does have "conditional volumes", but that way you could change the app.war according with your environment.

Other way would be running a script after the docker-compose up/start to overwrite it, and do it only when needed, like:

docker-compose exec your-container-name cp /a/volume/path/app.war /usr/local/tomcat/webapps/app.war