Docker-compose: merge/combine two containers

二次信任 提交于 2019-12-06 15:53:48

问题


I am trying to create a docker environment for my project. Let me explain.

I have a project called my-project which produces a war my-project.war. I created a Docker image called my-project:latest which contains the my-project.war in the /my-project directory. That was easy.

Now I want to combine the Docker images my-project:latest and tomcat:latest (which is the official Docker image for Tomcat) in docker-compose.yml. I started with this docker-compose.yml:

version: "3"
services:
  my-project:
    image: my-project:latest
  tomcat:
    image: tomcat:latest
    ports:
      - 8080:8080

But how can I now copy the file /my-project/my-project.war from the my-project container to the webapps/ directory in the tomcat container? Is this possible in docker-compose or should I start from a new Dockerfile?

In other words, I am having troubles combining these two containers.


回答1:


You can't do that in docker-compose. You can however use a very nice feature in docker called MultiStage Build

Basically, create a new Dockerfile to combine the images into one:

FROM my-project:latest as project

FROM tomcat:latest
COPY --from=project /my-project/my-project.war /webapps

Once you build this image using docker build -t app . you will have a tomcat image that contains you war at `/webapps'.

In the docker-compose just use this image:

version: "3"
services:
  application:
    image: app
    ports:
      - 8080:8080


来源:https://stackoverflow.com/questions/47461999/docker-compose-merge-combine-two-containers

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