Docker Compose - Run multiple Docker containers

What is Docker Compose?

Docker Compose is a tool for defining and running multiple docker containers.

YAML file to configure your application's services:

Docker Compose automatically creates a common docker network for docker containers in it (--net option in docker run)

You can maintain and update configuration more easily than with docker run command

Starting the application with Docker Compose

mongo.yaml
services:
  mongodb:
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    image: mongo
    ports:
      - "27017:27017"
  mongo-express:
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
    image: mongo-express
    ports:
      - "8080:8081"

version: "3"

✅ Step 1: start mongodb and mongo-express

docker-compose -f mongo.yaml up

You can access the mongo-express under localhost:8080 from your browser

✅ Step 2: in mongo-express UI - create a new database “my-db”

✅ Step 3: in mongo-express UI - create a new collection “users” in the database “my-db”

✅ Step 4: start node server

cd app
npm install
node server.js

✅ Step 5: access the nodejs application from browser

http://localhost:3000

Last updated