Mount volume in docker

I have a docker-compose.yml . One of the containers has a Tomcat server, and it has a default .war file deployed to the webapps directory.

I want to be able to transfer (override) the war archive for deployment by some that are on the main machine. I think that it would be best to have the ability to somehow switch / cancel the docker-compose launch: by default I want to run the webapp ( war ) file, which is inside the container, but I want to be able to mount (for example, during development / debug) if necessary.

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

 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 to achieve this?

+7
docker docker-compose docker-volume
source share
2 answers

Instead of (not) mounting a volume commenting on this line, I would use https://docs.docker.com/compose/extends/#example-use-case to start the service extension that the volume indicates.

I do this to solve two different problems:

  • I do not indicate when the docker image is intended to be launched during production and contains all the necessary files.
  • I specify the volume at design time to check for changes in real time.
+5
source share

Say .war filename is "app.war" ... you can overwrite it with the env variable as follows:

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

Then, when you need to run another war file, just change the value of APPPLICATION_ENV to the one you need to start and restart the container.

I don't think docker-compose has "conditional volumes", but in this way you can change app.war to suit your environment.

In another way, the script after docker-compose up/start will work to overwrite it, and only do this if necessary, for example:

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

All Articles