Mount a volume in docker-compose conditionally

烈酒焚心 提交于 2019-12-04 22:33:42

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!