docker-compose.yml
Overview
The [docker-compose.yml](/projects/291/68791) file defines and configures Docker services and networks for a local development environment, specifically targeting a backend API service. It leverages Docker Compose to orchestrate container deployment, networking, and runtime parameters, simplifying the process of running the API service locally within isolated and reproducible containers.
This configuration is tailored to work with Traefik as a reverse proxy for routing HTTP requests based on host rules, and it mounts the project source code into the container for live development with nodemon (a Node.js tool that automatically restarts the server on code changes).
Detailed Explanation
Top-Level Structure
services: Defines Docker containers (services) to be run.
networks: Defines Docker networks for service communication.
Service: api
The `api` service runs the backend API.
Property | Description |
|---|---|
Specifies the Docker image to use. Here, `unchained-local-node` is a custom/local Node.js image. | |
`env_file` | Points to the [.env](/projects/291/68960) file to load environment variables into the container. |
`labels` | Docker labels used by Traefik for automatic reverse proxy configuration: |
| |
| |
| |
`working_dir` | Sets the working directory inside the container to `/app/node/coinstacks/base/api`. |
`command` | Overrides the default container command to run `yarn nodemon` for hot-reloading during development. |
`volumes` | Mounts the local project directory into `/app` inside the container, enabling live code updates. |
Connects the container to the `base` Docker network for inter-container communication. |
Usage Example (CLI)
docker-compose up api
This command will start the `api` service as defined, mounting the local source code, setting environment variables, and using Traefik for routing.
Network: base
Property | Description |
|---|---|
`name` | Sets the network name to `base_default`. |
`external` | Marks this network as external, assuming it is already created outside this Compose file. |
Important Implementation Details
Traefik Integration:
Labels specify Traefik routing rules enabling requests toapi.base.localhostto be forwarded to this service on port 3000. This allows easy local development with domain-based routing.Live Development Setup:
Usingnodemoninside the container viayarn nodemonensures the Node.js API server reloads automatically on code changes. The volume mount keeps the container code in sync with the host file system.Network Isolation:
By using a dedicated external networkbase_default, the service can communicate securely with other containers attached to this network without exposing ports publicly.
Interaction with Other System Components
Codebase:
The service mounts the local project source at/appinside the container, ensuring any code changes on the host immediately reflect inside the containerized environment.Traefik Proxy:
This Compose file relies on an external Traefik instance configured to read Docker labels for routing. Traefik listens for requests toapi.base.localhostand forwards them to the API container on port 3000.External Network
base_default:
The service joins an existing Docker network (base_default), allowing it to communicate with other services or containers in the same network, facilitating microservice interactions or shared resources.
Visual Diagram
flowchart TD
subgraph Docker Compose
API[api Service]
NET[base Network (external)]
end
API -- "connects to" --> NET
API -- "mounts local source\n(../../.. to /app)" --> HostFS[(Host Filesystem)]
API -- "runs command\n'yarn nodemon'" --> NodeProcess((Node.js Server))
API -- "Traefik labels\nfor routing" --> Traefik[Traefik Proxy]
Traefik -- "routes HTTP requests\napi.base.localhost:80 -> API:3000" --> API
Summary
This [docker-compose.yml](/projects/291/68791) file defines a single backend API service running inside a Docker container, optimized for local development with live-reloading. It integrates with Traefik for hostname-based routing and joins an external Docker network for inter-service communication. The configuration promotes rapid development cycles by syncing local code changes directly into the container environment.
If you are setting up or maintaining the local development environment for the backend API, this file is the central orchestrator that ensures the API runs with correct environment variables, routing, and live reload capabilities.