docker-compose.yml
Overview
The [docker-compose.yml](/projects/291/68791) file defines and configures a multi-container Docker application environment specifically for the `api` service of the Bitcoincash project. This file leverages Docker Compose to simplify the orchestration of service containers, network configuration, and volume mounting, facilitating local development and testing of the Bitcoincash API.
In this context, the file sets up a single service named `api` that runs a Node.js-based backend using a pre-built Docker image. It integrates with Traefik as a reverse proxy for routing HTTP requests and enables live code reloading with `nodemon`. The configuration also connects the service to an external Docker network to allow communication with other related services or infrastructure components.
Detailed Explanation
Services Section
api Service
Purpose:
Hosts the Bitcoincash API backend service inside a Docker container.Configuration Details:
image: unchained-local-node
Uses a custom Docker image namedunchained-local-nodewhich presumably contains the Node.js runtime and the application code environment.env_file: .env
Loads environment variables from a .env file located alongside the docker-compose.yml, allowing configuration parameters (e.g., database URLs, API keys) to be externalized.labels:
These labels configure Traefik, the reverse proxy/load balancer:'traefik.enable=true'
Enables Traefik proxying for this container.'traefik.http.routers.bitcoincash-api.rule=Host(api.bitcoincash.localhost)'
Routes HTTP requests with hostnameapi.bitcoincash.localhostto this service.'traefik.http.services.bitcoincash-api.loadbalancer.server.port=3000'
Specifies that Traefik should forward traffic to container port 3000, where the Node.js server listens.
working_dir: /app/node/coinstacks/bitcoincash/api
Sets the working directory inside the container, pointing to the Bitcoincash API source folder.command: yarn nodemon
Overrides the default container command to runyarn nodemon, which starts the API with automatic restart on code changes, supporting live development.volumes:../../..:/app
Mounts the host directory three levels up into/appinside the container, enabling live code syncing and access to the full project source.
bitcoincash
Connects this container to the external Docker network namedbitcoincash_defaultfor inter-service communication.
Networks Section
bitcoincashNetwork:
Defines an external Docker network namedbitcoincash_default. This network is expected to be created outside this Compose file (e.g., viadocker network create bitcoincash_default) and enables the API container to communicate with other containers on the same network, such as database or caching services.
Usage Example
To start the API service with this configuration, the developer would run:
docker-compose up api
This command will:
Pull or build the
unchained-local-nodeimage (if not present).Initialize the container with the working directory and mounted volumes.
Start the Node.js API with
nodemonfor live reload.Connect the container to the
bitcoincash_defaultnetwork.Register the service with Traefik for proper HTTP routing.
Important Implementation Details
Live Reloading:
The use ofnodemonthrough thecommandfield facilitates real-time code changes without restarting containers manually, boosting developer productivity.Traefik Integration:
Labels integrate tightly with Traefik's dynamic configuration, enabling hostname-based routing and load balancing without manual proxy setup.Volume Mounting:
Mounting a relative path (../../..) to/appallows the container to run the code from the host machine, essential for local development but not recommended for production due to performance and security considerations.External Network Usage:
The external network allows modular infrastructure scaling by enabling independent container lifecycle management and communication across multiple Docker Compose projects or containers.
Interaction with Other System Components
Traefik Reverse Proxy:
This file expects Traefik to be running as a gateway, handling incoming HTTP requests and forwarding them to the API container based on hostname rules.Project Source Code:
The volume mount points to the main project directory, implying that source code management and updates are handled outside the container.Other Services on
bitcoincash_default:
By joining the external network, this API can communicate with other services such as databases, caches, or other API components, which are assumed to be configured elsewhere.
Visual Diagram
flowchart TD
A[Host Machine] -->|Mount ../../.. to /app| B[API Container]
B -->|Runs| C[yarn nodemon]
B -->|Listens on port 3000| D[API Server]
E[Traefik Proxy] -->|Routes Host api.bitcoincash.localhost| D
B --- F[bitcoincash_default Network]
F --> G[Other Services (DB, Cache, etc.)]
style B fill:#f9f,stroke:#333,stroke-width:2px
style E fill:#bbf,stroke:#333,stroke-width:2px
style F fill:#dfd,stroke:#333,stroke-width:2px
**Diagram Explanation:**
The host machine mounts the project code into the API container.
The container runs the API server with
nodemon.Traefik proxies incoming HTTP requests with the specified hostname to the container's port 3000.
The container connects to an external Docker network allowing it to communicate with other backend services.
Summary
This [docker-compose.yml](/projects/291/68791) file is a focused development configuration for the Bitcoincash API service, enabling live reloading, Traefik-based routing, and networked communication. It is primarily targeted at local development environments, emphasizing ease of use and integration with the broader modular project architecture.