docker-compose.yml
Overview
The `docker-compose.yml` file is a configuration file used by Docker Compose to define and manage multi-container Docker applications. This specific file configures a single service named `api` that runs a Node.js application within a Docker container. It defines how this service is built, started, networked, and how it interacts with other infrastructure components, such as the Traefik reverse proxy and an external Docker network.
This file simplifies the development and deployment process by encapsulating all necessary container settings, environment variables, volumes, and networking in one place, allowing developers to spin up the entire application stack with a single command.
Detailed Explanation
Services Section
The core of this file is the `services` section, which currently defines one service:
api Service
Purpose: Runs the Node.js API server inside a container.
Image:
unchained-local-node
Specifies the Docker image to use for this service. This is likely a custom-built image tailored for the local Node.js environment.env_file:
.env
Loads environment variables from a.envfile located in the same directory as thisdocker-compose.yml. This keeps sensitive or environment-specific data out of the configuration file.labels:
These labels configure Traefik, a popular reverse proxy/load balancer, to route incoming HTTP traffic appropriately:'traefik.enable=true'
Enables Traefik for this service.'traefik.http.routers.proxy-api.rule=Host(api.proxy.localhost)'
Routes requests with the host headerapi.proxy.localhostto this service.'traefik.http.services.proxy-api.loadbalancer.server.port=3000'
Indicates that Traefik should forward traffic to port 3000 inside the container.
working_dir:
/app/node/proxy/api
Sets the working directory inside the container, where commands will be executed.command:
yarn nodemon
Overrides the default command to runyarn nodemon, which starts the Node.js application with automatic reload on code changes (nodemon).volumes:
../..:/app
Binds the project root directory (two levels up from the current directory) on the host machine to/appinside the container. This allows live code editing and instant reflection inside the container.
networks:
Connects this service to theproxynetwork, enabling communication with other services on this network and Traefik.
Networks Section
Defines a Docker network used to isolate and connect containers:
proxy:
name: proxy_default
Uses an existing Docker network namedproxy_default.external: true
Indicates that this network is managed outside of this Compose file and should not be created or removed by Docker Compose. This is often used to connect the service to a shared proxy network managed by Traefik or other infrastructure.
Usage Example
To start the service defined in this file, navigate to the directory containing `docker-compose.yml` and run:
docker-compose up
This command will:
Pull or build the
unchained-local-nodeimage if necessary.Create and start the
apicontainer.Attach the container to the external
proxy_defaultnetwork.Apply Traefik routing rules so that the API is accessible at
http://api.proxy.localhost.
To stop and remove the containers, use:
docker-compose down
Important Implementation Details
Traefik Integration:
The file is tightly integrated with Traefik, which acts as a reverse proxy and load balancer. Traefik dynamically discovers containers based on Docker labels and routes traffic according to host rules.Live Code Updates:
The use of a volume mapping (../..:/app) combined withnodemonallows developers to edit code on the host machine and see changes reflected immediately inside the container without rebuilding the image.External Network:
By connecting to an external network (proxy_default), this service can seamlessly integrate with other services and shared infrastructure components (e.g., Traefik, databases, or other microservices) that are on the same network.
Interaction with Other System Components
Traefik Proxy:
Traefik listens on the external networkproxy_defaultand uses the labels defined in this file to route HTTP requests to theapiservice container. This enables clean routing rules and domain-based access to the API.Source Code Repository:
The volume mapping assumes the Docker Compose file is located within a subdirectory of the project root, sharing the source code with the container for development.Other Services:
Although this Compose file only defines theapiservice, it can be extended with additional services (e.g., databases, caches) that also connect to theproxynetwork for inter-service communication.
Visual Diagram
flowchart TD
A[Start docker-compose.yml] --> B[Define Services]
B --> C[Service: api]
C --> D[Image: unchained-local-node]
C --> E[Env: .env file]
C --> F[Labels: Traefik routing]
C --> G[Working Dir: /app/node/proxy/api]
C --> H[Command: yarn nodemon]
C --> I[Volumes: ../..:/app]
C --> J[Networks: proxy]
J --> K[External Network: proxy_default]
K --> L[Traefik Reverse Proxy]
L --> M[Routes traffic for api.proxy.localhost → Port 3000 on api container]
style C fill:#f9f,stroke:#333,stroke-width:2px
style K fill:#bbf,stroke:#333,stroke-width:2px
style L fill:#bfb,stroke:#333,stroke-width:2px
Summary
This `docker-compose.yml` file provides a concise yet powerful configuration for running a Node.js API container integrated with Traefik for HTTP routing and connected to an external Docker network for service discovery. It supports efficient development workflows with live code reloading and environment variable management, making it an essential part of the application's local and potentially production deployment setup.