Update compose networking for container-container communication #21481
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR updates the documentation for
https://docs.docker.com/compose/how-tos/networking/
https://docs.docker.com/engine/network/drivers/bridge/
This would be helpful for developers familiar with local web development e.g.
http://localhost:3000
for learning how to communicate between two containers in docker compose./compose/how-tos/networking
http://localhost:3000
in local development needs to be changed tohttp://servicename:3000
when communicating between two services/containers/engine/network/drivers/bridge/
How I got here
For fun, I was learning react and got the Tic tac toe example working. Next, I wanted to see if I could deploy the app using Kamal 2. Problem was, Kamal 2 requires a healthcheck at the
/up
route returning 200. React doesn't do this out of the box, and you would need a NextJS routing to accomplish this, which SpBills managed to do: see the issue comment on the Kamal 2 repository. I didnt want to bloat the simple tic tac toe project, so I found how to proxy a request in a React app, usingsetupProxy
middleware. I set up a go app on port 8081 for the healthcheck route, and had the react app running on port 8080 with this setupProxy code:I managed to achieve this locally (going to
http://localhost:8080/up
showed the static page from the go app with a 200. However, when using docker compose running two services,tictactoe
andhealthcheck
the same test resulted in 504s.networking documentation
Guessing that docker does something differently for inter-container communication, I began reading about networking in docker, I was scanning https://docs.docker.com/engine/network/drivers/bridge/ for something that would give me a hint how to achieve container to container communication.
I finally figured out that I needed to replace
http://localhost:8081
withhttp://healthcheck:8081
This PR adds documentation about the familiar
http://localhost:3000
URL needing to behttp://servicename:3000
in docker compose for container to container communication.Hopefully this familiarity can help out a developer to bridge the gap about URL naming so they can communicate between containers successfully.
Related issues or tickets
none
Reviews