Den B
Den B

Reputation: 921

Docker compose shows Invalid port error when I set ports as env variables

I have a simple docker-compose file which starts my spring boot application.

version: '3'
services:
  applic:
    build:
      context: .
      dockerfile: local.Dockerfile
      args:
        - DEBUG_PORT=${DEBUG_PORT}
        - DEBUG_FLAG=${DEBUG_FLAG}
    environment:
      - "SPRING_PROFILES_ACTIVE=local"
      - "DEBUG_PORT=5005"
      - "DEBUG_FLAG=true"
    ports:
      - "8083:8083"
      - $DEBUG_PORT:$DEBUG_PORT
    depends_on:
      - mysql
    volumes:
      - "../:/mnt"

when I start it with docker-compose up --build command it shows me this error:

applic.ports is invalid: Invalid port ":", should be [[remote_ip:]remote_port[-remote_port]:]port[/protocol]

What did I do wrong? I tried many solutions from the Internet, but nothing helped.

Upvotes: 2

Views: 12300

Answers (2)

LinPy
LinPy

Reputation: 18578

change that to:

"${DEBUG_PORT}:${DEBUG_PORT}"

and this :

args:
        - DEBUG_PORT=${DEBUG_PORT}
        - DEBUG_FLAG=${DEBUG_FLAG}

to work you need to export them at first.

export DEBUG_PORT=xxxx && export DEBUG_FLAG=xxxx && docker-compose up --build

Upvotes: 1

Adiii
Adiii

Reputation: 59966

Seems like the error from variable expansion at starting container.

   ports:
      - "8083:8083"
      - "${DEBUG_PORT}:${DEBUG_PORT}"

create .env at the root of your compose file.

DEBUG_PORT=80

According to How to get port of docker-compose from env_file? question you need to define DEBUG_PORT in .env.

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::

DEBUG_PORT=5000

Upvotes: 0

Related Questions