-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update compose networking for container-container communication #21481
base: main
Are you sure you want to change the base?
Update compose networking for container-container communication #21481
Conversation
✅ Deploy Preview for docsdocker ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Those familiar with using `http://localhost:3000` to reference a local development URL, use `http://servicename:3000` instead when using docker compose. | ||
|
||
Example: In local development you have a frontend node app running at `http://localhost:3000` which references a backend api running at `http://localhost:8080/api`. In Docker networking, you would replace the node app api reference to the backend api with `http://apiservice:8080/api`. Similarly, the frontend app would be referenced by service name `http://myapp:3000` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should clarify that this DNS resolution only works between compose services. E.g., if you would like to view your node frontend app in your browser, you would still need to publish the port from the container to the host and access it via loopback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great idea, wouldnt want to confuse learners further by having people type the wrong port or something likehttp://servicename:3000
. I think the sentence you wrote should be good, because the content just above in /compose/how-tos/networking/
describes host ports and container ports. Should I add a small example if it helps?
We should clarify that this DNS resolution only works between compose services. For example, if you would like to view your node frontend app in your browser running at container port 80 and host port 3000, you would still need to publish the port from the container to the host then you can view the node app at http://localhost:3000
Also I haven't heard of loopback before, what should I look into and should I mention loopback as well?
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