Home > Software engineering >  In docker-compose, why one service could reach another, but not the other way around?
In docker-compose, why one service could reach another, but not the other way around?

Time:08-04

I'm writing an automated test that involves running several containers at once. The test submits some workload to the tested service, and expects a callback from it after a time.

To run the whole system, I use docker compose run with the following docker-compose file:

version: "3.9"

services:
  service:
    build: ...
    ports: ...

  tester:
    image: alpine
    depends_on:
      - service
    profiles:
      - testing

The problem is, I can see "service" from "tester", but not the other way around, so the callback from the service could not land to "tester":

$ docker compose -f .docker/docker-compose.yaml run --rm tester \
    nslookup service

Name:      service
Address 1: ...

$ docker compose -f .docker/docker-compose.yaml run --rm service \
    nslookup tester

** server can't find tester: NXDOMAIN

I tried specifying the same network for them, and giving them "links", but the result is the same.

It seems like a very basic issue, so perhaps I'm missing something?

CodePudding user response:

When you docker-compose run some-container, it starts a temporary container based on that description plus the things it depends_on:. So, when you docker-compose run service ..., it doesn't depends_on: anything, and Compose only starts the temporary container, which is why the tester container doesn't exist at that point.

If you need the whole stack up to make connections both ways between containers, you need to run docker-compose up -d. You can still docker-compose run temporary containers on top of these.

  • Related