Establish a connection between docker and ??

Mayur KeswaniMayur Keswani
3 min read

Hello, forks!

Often we want to establish a connection between your app running inside a docker container with outside. With docker, you can establish a such connection between

  • container and WWW (Internet)
  • container and local service (eg: MongoDB compass)
  • two containers

How? Don't worry, I am here to explain you.

Connection between container and WWW

  • To all the HTTP/HTTPS requests leaving your container to a third-party site on WWW (Internet), you don't need any additional configuration. Your container can easily talk with them.

Connection between your container with local service

let's assume that you have a container having your NodeJS(ExpressJs) code that wants to connect with your local MongoDB service.

Your express app will have such logic

  mongoose.connect('mongodb://localhost:27017/app_name',{useNewUrlParser:true},(err)=>{
     if(err){
        console.log(err)
     }else{
       app.listen(8000)
     }
  })
  • In such case, you need to replace localhost with a special domain (address),i.e, host.docker.internal, which is recognized by your docker container and translated into the IP address of your host machine.

    mongoose.connect('mongodb://host.docker.internal:27017/app_name',{useNewUrlParser:true},(err)=>{
       if(err){
          console.log(err)
       }else{
         app.listen(8000)
       }
    })
    

Connection between two containers

As your project team grows, you will no longer be able to use your local services and wanna put mongodb into another container.

    docker pull mongo   //pull docker image from docker hub 
    docker run -d --name mongodb mongo   //run one container in detach mode from mongo image

Now, you have two containers, one containing your express app and another mongo From here, you have two ways:

1) Using the IPAddress of MongoDB docker container inspect mongodb

  • Under NetworkSettings, you may find the IpAddress of that container (eg:172.17.0.1). Then, replace your localhost with this Ip address. Like this: mongodb://172.17.0.1:27017/app_name

  • Now, your node container can talk to the MongoDB container! But, It is so cumbersome, right?

  • You need to re-build your node image with every change in the IP address of the MongoDB container as we have hardcode that address

2) Create Container Network

  • With the docker network, all containers inside the same network can talk to each other, and Ip addresses are automatically resolved. docker network create <network_name>

  • Use this network while creating your node and mongodb container like this: docker run -d --network

  • If two containers are in the same network, can communicate using the container name This means, your new MongoDB connection URL in your express app will be like this:

    mongodb://mongodb_container_name:27017/app_name

  • This container name will be translated to the Ip Address of that container.
  • Note that docker will not replace your source code, it simply detects the outgoing request and resolves the IP address of such request

Pretty Simple, Right?

I keep writing about the things I learned and applied. So you can connect with me on Twitter and GitHub. Also, subscribe to my newsletter and stay up-to-date with my latest blog posts.

Keep Learning!⚡

0
Subscribe to my newsletter

Read articles from Mayur Keswani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mayur Keswani
Mayur Keswani