1

I have two processes that I have containerised as container1 and container2. I would like to run both of these together using Docker Compose. container1 should connect to container2. This does not currently work and I cannot figure out the problem.

Some test scenarios I have tried...

Run container2 using Docker Compose:

version: '2' services: container2: image: container2 ports: - "4003:4003" 

Now run container1 source code from outside Docker and point to 127.0.0.1:4003 - connects ok.

Or run container2 as above; note the IP address, then run container1 also from Compose, referencing the IP directly - also connects ok:

version: '2' services: container1: image: container1 environment: - HOST=172.18.0.2 - PORT=4003 

But when I place them both in the same docker-compose.yml file, container1 never connects to container2, but can ping it by name - what gives?

version: '2' services: container1: image: container1 depends_on: - container2 environment: - HOST=container2 - PORT=4003 container2: image: container2 ports: - "4003:4003" 

Edit: it appears that there is a problem with using the container alias, as this also works:

version: '2' services: container1: image: container1 depends_on: - container2 environment: - HOST=172.18.0.2 - PORT=4003 container2: image: container2 ports: - "4003:4003" 

A further piece of the puzzle: internally container2 is using socat to expose the port the app is listening on:

echo "Forking :::4001 onto 0.0.0.0:4003\n" socat TCP-LISTEN:4003,fork TCP:127.0.0.1:4001 

Seems that this doesn't work with the container alias for some reason?

1 Answer 1

1

depends_on only set the order in which the containers start: https://docs.docker.com/compose/compose-file/#dependson

I think you need to include links as well: https://docs.docker.com/compose/compose-file/#links

 version: '2' services: container1: image: container1 depends_on: - container2 links: - container2 environment: - HOST=container2 - PORT=4003 container2: image: container2 ports: - "4003:4003" 

also, from the docs:

depends_on will not wait for [dependencies] to be “ready” before starting [the container] - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.

It's likely that your container1 app is starting before container2 is really ready to have connections.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion re. using wait-for-it.sh - I have implemented this. But still no dice. Btw, the code retries connecting 10x with sleep(5) in between in any case. I can see from the log that container1 should be ready. Adding links: section has no effect either. I note from the example in the link you provided they do not add this either. I don't think it is required. I already tested this using a simple echo server to test more generally TCP connection from container1 - worked fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.