[英]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.