Docker-compose:合并/组合两个容器 [英] Docker-compose: merge/combine two containers
问题描述
我正在尝试为我的项目创建一个 docker 环境.让我解释一下.
I am trying to create a docker environment for my project. Let me explain.
我有一个名为 my-project
的项目,它产生一个战争 my-project.war
.我创建了一个名为 my-project:latest
的 Docker 映像,其中包含 /my-project
目录中的 my-project.war
.那很容易.
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-compose.yml
.我从这个 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
容器中的 code> 目录?这在 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 构建了这个镜像.
你将拥有一个包含你在 `/webapps' 中的战争的 tomcat 镜像.
Once you build this image using docker build -t app .
you will have a tomcat image that contains you war at `/webapps'.
在 docker-compose 中只使用这个图像:
In the docker-compose just use this image:
version: "3"
services:
application:
image: app
ports:
- 8080:8080
这篇关于Docker-compose:合并/组合两个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!