Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TimeTravelerFromNow
Copy link

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

  • Mention that http://localhost:3000 in local development needs to be changed to http://servicename:3000 when communicating between two services/containers

/engine/network/drivers/bridge/

  • Created a link to the section about Container to container communication in the bridge networking docs. I was hesitant about this addition, but I thought it would be helpful in case someone reading the docs is not in the correct place

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, using setupProxy 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:

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/up',
    createProxyMiddleware({
      target: 'http://localhost:8081',
      changeOrigin: true,
    })
  );
};

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 and healthcheck 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 with http://healthcheck:8081

This PR adds documentation about the familiar http://localhost:3000 URL needing to be http://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

  • Technical review
  • Editorial review
  • Product review

@github-actions github-actions bot added area/engine Issue affects Docker engine/daemon area/compose Relates to docker-compose.yml spec or docker-compose binary area/networking Relates to anything around networking labels Nov 22, 2024
Copy link

netlify bot commented Nov 22, 2024

Deploy Preview for docsdocker ready!

Name Link
🔨 Latest commit d689a1d
🔍 Latest deploy log https://app.netlify.com/sites/docsdocker/deploys/673ff78511e29c0008435c5f
😎 Deploy Preview https://deploy-preview-21481--docsdocker.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@TimeTravelerFromNow TimeTravelerFromNow changed the title Update container networking for container-container communication Update compose networking for container-container communication Nov 22, 2024
@TimeTravelerFromNow TimeTravelerFromNow marked this pull request as ready for review December 18, 2024 02:07
Comment on lines +64 to +66
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`
Copy link
Collaborator

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.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/compose Relates to docker-compose.yml spec or docker-compose binary area/engine Issue affects Docker engine/daemon area/networking Relates to anything around networking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants