docker-compose.yml
Overview
The `docker-compose.yml` file defines and configures containerized services for the application using Docker Compose. This particular file is focused on setting up the **api** service for the BNB Smart Chain module within the project. It specifies how the API service container is built, its runtime environment, networking, volumes, and integration with Traefik as a reverse proxy/load balancer.
Docker Compose allows developers to define multi-container Docker applications in a declarative YAML format, simplifying deployment and orchestration.
Detailed Explanation
Services Section
api Service
Purpose: Runs the BNB Smart Chain API service inside a Docker container.
Image:
unchained-local-nodeThe container image presumably contains the Node.js environment and the application code pre-packaged.
env_file: .env
Environment variables are loaded from a .env file located in the same directory where
docker-compose.ymlresides.
labels:
Used to integrate with Traefik, a dynamic reverse proxy:
'traefik.enable=true'enables Traefik routing for this container.'traefik.http.routers.bnbsmartchain-api.rule=Host(api.bnbsmartchain.localhost)'defines routing based on the hostname.'traefik.http.services.bnbsmartchain-api.loadbalancer.server.port=3000' tells Traefik to forward requests to port 3000 inside the container.
working_dir:
/app/node/coinstacks/bnbsmartchain/apiSets the working directory inside the container where commands will run.
command:
yarn nodemonStarts the API using
nodemonvia Yarn to enable automatic restart on code changes.
volumes:
Mounts the host directory three levels up (
../../..) into/appinside the container, allowing live code changes and access to the project files.
networks:
Connects the container to the
bnbsmartchainnetwork (defined below).
Networks Section
bnbsmartchain
Defines a Docker network used by the container.
name: bnbsmartchain_default specifies the actual network name.
external: true indicates this network is created outside of this Compose file (likely shared among multiple services or projects).
Usage Example
To start the API service defined in this file, run:
docker-compose up -d api
This command will:
Pull or build the
unchained-local-nodeimage (if not available locally).Create the container with the specified environment variables, volumes, and network.
Register the service with Traefik for routing.
Start the API service, accessible via
http://api.bnbsmartchain.localhost.
Implementation Details and Algorithms
Traefik Integration:
The file leverages Traefik labels to dynamically configure routing. Traefik watches Docker labels and automatically configures HTTP routers and services without manual config files. This ensures that requests toapi.bnbsmartchain.localhostare forwarded to port 3000 of the API container.Development Workflow Support:
Usingnodemonas the command allows developers to benefit from auto-reloading during development, improving the developer experience.Volume Mounting:
Mounting the host project directory into the container enables live code editing without rebuilding the image, facilitating rapid iteration.External Network Usage:
By using an external network, the API container can seamlessly communicate with other containers or services attached to thebnbsmartchain_defaultnetwork, such as databases or other microservices.
Interaction with Other Parts of the System
Project Source Code:
The mounted volume points to the source code three directories up, which likely includes the entire project workspace. This means code changes in the host environment are reflected live inside the container.Traefik Reverse Proxy:
Traefik acts as an entry point for HTTP requests, routing traffic to the API container based on hostname rules.Other Services (not shown):
Although not defined here, the external network suggests other services (e.g., blockchain nodes, databases, caches) also connect tobnbsmartchain_defaultand can interact with the API container.
Visual Diagram
Below is a flowchart illustrating the relationships and workflow in this `docker-compose.yml` file, focusing on the API service, its runtime environment, and networking.
flowchart TD
A[Host Machine]
B[Docker Container: api]
C[Volume Mount (/app)]
D[Traefik Reverse Proxy]
E[External Network: bnbsmartchain_default]
A -->|Mounts source code| C
C --> B
B -->|Exposes port 3000| D
D -->|Routes requests for api.bnbsmartchain.localhost| B
B --- E
Summary
This `docker-compose.yml` file is a concise but powerful configuration for running the BNB Smart Chain API service containerized. It integrates environment management, live code development support, networking for multi-service communication, and Traefik-based routing—all essential for a scalable and developer-friendly microservice architecture.