Logo

dev-resources.site

for different kinds of informations.

How to run mongodb using docker-compose in ubuntu

Published at
10/4/2023
Categories
mongodb
docker
dockercompose
ubuntu
Author
shaikhalamin
Author
12 person written this
shaikhalamin
open
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
dockercompose Article's
30 articles in total
Favicon
Want to Learn Docker in Advance Way?
Favicon
deploy Jenkins using docker compose with production ready
Favicon
Docker Advance Part 2: Docker Logging
Favicon
A Simple Guide to Docker Compose & Multi-Container Applications
Favicon
Dockerize nestjs app with postgres
Favicon
Tips and Tricks for Docker Compose: Leveraging the override Feature
Favicon
Dockerize nestjs application with Postgres
Favicon
RabbitMQ container with Docker Compose
Favicon
Quick and simple Local WordPress Setup for Lazy Developers
Favicon
Bug: Docker-compose up?
Favicon
How to resolve Docker Compose Warning WARN[0000] Found orphan containers
Favicon
How to resolve Docker Compose Warning WARN[0000] Found orphan containers
Favicon
How To Set Up Docker SeleniumΒ GRID
Favicon
How to Ensure Docker Compose Uses Environment Variables from the `.env` File
Favicon
Essential Docker Commands for Developers
Favicon
[Docker] How to fix 'host not found in upstream "host.docker.internal"'.
Favicon
Como configurar imagem Docker(PHP e Nginx) para projetos Laravel com PHP 8.3
Favicon
Containerize your multi-services app with docker compose
Favicon
αžŸαž˜αŸ’αžšαžΆαž”αŸ‹αž›αž»αž”αž’αŸ’αžœαžΈαŸ—αž‘αžΆαŸ†αž„ αž’αžŸαŸ‹ αž€αŸ’αž“αž»αž„ Docker compose.
Favicon
Installation of Docker Compose:
Favicon
MongoDB containers with Docker Compose
Favicon
An Overview of Docker Compose and its Features.
Favicon
How to change docker root data directory and why would you want to do that (hint: to optimize space)
Favicon
Containerize a Web Application using docker compose
Favicon
O Docker Compose agora tem uma V2, veja como instalar
Favicon
Vue 2 vite dockerized steps
Favicon
An Advanced Guide (2) to Docker: Managing Multi-Container Applications 🐳
Favicon
How to run mongodb using docker-compose in ubuntu
Favicon
How to run postgres database using docker and docker-compose
Favicon
Easy Dockerization with Docker INIT

Featured ones: