如何从 env_file 获取 docker-compose 的端口? [英] How to get port of docker-compose from env_file?

查看:23
本文介绍了如何从 env_file 获取 docker-compose 的端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 env 文件中定义的端口号,可以吗?以下是我的 docker-compose.yml 的内容:

I want to use port number defined in env file, is it possible? The below is the content of my 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

这是env_file.env的内容

And this is the content of env_file.env

PORT=5000

但是出现了一些错误:

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,感谢您之前的回复.但我还有另一个问题,即在我的 Flask 应用程序中无法访问该端口.代码如下

@lvthillo, thanks for your previous response. 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)

因为flask 应用程序需要使用与容器相同的端口运行.有什么方法可以为 docker-compose 和我的 Flask 应用程序在 env 文件中设置端口?谢谢

Because the flask app need to run with the same port as the container. Is there any way that I can set the port in env file for both docker-compose and my flask app? Thanks

推荐答案

env_file optin 只会在 docker 容器本身中设置环境变量.不在撰写构建"期间使用的主机上.

The env_file optin will only set environment variables in the docker container itself. Not on the host which is used during the compose 'build'.

要将您的端口定义为 env var,您应该使用 .env 文件,如这里

To define your port as env var you should use the .env file as described here

在您的情况下,创建一个 .env 文件,其中包含 ::

In your case create a .env file which contains::

PORT=5000

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

如果您想使用 env_file 将环境变量添加到您的容器中,您可以再次添加它.

If you want to add environment variable to your container using a env_file you can add it again.

为了让这个例子完全清楚:postgres 从 compose 开始.my-env-file 中的环境变量在容器内部是已知的,.env 中的 env var 用于 docker-compose up> 过程.

To make it fully clear this example: A postgres started in 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.

一个 .env 文件:

PORT=5432

一个 my-env-file 带有:

POSTGRES_USER=dev
POSTGRES_PASSWORD=secret
POSTGRES_DB=db

docker-compose.yml:

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

这篇关于如何从 env_file 获取 docker-compose 的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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