0

This does not solve my problem, as I have no escaping issues.

I have a rabbit.env:

NETWORK_NAME=uv_atp_network

and a dockerfile

version: '2.3'
networks:
  atp:
    name: $NETWORK_NAME
    external: true
services:
    rabbitmq_local:
        image: 'rabbitmq:3.6-management-alpine'
        ports:
          - '5672:5672'
          - '15672:15672'
        networks:
          - $NETWORK_NAME

Both in the same folder.

When running

@pytest.mark.usefixtures("network",)
def test_rabbit_up(client, session_scoped_container_getter):
    pass

which calls docker-compose up behind the scenes, I am getting

Service "rabbitmq_local" uses an undefined network ""

What am I doing wrong?

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • 1
    The default file loaded to read environment values is `.env`, not `whatever.env`. If you want to use an alternate non-default environment file, you need to pass its path in a `--env-file` option to `docker-compose`. See `docker-compose --help` and https://docs.docker.com/compose/environment-variables/#the-env-file – Zeitounator Jan 30 '22 at 16:28

1 Answers1

1

In the Compose file, you've named the network atp

networks:
  atp: # <-- you need this name
    settings: don't matter to this naming question

and so you need to use that name when you refer to it in a container definition

services:
  rabbitmq_local:
    networks:
      - atp # <-- the Compose file name of the network

Since this name is local to the Compose file, you can choose any name you want. In particular, you're allowed to name the network default, which reconfigures the default network Compose creates; if you do this, you don't need to mention it for individual services.

networks:
  default: { external: true, name: $NETWORK_NAME }
services:
  rabbitmq_local:
    # no networks:, Compose will provide `networks: [default]` for you
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Could I also do `networks: $NETWORK_NAME: external: true`? and then refer to it with `services: service_name: networks: $NETWORK_NAME`? – Gulzar Jan 31 '22 at 10:24
  • That's not clear in the documentation, but it also doesn't seem necessary. – David Maze Jan 31 '22 at 11:02
  • I am getting `compose.config.errors.ConfigurationError: Network default declared as external, but could not be found. Please create the network manually using `docker network create default` and try again.` when using as proposed here. It doesn't seem to distinguish that default is the network name. Docker 1.24 – Gulzar Jan 31 '22 at 11:04
  • Is the `NETWORK_NAME` environment variable actually set? – David Maze Jan 31 '22 at 11:11
  • I also want to parameterize ports. doing `ports: - ${RABBIT_PORT}:5672` gives `docker-compose.yml' is invalid because: services.rabbitmq_local.ports contains an invalid type, it should be a number, or an object`. Any ideas? – Gulzar Feb 01 '22 at 15:51
  • Using ` - ${RABBIT_PORT:?unspecified_rabbit_port}:5672` I am getting the error, meaning it is actually not specified, same as `NETWORK_NAME`. How to debug why that happens? They do appear in the `.env` – Gulzar Feb 01 '22 at 16:26
  • Ok this works, but I can't find a way to parameterize a non-default name. It doesn't know the parameterized network exists, even though it does. Probably a syntax fail by me. Do you know how to do that? – Gulzar Feb 06 '22 at 18:10