docker-compose.yml
Overview
The `docker-compose.yml` file defines and configures Docker services, networks, and volumes for the application environment. Specifically, this file sets up a single service called `api` that runs a local Node.js application containerized with the image `unchained-local-node`. It configures environment variables, Traefik labels for reverse proxy routing, working directory, command execution, volume mounts, and network settings. The file also declares an externally managed Docker network named `bitcoin_default` to which the service connects.
This configuration enables seamless local development and testing by mapping the application source code into the container, automating restarts with `nodemon`, and exposing the API through Traefik with a custom hostname.
Detailed Explanation
Services Section
api Service
Purpose:
Runs the Bitcoin API Node.js application inside a Docker container.Configuration:
Key
Description
Docker image used: `unchained-local-node` (local build or prebuilt image containing the app environment)
`env_file`
Loads environment variables from `.env` file for container configuration
`labels`
Labels for Traefik reverse proxy to enable routing and load balancing
`working_dir`
Sets the working directory inside the container to [/app/node/coinstacks/bitcoin/api](/projects/291/69281)
`command`
Command executed when the container starts: `yarn nodemon` (runs Node.js app with automatic reload)
`volumes`
Mounts the project source code from host to container (`../../..:/app`) for live code updates
Connects the container to the external Docker network `bitcoin`
Traefik Labels Explained:
'traefik.enable=true': Enables Traefik proxy for this container.'traefik.http.routers.bitcoin-api.rule=Host(api.bitcoin.localhost)': Routes requests with hostapi.bitcoin.localhostto this service.'traefik.http.services.bitcoin-api.loadbalancer.server.port=3000': Specifies the internal container port (3000) Traefik will forward traffic to.
Usage Example:
To start the service with Docker Compose:
docker-compose up -d apiThen access the API at:
http://api.bitcoin.localhost/assuming your
/etc/hostsor DNS is configured to resolveapi.bitcoin.localhostto your Docker host.
Networks Section
bitcoin Network
Purpose:
Defines an external Docker network namedbitcoin_defaultthat theapiservice connects to. This network is assumed to be created outside this docker-compose file (e.g., by another compose file or manually).Configuration:
Key
Description
`name`
The actual Docker network name: `bitcoin_default`
`external`
Set to `true`, indicating the network is not created by this compose file
Usage Note:
The external network must exist prior to runningdocker-compose up. You can create it with:docker network create bitcoin_default
Important Implementation Details
Volume Mounting:
The host directory../../..(relative to location of this file) is mounted into/appinside the container. This allows live code changes on the host to immediately reflect inside the running container, supporting rapid development and debugging.Working Directory and Command:
The container’s working directory is set to the API’s source folder.yarn nodemonruns the Node.js app with hot reload on code changes, enhancing developer experience.Traefik Integration:
Traefik is used as a reverse proxy and load balancer. The labels configure Traefik to route traffic destined forapi.bitcoin.localhostto port 3000 inside the container. This setup abstracts service ports and supports scalable service discovery.External Network Usage:
By connecting the service to an external network, this compose file integrates with a broader Docker network environment, facilitating multi-service communication across different compose projects or containers.
Interaction with Other Parts of the System
API Service:
This file specifically manages the Bitcoin API service container lifecycle and networking within the Docker environment.Traefik Proxy:
Traefik acts as the front-facing reverse proxy that routes HTTP requests to this API service based on hostname rules defined here.Host System:
The.envfile on the host provides environment variables injected into the container, allowing configuration without hardcoding secrets or parameters.Codebase:
The mounted volume maps the host source code into the container, enabling synchronization between local development and container runtime.External Network:
Thebitcoin_defaultnetwork likely interconnects other related services or containers in the broader application ecosystem, enabling service discovery and communication.
Visual Diagram
Below is a **flowchart** illustrating the main components and their interactions configured in this `docker-compose.yml` file:
flowchart TD
Host[Host Machine]
EnvFile[.env Environment File]
SourceCode[../../.. Source Code]
DockerCompose[docker-compose.yml]
DockerContainer[API Container ("unchained-local-node")]
Traefik[Traefik Reverse Proxy]
BitcoinNetwork["bitcoin_default" Docker Network]
Host --> EnvFile
Host --> SourceCode
DockerCompose --> DockerContainer
DockerContainer --> BitcoinNetwork
DockerContainer --> Traefik
Traefik -->|Routes requests for api.bitcoin.localhost| DockerContainer
EnvFile --> DockerContainer
SourceCode -->|Volume Mount| DockerContainer
Summary
This `docker-compose.yml` file is a focused configuration for running the Bitcoin API service containerized with Node.js. It leverages environment files, live code mounting, Traefik reverse proxy routing, and Docker networking to establish a flexible and developer-friendly local service environment within a modular multi-container system. Its integration points position it well as a fundamental building block in a larger distributed architecture.