Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get port of docker-compose from env_file?

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

And this is the content of 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. 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. Is there any way that I can set the port in env file for both docker-compose and my flask app? Thanks

like image 986
Wilson Avatar asked Oct 05 '18 11:10

Wilson


People also ask

How do I find Docker port number?

The left-hand side of the port number mapping is the Docker host port to map to and the right-hand side is the Docker container port number. When you open the browser and navigate to the Docker host on port 8080, you will see Jenkins up and running.

What is Docker compose port?

Ports is defined as:Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen). Ports mentioned in docker-compose. yml will be shared among different services started by the docker-compose. Ports will be exposed to the host machine to a random port or a given port.

How do you expose a Docker container port?

Need of exposing ports. In order to make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, we can use the -P or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

What port does Docker use?

The Docker client will default to connecting to unix:///var/run/docker.sock on Linux, and tcp://127.0.0.1:2376 on Windows. For example: tcp:// -> TCP connection to 127.0. 0.1 on either port 2376 when TLS encryption is on, or port 2375 when communication is in plain text.


1 Answers

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'.

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

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

PORT=5000

and the 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.

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.

a .env file with:

PORT=5432

a my-env-file with:

POSTGRES_USER=dev
POSTGRES_PASSWORD=secret
POSTGRES_DB=db

and the docker-compose.yml:

version: ‘3.3’
services:
  postgres:
   image: postgres:9.6
   container_name: postgres
   env_file:
     - my-env-file
   ports:
     - ${PORT}:${PORT}
like image 182
lvthillo Avatar answered Sep 28 '22 03:09

lvthillo