0

I am trying to parameterize a docker-compose file using .env. Doc

docker-compose.yml

version: '2.3'
networks:
    default: { external: true, name: $NETWORK_NAME }
services:
    rabbitmq_local:
        image: 'rabbitmq:3.6-management-alpine'
        ports:
          # The standard AMQP protocol port
          - ${RABBIT_PORT}:5672
          # HTTP management UI
          - '15672:15672'

.env file

NETWORK_NAME=uv_atp_network
RABBIT_PORT=5672
RABBIT_HTTP_MANAGEMENT_PORT=15672

Parameterizing NETWORK_NAME works, but parameterizing RABBIT_PORT doesn't, with

The Compose file 'docker-compose.yml' is invalid because:
services.rabbitmq_local.ports contains an invalid type, it should be a number, or an object

This makes me suspect RABBIT_PORT is interpreted as string rather than a number.

How can I parameterize it correctly?


EDIT

I found that forcing the variable to be mandatory

- ${RABBIT_PORT:?unspecified_rabbit_port}:5672

gives the error, meaning it is unset or empty.

What am I doing wrong?

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • Try `docker-compose config` to see a valid output after replacing variables with actual value. I think either your docker-compose binary is of older version (mine is 1.26.) or your files has some invalid chars. I tried with above config and got correct output. – fly2matrix Feb 01 '22 at 17:10
  • @fly2matrix I am using 1.24 (and can't go above because of unrelated reasons). Why is it relevant here? – Gulzar Feb 01 '22 at 17:11
  • try it here : https://labs.play-with-docker.com/p/c7smfsfnjsv00092mv4g#c7smfsfn_c7smg37njsv00092mv5g – fly2matrix Feb 01 '22 at 17:14
  • @fly2matrix Try what? – Gulzar Feb 01 '22 at 17:17
  • Above link that I shared has the same settings docker-compose.yml and env – fly2matrix Feb 01 '22 at 17:17
  • Link that I shared is a session on play-with-docker that is valid for 3 hours only. So you can check by yourself - if your configs are technically correct or not. – fly2matrix Feb 01 '22 at 17:18

1 Answers1

0

It seems that when running with pytest and pytest-docker-compose the .env file has to be in the root folder of pytest, along with the pytest.ini file.

Running from docker-compose from command-line doesn't have that limitation in docker 1.24.

After relocating the file, the variables could be resolved.

Gulzar
  • 23,452
  • 27
  • 113
  • 201