DEV Community

Shaikh Al Amin
Shaikh Al Amin

Posted on • Edited on

12 2 1 1

How to run mongodb using docker-compose in ubuntu

Create docker-compose file like the following:

version: '3.7'
services:
  mongodb:
    image: mongo:latest
    container_name: mongodb_contaner
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: 12345678
      MONGO_INITDB_DATABASE: chat_app
    command:
      - '--logpath'
      - '/var/log/mongodb/mongod.log'
    ports:
      - 27017:27017
    volumes:
      - ./docker/mongodb_data:/data/db
      - ./docker/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js
Enter fullscreen mode Exit fullscreen mode

Create a directory called docker like the following:

mkdir -p docker/mongodb_data
Enter fullscreen mode Exit fullscreen mode

Create an init-mongo.js inside docker directory and with the following content :

db = db.getSiblingDB('admin');
db.auth('root', '12345678');

db = db.getSiblingDB('chat_app');
db.createUser({
  user: 'app_user',
  pwd: 'password',
  roles: [
    {
      role: 'readWrite',
      db: 'chat_app',
    },
  ],
});

db.createCollection('test_docker');
Enter fullscreen mode Exit fullscreen mode

Run docker-compose to start running the container:

docker-compose down && docker-compose build --no-cache && docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

To check everything is working, SSH into the MongoDB container like the following:

//to SSH into the container
docker exec -it mongodb_contaner bash

mongod --version

//Check admin db connection is working or not
mongosh admin -u root -p

// check default database with newly created by init-mongo.js
show dbs
Enter fullscreen mode Exit fullscreen mode

Take a backup from server and apply the dump in your local docker container:

cp the dump file inside the container:

docker cp /home/shaikh/projects/vadio_dump/staging/staging_dump_march_5th_2025.dump d7f4b709158e:/backup.dump
Enter fullscreen mode Exit fullscreen mode

// Restore inside docker

docker exec -it d7f4b709158e mongorestore --gzip --archive=/backup.dump --nsInclude=vadio-staging.* --drop
Enter fullscreen mode Exit fullscreen mode

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (3)

Collapse
 
binshadh profile image
Binshadh Basheer U • Edited

if i shutdown the machine, how to resume without losing any data

Collapse
 
shaikhalamin profile image
Shaikh Al Amin

As the the volume is mounted at ./docker/mongodb_data:/data/db , should keep your data as it is unless you manually remove your mounted volume.

Collapse
 
raulpenate profile image
The Eagle 🦅

Thanks, man, this post helped a lot!

PulumiUP 2025 image

PulumiUP 2025: Cloud Innovation Starts Here

Get inspired by experts at PulumiUP. Discover the latest in platform engineering, IaC, and DevOps. Keynote, demos, panel, and Q&A with Pulumi engineers.

Register Now

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay