防止在 docker-compose 中跳过

Prevent skip in docker-compose

我有一个 docker-compose.yml 用于带有后端的 Django 网络应用程序。它看起来像这样:

version: '2'

services:
  db:
    image: # special image
    ports:
      - "1433:1433"
    environment:
      PASSWORD: "*********"

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    depends_on:
      - db

当我运行sudo docker-compose build时,第一行输出说

db uses an image, skipping. 
Building web.

我需要在 web 之前构建 db 图像。

如何让 db 图像先构建?

您可以在构建 web 之前在 Detached mode 中启动您的 db 容器:

$ docker-compose up -d db
$ docker-compose build web

不过,这seems like an anti-pattern. I would recommended that you keep the build process for web as generic as possible, and instead use environment variables or command arguments来完成这个。

例如,如果您需要将相同的配置值传递给 webdb,您可以使用 an env_file:

来完成此操作
# db_credentials.env
USER="django"
PASSWORD="********"
DATABASE="django_db"

并且在您的 docker-compose.yml 文件中:

services:
  db:
    # ...
    env_file: db_credentials.env

  web:
    # ...
    env_file: db_credentials.env