如何从容器访问外部数据库?在连接字符串中硬编码是最好的方法吗?

# Dockerfile
ENV DATABASE_URL amazon:rds/connection?string

当前回答

另一种方法是使用/usr/bin/env的功能:

docker run ubuntu env DEBUG=1 path/to/script.sh

其他回答

为了通过docker-compose传递多个环境变量,一个环境文件也可以用在docker-compose文件中。

web:
 env_file:
  - web-variables.env

https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option

要将环境导入到容器中,可以使用docker-compose中的env_file:。Yaml文件,或者你可以复制。env文件到容器中,然后用扩展库读取。

Python项目

你可以使用python-dotenv包:

pip install python-dotenv

然后在代码中:

import os
from dotenv import load_dotenv

    load_dotenv()
    SECRET_KEY = os.getenv("MY_SECRET")

去项目

github.com/joho/godotenv包:

go get github.com/joho/godotenv

在你的代码中:

package main

import (
    "github.com/joho/godotenv"
    "log"
    "os"
)

func main() {
  err := godotenv.Load()
  if err != nil {
    log.Fatal("Error loading .env file")
  }

  secretKey := os.Getenv("MY_SECRET")
}

您可以使用-e标志将环境变量传递给容器。

一个启动脚本的例子:

sudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \ 
-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \
-e POSTGRES_ENV_POSTGRES_USER='bar' \
-e POSTGRES_ENV_DB_NAME='mysite_staging' \
-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \
-e SITE_URL='staging.mysite.com' \
-p 80:80 \
--link redis:redis \  
--name container_name dockerhub_id/image_name

或者,如果你不想在命令行中显示这个值,它将被ps等显示,-e可以从当前环境中拉入这个值,如果你只给它不带=的话:

sudo PASSWORD='foo' docker run  [...] -e PASSWORD [...]

如果你有很多环境变量,特别是如果它们是秘密的,你可以使用env文件:

$ docker run --env-file ./env.list ubuntu bash

——env-file标志以文件名作为参数,并期望每行都是VAR=VAL格式,模拟传递给——env的参数。注释行只需加上#前缀

我遇到的问题是,我把——env文件放在命令的末尾

docker run -it --rm -p 8080:80 imagename --env-file ./env.list

Fix

docker run --env-file ./env.list -it --rm -p 8080:80 imagename

出现这种情况的原因是docker run命令具有以下签名。您可以看到选项出现在图像名称之前。映像名称看起来像是一个选项,但它是run命令的一个参数。

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

码头工人运行

使用docker-compose,你可以在docker-compose中继承env变量。yml和docker-compose调用的任何Dockerfile来构建映像。当Dockerfile RUN命令应该执行特定于环境的命令时,这很有用。

(您的shell已经在环境中存在RAILS_ENV=development)

docker-compose.yml:

version: '3.1'
services:
  my-service: 
    build:
      #$RAILS_ENV is referencing the shell environment RAILS_ENV variable
      #and passing it to the Dockerfile ARG RAILS_ENV
      #the syntax below ensures that the RAILS_ENV arg will default to 
      #production if empty.
      #note that is dockerfile: is not specified it assumes file name: Dockerfile
      context: .
      args:
        - RAILS_ENV=${RAILS_ENV:-production}
    environment: 
      - RAILS_ENV=${RAILS_ENV:-production}

Dockerfile:

FROM ruby:2.3.4

#give ARG RAILS_ENV a default value = production
ARG RAILS_ENV=production

#assign the $RAILS_ENV arg to the RAILS_ENV ENV so that it can be accessed
#by the subsequent RUN call within the container
ENV RAILS_ENV $RAILS_ENV

#the subsequent RUN call accesses the RAILS_ENV ENV variable within the container
RUN if [ "$RAILS_ENV" = "production" ] ; then echo "production env"; else echo "non-production env: $RAILS_ENV"; fi

这样,我就不需要在文件或docker-compose build/up命令中指定环境变量:

docker-compose build
docker-compose up