I was having the same issue (newbie) with unnamed / tangling Docker Images and Volumes. After much research and experimentation (and the first answer posted here) I now have a DOCKER-COMPOSE.YAML the can be used to build a generic 3-Tier MERN Stack App that is Dockerized into three Containers. And all the Docker objects are named explicitly by the YAML file.
Containers:
<appname>-frontend
<appname>-backend
<appname>-database
Images:
<appname>-frontend
<appname>-backend
<appname>-database
Volumes:
<appname>-frontend
<appname>-backend
<appname>-database (dg config)
<appname>-mongodb (external persistent db)
My 3-TIER MERN App DOCKER-COMPOSE.YAML:
#
version: '3'
#
# V O L U M E S -- NOTE: can't use ${APP_NAME} here, and internal are auto-prefixed with <appname>_<name-specified>
# -------------
#
volumes:
frontend-volume: # our Frontend/Client execution volume
name: "badbank-frontend"
external: false # temporary, build specific
#
backend-volume: # our Backend/Server execution volume
name: "badbank-backend"
external: false # temporary, build specific
#
database-volume: # our Database configuration volume
name: "badbank-database"
external: false # temporary, build specific
#
mongodb-volume: # our Database/MongoDB storage volume
name: "badbank-mongodb"
external: true # keep after all Containers are destroyed
#
services:
#
# F R O N T E N D
# ---------------
frontend:
# frontend, client, app ui
container_name: ${APP_NAME}-frontend
# use the shared .env file for all 3 Tiers
env_file:
- .\.env # shared environment vars for all 3-Tiers
- .\.env.${NODE_ENV:-development} # "development" to override the "production" vars in the 'Dockerfile'
build:
context: frontend # build from \frontend
args:
APP_NAME: ${APP_NAME}
NODE_ENV: ${NODE_ENV:-development}
ports:
- '${APP_FRONTEND_PORT:-3000}:${APP_FRONTEND_PORT:-3000}'
volumes:
- frontend-volume:/exe/frontend # temporary volume
restart: always
networks:
- react-express # frontend to backend connection
depends_on:
- backend
#
# B A C K E N D
# -------------
backend:
# backend, server, app internals
container_name: ${APP_NAME}-backend
# use the shared .env file for all 3 Tiers
env_file:
- .\.env # shared environment vars for all 3-Tiers
- .\.env.${NODE_ENV:-development} # "development" to override the "production" vars in the 'Dockerfile'
build:
context: backend # build from \backend
args:
APP_NAME: ${APP_NAME}
NODE_ENV: ${NODE_ENV:-development}
ports:
- '${APP_BACKEND_PORT:-8080}:${APP_BACKEND_PORT:-8080}'
volumes:
- backend-volume:/exe/frontend # temporary volume
restart: always
networks:
- react-express # frontend to backend connection
- express-mongo # backend to database connection
depends_on:
- database
#
# D A T A B A S E
# ---------------
database:
# database, db, data store, app persistent data
container_name: ${APP_NAME}-database
# use the shared .env file for all 3 Tiers
env_file:
- .\.env # shared environment vars for all 3-Tiers
- .\.env.${NODE_ENV:-development} # "development" to override the "production" vars in the 'Dockerfile'
build:
context: database # build from \database
args:
APP_NAME: ${APP_NAME}
NODE_ENV: ${NODE_ENV:-development}
ports:
- '${APP_DATABASE_PORT:-27017}:${APP_DATABASE_PORT:-27017}'
volumes:
- database-volume:/data/configdb # temporary volume -- NOTE: /data/configdb/ is the path required by MongoDB
- mongodb-volume:/data/db # persist our database in this volume -- NOTE: /data/db/ is the path required by MongoDB
restart: always
networks:
- express-mongo # backend to database connection
#
networks:
react-express:
express-mongo:
#