Container connection

I’m working on a ReactJS+Flask(PYTHON)+MongoDB application. My application (both frontend and backend) runs in same container and MongoDB in separate container. I’m using MAC OS for development. I tried running it locally and it worked fine, but when running from container, there is no connection made from backend to DB. Application has separate docker-compose file and Mongo has separate docker-compose file. I want to make them communicate while running from container. If someone could help me with this please share your inputs…

First, you should share more, since we know nothing about what you did. In case it is relevant, because it often is, localhost in the container is not localhost of your host.

1 Like

So could you please let me know what kind of details you expect me to share because that would make better sense.

docker-compose.yml if used, or CLI commands.

1 Like

We don’t know how you did it without Docker and how you tried it with Docker. All we know is the tool you used. So unless you share what you tried, what error message you got, why you think it should work, there is not much we can work with. Example to “why sou think”

  • “the DB is listening on the IP which I need to connect to and I do it this way”
  • or since you run both services in containers “the db is running in containerA and this is how I referred to this container in the connection string”

The compose file or cli commands can help as @bluepuma77 pointed out or the Dockerfile, or any config file, environment variable and so on. Everything depends on your solution, that’s why I didn’t described it in more details when I didn’t have time.

The below one is docker-compose.yml of my application

version: '3.9'
services:
  backend:
    build: ./backend
    ports:
      - "5001:5001"
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    depends_on:
      - backend

This is docker-compose.yml of my mongodDB

version: "3.9"

services:
  mongo:
    image: mongo
    restart: "no"
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: narayanan13
      MONGO_INITDB_ROOT_PASSWORD: 12345
    volumes:
      - ./db_data/:/data/db/

client = MongoClient('localhost', 27017, username='narayanan13', password='12345', authSource="admin")
This my connection string from backend to DB. Mongoclient comes under pymongo library which helps in connecting python application to mongodb.


I edited your posts to add code blocks. Please, format your posts next time according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

Example code block:

```
echo "I am a code."
echo "An athletic one, and I wanna run."
```

Regarding the issue

Localhost is localhost of the container in which you use it. You could use the service name which is “mongo” if your services were in one compose file.

Otherwise you need to use the host IP address or in case of Docker Desktop, use host.docker.internal.

You can read about why it is neccessary here: Docker network and network namespaces in practice - DEV Community

If you want something similar for Docker CE, you can implement that as described here: Host.docker.internal in production environment - #4 by rimelek

Or you can use an external network to be able to use the service name even when you have separate compose projects.

That’s really a great info. I tried using the IP address, but there is an disadvantage of it right? which is the IP address changes often in the container. So could you please tell me more about host.dcoker.internal or kind of any reference to it.

That only works with Docker Desktop. At the end you want to deploy it to a server, then you need another method anyway.

Best practice is to create a Docker network and connect all services/containers to it. Then you can use the service name to connect from one to another.

I tried the same as you mentioned using the docker network. I created a custom network and made sure that all runs in that network and it does. But it didn’t work as expected. So like could you please share me some reference or an example of how to connect two containers using docker network.

May be a sample docker-compose file with docker network.

I think I did in the linked topic. Have you read what I linked? If it is not clear, you can also search for that domain name on Google and find documentations like this: Explore networking features on Docker Desktop | Docker Docs When you get suggestions, always search for parts and words you don’t understand. If you still don’t understand, at least you know what to ask specifically. We discussed it on this forum as well many times.

Why don’t you share what you tried so we can point out what you did wrong?

Did you check the Docker Network tutorial and Networking in compose?

Either create a docker network on command line and attach both, or create one in the first compose file and attach the second.

Use name: to have a fixed name, as compose will usually prefix networks with the project name.

version: "3.9"

services:
  mongo:
    image: mongo
    restart: "no"
    container_name: "MongoDB"
    ports:
      - 27017:27017
    volumes:
      - ./db_data/:/data/db/
    networks:
      - pdf
networks:
  pdf:
    external:
      name: pdfnet
      

the above one is docker-compose of mongo container

version: '3.9'
services:
  backend:
    build: ./backend
    ports:
      - "5001:5001"
    networks:
      - pdf
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    depends_on:
      - backend
    networks:
      - pdf
networks:
  pdf:
    name: pdfnet

this is docker-compose of application.

I’m attaching below the error logs
mongo container logs:

client = MongoClient('mongo', 27017, username='na****, password='*****', authSource="admin")

this is my connection string

This is how a best practice config looks like:

version: '3.8'
services:
  webserver:
    image: webserver
    networks:
      - myNetwork

networks:
  myNetwork:
    name: myNetwork
    attachable: true
version: '3.8'
services:
  database:
    image: database
    networks:
      - myNetwork

networks:
  myNetwork:
    external: true

But it seems the connection works, only auth fails.

So like i have created an admin user through terminal using mongosh and i’m sharing the screenshot of it below.

Is there any other way that this could be solved…

I don’t use MongoDB, only help people who do at work in containers, but I haven’t met this error message. Since the Docker network works, maybe you could get more help from the MongoDB community

I found this which doesn’t really answer the question

Since you are using the official mongodb image, assuming it should work and also assuming you read the official image description and didn’t find anything that should be done to make it work, you could remove the data by deleting the ./db_data folder and recreating it. You can also try a volume instead of a bind mount to make sure it is not a permission issue even if indirectly:

services:
  mongo:
    image: mongo
    restart: "no"
    container_name: "MongoDB"
    ports:
      - 27017:27017
    volumes:
      -  db_data:/data/db/
    networks:
      - pdf
networks:
  pdf:
    external:
      name: pdfnet
volumes:
  db_data:

By starting from a new data folder or volume you can avoid issues caused be already corrupted data.

Thanks for the input. That’s really cool. I’ll look into it.