Docker-compose:在生产中用预先构建的映像替换基于“构建"的服务? [英] Docker-compose: replace "build"-based service with pre-built image in production?
问题描述
假设我们有以下 docker-compose.yml
:
version: '3'
services:
db:
image: "postgres"
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=mysecretpassword
web:
build: web
depends_on: [ db ]
ports:
- "80:80"
第一个服务 db
,仅运行带有Docker Hub中带有官方postgres映像的容器.
The first service, db
, just runs a container with the official postgres image from Docker Hub.
第二项服务 web
,首先基于Dockerfile 在也称为 web
的文件夹中构建新映像,然后运行容器带有该图像.
The second service, web
, first builds a new image based on the Dockerfile
in a folder also called web
, then runs a container with that image.
在开发时,我们现在可以(反复)对 web
文件夹中的内容进行更改,然后运行 docker-compose up --build
在本地运行我们的应用程序.
While developing, we now can (repeatedly) make changes to whatever is in the web
folder, then run docker-compose up --build
to run our app locally.
假设我们现在要部署到生产环境.我的理解是,现在可以使用 docker-compose.yml
以Docker的群集模式定义堆栈"(请参阅例如答案).但是,对于 web
服务的 build
步骤,Docker的撰写文件文档指出
Let's say we now want to deploy to production. My understanding is that docker-compose.yml
can now be used to "define a stack in Docker's swarm mode" (see this answer, for instance). However, for the build
step of the web
service, Docker's compose file documentation states that
在以(版本3)撰写文件以群集模式部署堆栈时,将忽略此选项.docker stack命令仅接受预构建的图像.
This option is ignored when deploying a stack in swarm mode with a (version 3) Compose file. The docker stack command accepts only pre-built images.
无论如何,在生产机器上构建映像可能不是一个好主意,因为这会留下构建工件(源代码).这应该在构建服务器上发生.
It probably wouldn't be a great idea to build the image on the production machine anyways, as this would leave build artifacts (source code) behind; this should happen on a build server.
我的问题是,有没有推荐的方法来修改 docker-compose.yml
到生产的过程中,将 build:web
换成 image:< id>
以某种方式?
My question is, is there a recommended way to modify docker-compose.yml
en route to production to swap out build: web
with image: <id>
somehow?
在在生产环境中使用Compose 上没有任何内容.我的方法总体上有问题吗?
Nothing on Use Compose in production on that. Is there something wrong with my approach in general?
推荐答案
docker-compose.yml
仅应包含规范的服务定义.
docker-compose.yml
should only contain canonical service definitions.
任何特定于构建环境的内容(例如dev与prod)都应在单独的文件 docker-compose.override.yml
中声明.每个构建环境都可以具有该文件的自己版本.
Anything that's specific to the build environment (e.g. dev vs prod) should be declared in a separate file docker-compose.override.yml
. Each build environment can have its own version of that file.
build:web
声明不属于 docker-compose.yml
,因为它仅应在本地运行(并且可能在构建服务器上运行),而不是在本地运行在生产中.
The build: web
declaration doesn't belong into docker-compose.yml
, as it's only supposed to run locally (and possibly on a build server), not in production.
因此,在上面的示例中,这是 docker-compose.yml
的样子:
Therefore, in the example above, this is what docker-compose.yml
should look like:
version: '3'
services:
db:
image: "postgres"
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=mysecretpassword
web:
depends_on: [ db ]
ports:
- "80:80"
这将是本地开发的默认 docker-compose.override.yml
:
And this would be the default docker-compose.override.yml
for local development:
version: '3'
services:
web:
build: web
运行 docker-compose up --build -d
现在将构建最新的代码更改并在本地启动我们的应用程序.
Running docker-compose up --build -d
will now build the latest code changes and launch our app locally.
还可能有另一个版本 docker-compose.override.build.yml
,其目标是构建/CI服务器:
There could also be another version docker-compose.override.build.yml
, targeting a build/CI server:
version: '3'
services:
web:
build: web
image: mydockeruser/web
运行 docker-compose -f docker-compose.yml -f docker-compose.override.build.yml push
将构建最新的代码更改并将映像推送到其注册表/存储库.
Running docker-compose -f docker-compose.yml -f docker-compose.override.build.yml push
will build the latest code changes and push the image to its registry/repository.
最后,可能还有另一个版本 docker-compose.override.prod.yml
:
Finally, there could be another version docker-compose.override.prod.yml
:
version: '3'
services:
web:
image: mydockeruser/web
部署到生产环境(仅部署到单个Docker主机,而不是集群)现在可以像仅复制 docker-compose.yml
和 docker-compose.override.prod一样简单.yml
并在-d 上运行 docker-compose -f docker-compose.yml -f docker-compose.override.prod.yml.
Deploying to production (just to a single Docker host, not a cluster) can now be as simple as copying over only docker-compose.yml
and docker-compose.override.prod.yml
and running docker-compose -f docker-compose.yml -f docker-compose.override.prod.yml up -d
.
这篇关于Docker-compose:在生产中用预先构建的映像替换基于“构建"的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!