[英]How to organize multiple Dockerfiles, docker-compose.yaml and .dockerignore
在查看了关于如何命名 Dockerfiles的答案后,我仍然有很多问题。
就我而言,我至少需要两个Dockerfile
,一个docker-compose.yaml
和一个.dockerignore
。 听起来像使用诸如<purpose>.Dockerfile
或Dockerfile.<purpose>
的扩展名的缺点是失去了在 hub.docker.com 上使用 autobuilder 的功能。
所以其他人建议你将每个Dockerfile
在一个目录中并从那里构建。
所以也许是这样的:
dockerfiles/
--python_consumer/
-----Dockerfile
--python_producer/
-----Dockerfile
--docker-compose.yaml
--.dockerignore
在这种情况下, .dockerignore 会在所有 dockerfiles 全局工作吗? 以这种方式构建有什么大的缺点吗?
我docker-compose.yaml
没有单独的目录和一个用于上下文的组合消费者/生产图像。
version: '3.8'
services:
standalone:
hostname: standalone
container_name: standalone
image: apachepulsar/pulsar:2.8.1
ports:
- 8080:8080 # exposed would be a better practice
- 6650:6650 # exposed would be a better practice
command: bin/pulsar standalone
healthcheck:
test: ["CMD", "nc", "-vz", "localhost", "6650"]
interval: 20s
timeout: 5s
retries: 5
networks:
- conprod
conprod:
hostname: conprod
container_name: conprod
build:
context: .
dockerfile: ./Dockerfile
restart: on-failure # best practice is probably "unless-stopped"
depends_on:
- standalone
networks:
- conprod
networks:
conprod:
当你构建一个镜像时,你会向 Docker 守护进程发送一个构建上下文; 在您的 Compose 设置中,这是在build: { context: }
设置中命名的目录。 .dockerignore
文件必须在那个确切的目录中,而不是其他地方。 它的实际效果是导致文件被排除在构建上下文之外,这可以导致更快的构建序列。
构建上下文的另一个重要影响是所有 Dockerfile COPY
指令都被认为是相对于该目录的; 您不能从父目录或同级目录COPY
。 因此,如果文件在项目之间共享,则必须将上下文目录设置为将包含的所有文件的某个祖先目录,并且COPY
指令将相对于该目录(即使 Dockerfile 位于每个项目的目录中)。 另请参阅如何在 Docker 的构建上下文之外包含文件?
如果您的项目是完全独立的:可能有一个前端和一个后端项目,或者在您的情况下,生产者和消费者共享一种消息格式但没有任何实际代码。 那么在这种情况下:
Dockerfile
.dockerignore
一个.dockerignore
文件(不能在父目录下)COPY
指令是相对于项目子目录的COPY requirements.txt ./
build: directory
语法,因为您拥有标准(默认) dockerfile:
name version: '3.8' services: producer: build: ./python_producer environment: - RABBITMQ_HOST=rabbitmq consumer: build: ./python_consumer environment: - RABBITMQ_HOST=rabbitmq rabbitmq: image: rabbitmq:3 hostname: rabbitmq # RabbitMQ is very unusual in needing to set this
如果您的项目共享代码或其他文件:在您的示例中,您可能为共享代码中的消息格式定义了 Python 数据结构。 在这种情况下:
Dockerfile
.dockerignore
文件COPY
指令是相对于项目根目录的COPY python_producer/requirements.txt ./
context: .
和dockerfile:
指向每个组件的 Dockerfile version: '3.8' services: producer: build: context: . dockerfile: python_producer/Dockerfile environment: - RABBITMQ_HOST=rabbitmq consumer: build: context: . dockerfile: python_consumer/Dockerfile environment: - RABBITMQ_HOST=rabbitmq rabbitmq: image: rabbitmq:3 hostname: rabbitmq # RabbitMQ is very unusual in needing to set this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.