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

Draft
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
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.

1 participant