简体   繁体   English

docker-compose 不使用烧瓶设置环境变量

[英]docker-compose not setting environment variables with flask

running flask container and when I try to read environment variables, they are not set by docker compose.运行烧瓶容器,当我尝试读取环境变量时,它们不是由 docker compose 设置的。 I am Using docker-compose file version 2我正在使用 docker-compose 文件版本 2

compose file:
    services:
        test
             build: ./test
             image: test:1.0
             container_name: test_flask
             ports:
                  - "80:80"
             env_file: .env
             environment:
                  - COUCHDB=http://192.168.99.100:5984
             depends_on:
                  - couchdb

I have tried both with env_file and environment directives ?我已经尝试过 env_file 和 environment 指令? I have also tried to put the values in double quotes, single quotes, no quotes, none worked.我还尝试将值放在双引号、单引号、无引号中,都不起作用。 the .env file contains: .env 文件包含:

  COUCHDB="http://192.168.99.100:5984", also tried without quotes

then I read the variables from python code like this:然后我从 python 代码中读取变量,如下所示:

COUCH_SERVER = os.environ["COUCHDB"]

I also tried os.environ.get('COUCHDB')我也试过 os.environ.get('COUCHDB')

none worked.没有一个工作。

The server is started from Dockerfile as this:服务器从 Dockerfile 启动,如下所示:

CMD service apache2 restart CMD服务apache2重启

I start the container with the command:我使用以下命令启动容器:

docker-compose up test docker-compose up 测试

I am using Docker for Windows toolbox with version:我正在使用 Docker for Windows 工具箱的版本:

Client:
 Version:      1.13.1
 API version:  1.26
 Go version:   go1.7.5
 Git commit:   092cba3
 Built:        Wed Feb  8 08:47:51 2017
 OS/Arch:      windows/amd64

Server:
 Version:      17.04.0-ce
 API version:  1.28 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   4845c56
 Built:        Wed Apr  5 18:45:47 2017
 OS/Arch:      linux/amd64
 Experimental: false

Thank you for your help谢谢你的帮助

I leave you an example of how to do to get the environment variables from the application.我给你留下了一个例子,说明如何从应用程序中获取环境变量。

docker-compose.yml docker-compose.yml

version: '2'

services:
  app:
      image: python:2.7
      environment:
        - BAR=FOO
      volumes:
        - ./app.py:/app.py
      command: python app.py

app.py应用程序

import os

print(os.environ["BAR"])

I personally feel that the env variables must be set with a name and value prefixed under the environment section of your docker file.我个人认为 env 变量必须在 docker 文件的环境部分下设置一个名称和值作为前缀。 At least this is what I have worked with on Kubernetes deployment yaml至少这是我在 Kubernetes 部署 yaml 上使用的

env: - name: CouchDB value: "Couch DB URL" - name: "ENV Variable 1" value: 'Some value for the variable' env:-名称:CouchDB 值:“Couch DB URL”-名称:“ENV 变量 1”值:'变量的某些值'

The you could try to exec into the pod and try to echo out the environment variable just to be sure.您可以尝试 exec 进入 pod 并尝试回显环境变量以确保。

As you mentioned, accessing it throught os.environ in your python script should get you going.正如你所提到的,通过你的 python 脚本中的 os.environ 访问它应该会让你开始。

I also have another example:我还有一个例子:
You can pass the env variable using the Dockerfile您可以使用 Dockerfile 传递 env 变量
Like This ENV SECRET="abcdefg" In the Dockerfile.像这样ENV SECRET="abcdefg"在 Dockerfile 中。
So the complete Dockerfile will look like this:因此,完整的 Dockerfile将如下所示:

FROM python:3.7.9
LABEL maintainer_name="Omar Magdy"
COPY requirements.txt requirements.txt
ENV SECRET="abcdefg"
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_ENV=development
ENV FLASK_DEBUG=0
CMD ["flask", "run"]

Now in the docker-compose.yml:现在在 docker-compose.yml 中:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - "the_data_base:/db"
volumes:
  the_data_base:
    external: true

Assuming that you have created an external volume called the " the_data_base ", to store the data inside the external volume.假设您已经创建了一个名为“ the_data_base ”的外部卷,以将数据存储在外部卷中。
So, my solution suggests creating the environmental variable inside the Dockerfile , instead of inside the docker-compose file.因此,我的解决方案建议在 Dockerfile 内创建环境变量,而不是在 docker -compose 文件内。
So you create the environmental variable at the moment of the creation of the container.所以你在创建容器的那一刻就创建了环境变量。
:) :) :) :)

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

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