我创建了以下 docker-compose.yml 文件来为 Django 应用程序构建 docker 容器:
version: "2.4"
services:
db:
image: postgres:11
env_file:
- .env_prod_db
volumes:
- db:/var/lib/postgresql/data/
networks:
- net
logging:
driver: "json-file"
options:
max-file: "5"
max-size: "10m"
web:
build:
context: .
dockerfile: Dockerfile
env_file:
- .env_prod_web
command: gunicorn roster_project.wsgi:application --disable-redirect-access-to-syslog --error-logfile '-' --access-logfile '-' --access-logformat '%(t)s [GUNICORN] %(h)s %(l)s %(u)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"' --workers 3 --bind 0.0.0.0:8000
volumes:
- static:/roster/webserver/static/
networks:
- net
expose:
- 8000
depends_on:
- db
logging:
driver: "json-file"
options:
max-file: "5"
max-size: "10m"
nginx:
build: ./nginx
ports:
- 80:80
volumes:
- static:/roster/webserver/static/
networks:
- net
depends_on:
- web
logging:
driver: "json-file"
options:
max-file: "5"
max-size: "10m"
networks:
net:
enable_ipv6: true
driver: bridge
ipam:
driver: default
config:
- subnet: fd02::/64
gateway: fd02::1
volumes:
db:
static:
如果我的应用程序的潜在用户首先从 Github 下载所有源代码,他们可以使用此文件来部署我的应用程序。但是,我希望他们能够通过使用 docker-compose 下载我存储在 Docker Hub 存储库中的 docker 镜像来部署应用程序。如果我将 docker 镜像上传到 Docker Hub 存储库,我是否需要创建一个额外的 docker-compose.yml 来引用存储库镜像,以便其他人可以在他们自己的 docker 主机上部署我的应用程序?或者我可以以某种方式将构建和部署要求组合到一个 docker-compose.yml 文件中吗?
最佳答案
您可以use multiple Compose files当你运行 docker-compose
命令。最简单的方法是拥有一个主要的 docker-compose.yml
列出标准(通常面向生产)设置的文件,以及 docker-compose.override.yml
覆盖其设置的文件。
例如,基数 docker-compose.yml
文件可能如下所示:
version: "2.4"
services:
db:
image: postgres:11
volumes:
- db:/var/lib/postgresql/data/
web:
image: me/web
depends_on:
- db
nginx:
image: me/nginx
ports:
- 80:80
depends_on:
- web
volumes:
db:
请注意,我已经删除了所有特定于部署的设置(日志配置、手动 IP 覆盖、环境文件);我正在使用 Compose 提供的 default
网络;我避免使用 Docker 卷中的旧内容覆盖静态 Assets ;我提供 image:
甚至为我在本地构建的东西命名。此设置与“真实”生产设置之间的差异可以放在单独的
docker-compose.production.yml
中。文件:version: "2.4"
services:
db:
# Note, no image: or other similar settings here;
# they come from the base docker-compose.yml
env_file:
- .env_prod_db
logging:
driver: "json-file"
options:
max-file: "5"
max-size: "10m"
web: # similarly
db: # similarly
networks:
default:
enable_ipv6: true
# and other settings as necessary
另一方面,对于开发,您需要提供 build:
信息。 docker-compose.development.yml
可以包含:version: "2.4"
services:
web:
build: .
nginx:
build: ./nginx
然后您可以使用符号链接(symbolic link)将其中一个设置为当前覆盖文件ln -sf docker-compose.production.yml docker-compose.override.yml
下游部署者将需要基础 docker-compose.yml
,其中提到了 Docker Hub 镜像。如果对他们有意义,他们可以使用您的生产值,或者他们可以使用不同的设置。他们不应该需要其余的应用程序源代码或 Dockerfile(尽管它们可能都在同一个 GitHub 存储库中)。
关于docker - 使用 docker-compose 在生产环境中部署 docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63894195/