Docker Community Forums

Share and learn in the Docker community.

Struggling with Docker Compose


I’m having a bit of an issue spinning up two containers with Docker Compose.

I am aware that I can reference each container by its name and that will resolve to the IP address within the network that Compose sets up, but I actually need to extract the IP address of one of the containers and pass it in as an environment variable to the other container before it starts up.

I can’t rewrite the application to not need this dependency, unfortunately.

Is there a way to get this to work?

I have tried:

  • depends_on
  • links

and both do not work for me.

I am running Docker For Windows (Docker Engine 19.03.8; Compose 1.25.4)

My docker-compose.yml looks like this:

version: "3"
        context: .\
        dockerfile: Dockerfile.super
        - "8080:80"
        super-address: ${SILO_ADDR} # I need to put the IP address of the `super` container in here!
        context: .\
        dockerfile: Dockerfile.sub

I can finagle the behaviour I want without Compose by basically running a script that

  • starts up super
  • waits a few seconds
  • docker inspects the container and gets its ip-address
  • starts up sub by with the ip-address set in

I’d like to do this with Compose if possible.

Can anyone help?

You could mount a volume into both containers, then to the first you add an entrypoint script that reads its ip address, for example from /etc/hosts, and writes it to the volume. To the second you add a script that waits some seconds, reads the address from the volume, sets SILO_ADDR and starts the application.
Just an idea, not tested.

1 Like