This repository is for demo how to use docker compose
- create bridge network for container communication
docker network create mongo-network
- ls current network created
docker network ls
- run mongo over mongo-network
docker run -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=supersecret \
--network mongo-network \
--name mongodb \
mongo
- run mongo-express over mongo-network
docker run -d \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=supersecret \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
--network mongo-network \
--name mongo-express \
mongo-express
docker log for check mongo-express default user/password
- create docker-compose.yml
networks:
mongo-network:
driver: bridge
name: mongo-network
services:
mongodb:
image: mongo:latest
container_name: mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: supersecret
networks:
- mongo-network
ports:
- 27017:27017
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh mongodb://admin:supersecret@localhost:27017/ --quiet
interval: 5s
timeout: 10s
retries: 3
mongo-express:
image: mongo-express:latest
container_name: mongo-express
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: supersecret
ME_CONFIG_MONGODB_SERVER: mongodb
depends_on:
mongodb:
condition: service_healthy
networks:
- mongo-network
ports:
- 8081:8081
- use docker compose command to start
docker compose up -d
This app will directly load data from mongodb
mkdir app
app:
build:
context: .
dockerfile: ./app/Dockerfile
target: prod
container_name: web-app
image: web-app
networks:
- mongo-network
environment:
MONGO_DB_USERNAME: admin
MONGO_DB_PWD: supersecret
ports:
- 3000:3000
depends_on:
mongodb:
condition: service_healthy
- use environment to inject credential data
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_DB_ADMIN_USER}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_DB_ADMIN_PASSWD}
services:
myapp:
image: maapp:latest
secrets:
- my_secret
secrets:
my_secret:
file: ./my_secret.txt