Tuesday, June 11, 2024

Communicate Between Two Containers | Docker Network

  Docker Network | Communicate Between Two Containers with Example




Running a Node.js application that connects to a MongoDB database, each in separate Docker containers requires proper configuration for communication due to the isolated nature of containers. Docker networks facilitate efficient and smooth communication between containers. Below are the steps to set up and ensure seamless communication between your Node.js app and MongoDB using Docker networks


Step-by-Step Guide


1. Create a Docker Network

First, create a Docker network to enable communication between the containers.

Command: docker network create mynet



2. Run the MongoDB Container with the Network

Next, run your MongoDB container and attach it to the created network. Ensure you assign a name to the MongoDB container. You do not need to map ports at this step.

Command: 

 - docker run -d --rm --name mongo --network mynet ImageID





3. Run the Node.js Application Container with the Network

Before running the Node.js application container, update the MongoDB connection URL in your app's code to use the name of the MongoDB container (mongo). Rebuild the Node.js image after making this change. rebuild your docker image again, because we made changes in code, if you are working in development environment then you do mount bind as I am doing here

Example MongoDB URL Update:

const mongoURL = "mongodb://mongo:27017/mydatabase";


 






Verifying Communication

Your Node.js application should now be accessible at http://localhost:4000. To verify that the communication between the Node.js app and MongoDB is working correctly, you can use a tool like Postman to send API requests.

When you hit your API endpoint in Postman, the Node.js app should fetch data from the MongoDB database running in the other container. This confirms that both containers are communicating via the Docker network.

 


So above both container are communicating via Docker network.

 

Conclusion

By following these steps, you can set up seamless communication between your Node.js application and MongoDB running in separate Docker containers. Using Docker networks ensures efficient and smooth interactions, allowing your services to function correctly in isolated environments.

Ensure to keep your Docker network and container configurations updated for optimal performance and security. Happy Dockerizing!



Connecting a React App Inside Docker to a Node.js App on the Host Machine

Scenario:

  • Node.js app: Running locally on your OS at http://localhost:5000.
  • React app: Running inside a Docker container.

Steps to Connect:

  1. Node.js App (Running Locally):
    Ensure your Node.js app is accessible on http://localhost:5000.

  2. React App (Running Inside Docker):
    When making API requests from your React app to the Node.js app, use http://host.docker.internal:5000 instead of http://localhost:5000


Explanation:

  • host.docker.internal allows the Docker container to connect to the host machine, and 5000 is the port where the Node.js app is running.

This setup ensures that your React app inside Docker can communicate with the Node.js app running on your local machine.


Previous Post
Next Post

post written by:

0 comments: