如何将环境变量传递给 docker-compose up [英] How to pass environment variable to docker-compose up

查看:32
本文介绍了如何将环境变量传递给 docker-compose up的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个容器.我已经将图像上传到私有 Docker 注册表.我想编写一个撰写文件来下载和部署映像.但我想将 TAG 名称作为来自 docker-compose run 命令的变量传递.我的撰写文件如下所示.如何将 KB_DB_TAG_VERSION 的值作为 docker-compose up 命令的一部分传递?

I am trying to run a container. I already have the image uploaded to private Docker registry. I want to write a compose file to download and deploy the image. But I want to pass the TAG name as a variable from the docker-compose run command.My compose file looks like below. How can I pass the value for KB_DB_TAG_VERSION as part of docker-compose up command?

version: '3'
services:
   db:
    #build: k-db
    user: "1000:50"
    volumes:
      - /data/mysql:/var/lib/mysql
    container_name: k-db
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    image:  XX:$KB_DB_TAG_VERSION
    image: k-db
    ports:
      - "3307:3306"

推荐答案

您有两个选择:

  1. 按照另一个.env文件https://stackoverflow.com/a/49294173/3937850">答案.
  2. KEY=VALUE 对添加到您的 docker-compose 命令中,例如:

  1. Create the .env file as already suggested in another answer.
  2. Prepend KEY=VALUE pair(s) to your docker-compose command, e.g:

KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0 docker-compose up

在脚本中更早地导出它也应该有效,例如:

Exporting it earlier in a script should also work, e.g.:

export KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0
docker-compose up

这篇关于如何将环境变量传递给 docker-compose up的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆