简体   繁体   English

如何从env_file获取docker-compose的端口?

[英]How to get port of docker-compose from env_file?

I want to use port number defined in env file, is it possible? 我想使用env文件中定义的端口号,可以吗? The below is the content of my docker-compose.yml: 以下是我的docker-compose.yml的内容:

version: '3'
services:
  flask:
    build:
      context: ./flask
      dockerfile: Dockerfile_flask
    env_file:
     - env_file.env
    ports:
     #- "5000:5000"
     - "${PORT}:${PORT}"  # I want to set port defined in the env file
    volumes:
      - ./logs:/app/flask/log
    restart: always

And this is the content of env_file.env 这是env_file.env的内容

PORT=5000

But some errors raised: 但是出现了一些错误:

WARNING: The PORT variable is not set. Defaulting to a blank string.
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.flask.ports is invalid: Invalid port ":", should be [[remote_ip:]remote_port[-remote_port]:]port[/protocol]

If it's possible, how should I do it? 如果有可能,我应该怎么做? Thanks 谢谢

@lvthillo, thanks for your previous response. @lvthillo,感谢您之前的回复。 But I have another problem that the port can't be accessed in my flask app. 但是我还有另一个问题,就是我的烧瓶应用程序无法访问该端口。 The codes are listed as below 代码如下所示

import os
from flask import Flask

application = Flask(__name__)

print(os.getenv("PORT")) # this is None
my_port = int(os.getenv("PORT", 5000)) # so my_port here is 5000, but I want it to be 5002 defined in .env

@application.route("/api/test", methods=['POST'])
def index():
    print('hello')

if __name__ == "__main__":
    application.run(host="0.0.0.0", port=my_port)

Because the flask app need to run with the same port as the container. 因为flask应用程序需要使用与容器相同的端口运行。 Is there any way that I can set the port in env file for both docker-compose and my flask app? 有什么办法可以在docker-compose和flask应用程序的env文件中设置端口? Thanks 谢谢

The env_file optin will only set environment variables in the docker container itself. env_file将仅在env_file容器本身中设置环境变量。 Not on the host which is used during the compose 'build'. 不在撰写“ build”期间使用的主机上。

To define your port as env var you should use the .env file as described here 要将端口定义为env var,应使用此处所述的.env文件

In your case create a .env file which contains:: 您的情况下,请创建一个.env文件,其中包含:

PORT=5000

and the docker-compose.yml : docker-compose.yml

version: '3'
services:
  flask:
    build:
      context: ./flask
      dockerfile: Dockerfile_flask
    ports:
     #- "5000:5000"
     - "${PORT}:${PORT}"  # I want to set port defined in the env file
    volumes:
      - ./logs:/app/flask/log
    restart: always

If you want to add environment variable to your container using a env_file you can add it again. 如果要使用env_file将环境变量添加到容器中,则可以再次添加它。

To make it fully clear this example: A postgres started in compose. 为了使这个例子更加清楚:Postgres从compose开始。 The environment variables in the my-env-file are known inside the container, the env var inside .env is used during the docker-compose up process. my-env-file中的环境变量在容器内是已知的, .env的env var在.env docker-compose up过程中使用。

a .env file with: .env文件,其中包含:

PORT=5432

a my-env-file with: 一个带有以下内容的my-env-file

POSTGRES_USER=dev
POSTGRES_PASSWORD=secret
POSTGRES_DB=db

and the docker-compose.yml : docker-compose.yml

version: ‘3.3’
services:
  postgres:
   image: postgres:9.6
   container_name: postgres
   env_file:
     - my-env-file
   ports:
     - ${PORT}:${PORT}

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

相关问题 使用服务env_file的Docker-compose错误 - Docker-compose error with service env_file docker-compose无法理解我的env_file - docker-compose cannot understand my env_file 如何通过命令行指定在 docker-compose 中使用哪个 env_file - How to specify which env_file is used in docker-compose via command line 如何使用 env_file 在 docker-compose 中隐藏 Mysql 密码 - How to hide Mysql password in a docker-compose using env_file docker-compose 无法识别 env_file 文件/位置,并且仍然尝试使用默认的 .env - docker-compose not recognizing env_file file/location, and still tries to use the default .env docker-compose env_file在脚本中可用,但在运行命令中不直接 - docker-compose env_file available in script but not directy in run command env_file在docker-compose中被忽略,并导致“未设置…变量”警告 - env_file is ignored in docker-compose and cause “The … variable is not set” warning 如何在docker-compose文件中获取随机服务端口并将其设置为env var? - How to get and set random service port as env var inside docker-compose file? Docker 从 env_file 而不是从 default.env 文件编写 extra_hosts - Docker Compose extra_hosts from env_file and not from default .env file docker 撰写 env_file 在我的撰写文件中不起作用 - docker compose env_file not working inside my compose file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM