不要将 Dockerfile 重命名为 Dockerfile.db 或 Dockerfile.web,您的 IDE 可能不支持它,您将失去语法高亮。
作为Kingsley Uchnor said,你可以有多个Dockerfile,每个目录一个,代表你想要构建的东西。
我喜欢有一个docker 文件夹来保存每个应用程序及其配置。这是具有数据库的 Web 应用程序的示例项目文件夹层次结构。
docker-compose.yml
docker
├── web
│ └── Dockerfile
└── db
└── Dockerfile
docker-compose.yml 示例:
version: '3'
services:
web:
# will build ./docker/web/Dockerfile
build: ./docker/web
ports:
- "5000:5000"
volumes:
- .:/code
db:
# will build ./docker/db/Dockerfile
build: ./docker/db
ports:
- "3306:3306"
redis:
# will use docker hub's redis prebuilt image from here:
# https://hub.docker.com/_/redis/
image: "redis:alpine"
docker-compose命令行使用示例:
# The following command will create and start all containers in the background
# using docker-compose.yml from current directory
docker-compose up -d
# get help
docker-compose --help
如果您在构建 Dockerfile 时需要以前文件夹中的文件
您仍然可以使用上述解决方案并将您的Dockerfile 放在docker/web/Dockerfile 等目录中,您只需在docker-compose.yml 中设置构建context,如下所示:
version: '3'
services:
web:
build:
context: .
dockerfile: ./docker/web/Dockerfile
ports:
- "5000:5000"
volumes:
- .:/code
这样,你就可以拥有这样的东西:
config-on-root.ini
docker-compose.yml
docker
└── web
├── Dockerfile
└── some-other-config.ini
还有一个像这样的./docker/web/Dockerfile:
FROM alpine:latest
COPY config-on-root.ini /
COPY docker/web/some-other-config.ini /
Here are some quick commands from tldr docker-compose。请务必参考official documentation 了解更多详情。