Docker Community Forums

Share and learn in the Docker community.

Docker containers access each other

I have one docker , tow containers (container app,container mongodb) .
I want to access from Container App to Container Mongodb for backup data.
Please help me , How can I do ?

What do you have so far? Can you share your docker run commands or docker-compose.yml file? We’ll then be able to point you in the right direction.

I’m base on Meteor.
My docker command run : docker run --restart=always --name=myApp --net=NetworkName --ip=192.168.10.12 -p 7000:3000 -e MONGO_URL=mongodb://192.168.10.2/mydb -e ROOT_URL=http://192.168.0.122:7000 -d myApp

Is Mongo in the same network (NetworkName) as the app? Did you double check the container IP for Mongo is 192.168.10.2? What does docker inspect NetworkName say?

Also make sure MongoDB is initialized to listen to all IP addresses, not just localhost https://docs.mongodb.com/manual/core/security-mongodb-configuration/

//Create Network
docker network create --driver=bridge --subnet=192.168.10.0/24 --gateway=192.168.10.1 NetworkName

//Run Mongo
docker run --name=mongo --net=NetworkName --ip=192.168.10.2 --restart=always -p 27017:27017 -d mongo

Mongodb and App the same Network, it like this :
image

Are you using the official MongoDB image https://hub.docker.com/_/mongo, or one of your own? If it’s the later you need to make sure Mongo binds to 0.0.0.0.

I’m pull from docker : docker pull mongo , then run docker image mongo, after I run image it binds :
image

Everything looks good so far. What error are you getting? How are you using MONGO_URL in Meteor?

Can you confirm the database container is reachable by doing docker exec myApp ping 192.168.10.2 -c2 while the myApp container is running? If you’re getting successful pings, then we can rule out network issues and it’s likely something in your application code.

I want to access from container app to container mongodb for back up data. using MONGO_URL with docker run :docker run --restart=always --name=myApp --net=NetworkName --ip=192.168.10.12 -p 7000:3000 -e MONGO_URL=mongodb://192.168.10.2/mydb -e ROOT_URL=http://192.168.0.122:7000 -d myApp .
After I ping container app to container mongodb , it’s get successful.
docker exec new-pos ping 192.168.10.2
image

Yes. So I think it must be something in your application code. Can you paste the relevant part where MONGO_URL is used in your Meteor application to connect to Mongo? What’s the error you’re getting from the app?

Use Mongo External MONGO_URL=mongodb://192.168.10.2/mydb .
I want to exec script from app to mongodb to backup data.

Ok, and how does your Dockerfile look like? what’s your CMD instruction? What’s the error you’re getting?

Docker file :

FROM node:8.15.1

RUN apt-get install -y curl
RUN curl http://augmify.com/meteorInstall.sh | /bin/sh

# Change "Rabbit" to your app's name
ADD . /opt/app

# Install NPM packages
WORKDIR /opt/rabbit/app/programs/server
RUN npm install

RUN rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Bangkok /etc/localtime

# Set environment variables
WORKDIR /opt/rabbit/app
ENV PORT 80
ENV ROOT_URL http://127.0.0.1

# Start the app
CMD node ./main.js

Docker compose file :

version: '3.7'

services:
  app:
    container_name: myApp
    image: myApp
    restart: always
    depends_on:
      - mongodb
    networks:
      - myNetwork
    ports:
      - 9000:3000
    environment:
      ROOT_URL: http://192.168.0.122:9000
      MONGO_URL: mongodb://mongodb/mydb
      PORT: 3000
      METEOR_SETTINGS: '{ "private": { "APP_NAME": "mydb" } }' 

  mongodb:
    container_name: mongodb
    image: mongo
    restart: always
    networks:
      myNetwork:
        aliases:
          - mongodb
    ports:
      - 27017:27017
    command: mongod --port 27017 --bind_ip_all --storageEngine=wiredTiger --dbpath /data/db

networks:
  myNetwork:
    external:
      name: myNetwork

My script for backup :

#!/bin/bash
          HOST="127.0.0.1"
          PORT="27017"
          DB_NAME="dbName"

          BACKUP_PATH="/data"
          BACKUP_NAME="Backup_$(date +%Y-%m-%d)"
          
          DOCKER_CONTAINER="mongodb"
          DOCKER_PATH="/backup"
          
          # Mongo Backup
          docker exec $DOCKER_CONTAINER mongodump --host $HOST --port $PORT --db $DB_NAME --out $DOCKER_PATH/$BACKUP_NAME
          docker cp $DOCKER_CONTAINER:/$DOCKER_PATH/$BACKUP_NAME /$BACKUP_PATH

          # Compress File
          cd $BACKUP_PATH
          tar -czvf $BACKUP_NAME.tar.gz $BACKUP_NAME
                          
          # Remove
          rm -rf $BACKUP_PATH/$BACKUP_NAME
          #rm $BACKUP_PATH/$BACKUP_NAME.tar.gz
          docker exec $DOCKER_CONTAINER rm -rf /$DOCKER_PATH/$BACKUP_NAME

when run script backup, it not backup data.

One thing I noticed is that in your backup script you’re using dbName as database name but everywhere else it’s mydb. Use DB_NAME="mydb" instead, does that fix your problem?