Docker-compose:合并/合并两个容器 [英] Docker-compose: merge/combine two containers

查看:242
本文介绍了Docker-compose:合并/合并两个容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目创建一个docker环境。让我解释一下。

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

我有一个名为 my-project 的项目,它会产生战争 my-project.war 。我创建了一个名为 my-project:latest 的Docker映像,该映像在<$ c中包含 my-project.war $ c> / my-project 目录。

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.

现在我想将Docker镜像 my-project:latest 和<$ c $ docker-compose.yml 中的c> tomcat:latest (这是Tomcat的官方Docker映像)。我从以下 docker-compose.yml 开始:

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

但是现在如何复制文件 / my-project / my-project.war my-project 容器到 webapps / tomcat 容器中的c>目录?这可能在docker-compose中还是应该从新的 Dockerfile 开始?

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.

推荐答案

您不能在docker-compose中做到这一点。不过,您可以在Docker中使用一个非常好的功能,称为 MultiStage Build

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

基本上,创建一个新的Dockerfile将映像合并为一个:

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

一旦您使用 docker build -t app构建此映像,

在docker-compose中,只需使用以下图像即可:

In the docker-compose just use this image:

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

这篇关于Docker-compose:合并/合并两个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆