Here’s the basic Docker Compose file structure.
version: "3.7"
services:
app:
container_name: knote-app
image: learnitguide/knotejs:1.0
environment:
- MONGO_URL=mongodb://mongo_db_host:27017/dev
ports:
- "80:3000"
depends_on:
- mongo
links:
- mongo:mongo_db_host
mongo:
container_name: knote-mongo
image: mongo
ports:
- "27017:27017"
Explanation:
version: "3.7": This specifies the version of Docker Compose being used.
services: Defines the services (containers) in the application.
app: Your application container.
container_name: The name of the container.
image: The Docker image used to build the container.
environment: Environment variables, like the MongoDB URL.
ports: Maps port 3000 of the container to port 80 on the host machine.
depends_on: Specifies that the app service depends on the mongo service.
links: Allows the app to link to the mongo container via the alias mongo_db_host.
mongo: MongoDB service.
container_name: Name of the MongoDB container.
image: The MongoDB Docker image.
ports: Maps the default MongoDB port 27017 to the host machine.