首页 > 解决方案 > Docker Compose 环境

问题描述

我的 docker-compose.yml 如下所示。

version: '3.4'

services:
  identity.api:
    image: panda/identity.api
    build: 
      context: .
      dockerfile: Services/Identity/PandaMarket.Identity.Api/Dockerfile

    ports:
      - "5000:80"
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ConnectionString=Server=sql.server;Database=PandaMarket.Identity;User Id=sa;Password=Password#123

当我执行 docker-compose build 或 docker-compose up

我检查docker inspect panda/identity.api,我找不到 ASPNETCORE_ENVIRONMENT & ConnectionString 出现。

当我表演时

docker inspect -f '{{range $index, $value := .Config.Env}} {{$value}} {{end}}' panda/identity.api

我只能看到这些变量

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin  ASPNETCORE_URLS=http://+:80  DOTNET_RUNNING_IN_CONTAINER=true  ASPNETCORE_VERSION=2.1.2

我的问题是环境如何反映在图像中?

标签: dockerdocker-compose

解决方案


如果您希望在映像中定义环境变量,则需要在 Dockerfile 中定义它们。您可以传递构建参数以允许您从撰写文件中更改该环境变量。

环境变量和其他设置(如命令和入口点)在 compose 文件中设置后,将应用于正在运行的容器。您将需要运行docker-compose updocker stack deploy ...查看这些设置的结果。

请注意,开始不鼓励从 compose 运行构建。它们不再是第 3 版格式的功能,并且在您使用 compose 文件通过 swarm 模式等编排工具进行部署时不适用。理想的解决方案是使用推送到注册表的 CI 服务器进行构建。


推荐阅读