简体   繁体   English

Docker映像:创建内部可用的环境变量

[英]Docker Image: Create Internally available environment variables

On my image I want to set some environment variables eg: MY_VAR where it will have a static value eg: MY_VAR=12 but I do NOT want to be able to set it via docker's -e param or via docker-compose.yml's environment section. 在我的映像上,我想设置一些环境变量,例如: MY_VAR ,它将具有静态值,例如: MY_VAR=12但是我希望能够通过docker的-e参数或通过docker-compose.yml的environment部分进行设置。 。

Furthermore I do not want to be as build argument when i do either docker build or docker-compose build 此外,当我执行docker builddocker-compose build时,我不想成为build参数

How can I do that? 我怎样才能做到这一点?

You can do that from an entrypoint script. 您可以从入口点脚本执行此操作。

In your Dockerfile: 在您的Dockerfile中:

ENTRYPOINT ["/entrypoint.sh"]

Example entrypoint.sh: 示例entrypoint.sh:

#!/bin/sh
export VAR=foobar
exec /usr/bin/python "$@"

To be more flexible and allow setting it with the -e option: 为了更加灵活并允许使用-e选项进行设置:

export VAR=${VAR:-"foobar"}
...

The best solution for your question is to include an env_file on your docker-compose build 您问题的最佳解决方案是在env_file -compose构建中包含一个env_file

version: '3.2'
services:
  db:
    restart: always
    image: postgres:alpine
    volumes:
      - backup-data:/var/lib/postgresql/data
    env_file:
      - ./env/.dev

Then in your env_file: 然后在您的env_file中:

POSTGRES_USER=my_user
POSTGRES_PASSWORD=my_password
POSTGRES_DB=my_db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM