docker-compose.yml
Overview
This `docker-compose.yml` file is used to define and configure a Docker multi-container application environment, specifically targeting the deployment and execution of an API service named `api` within a larger modular software project.
The primary purpose of this file is to:
Specify the container image to run the API service.
Configure environment variables and network settings for the service.
Define how the service integrates with a Traefik reverse proxy for routing HTTP requests.
Mount necessary source code directories for live development.
Connect the service to an external Docker network named
gnosis_default.
This file serves as a lightweight orchestration layer that enables developers and deployment pipelines to easily start and manage the API container in a consistent environment.
Detailed Explanation
Services Section
api service
Purpose: Runs the backend API server container for the Gnosis module.
Image:
unchained-local-nodeThis is a pre-built Docker image, presumably containing a Node.js runtime and the necessary application code or dependencies.
Env_file:
.envLoads environment variables from a local
.envfile to configure the container runtime environment.
Labels:
These labels are used by the Traefik reverse proxy to enable routing and service discovery.
traefik.enable=true
Enables Traefik proxy for this container.traefik.http.routers.gnosis-api.rule=Host(api.gnosis.localhost)
Defines the routing rule to forward requests with Host headerapi.gnosis.localhostto this service.traefik.http.services.gnosis-api.loadbalancer.server.port=3000
Indicates that the service listens on port 3000 inside the container.
working_dir: /app/node/coinstacks/gnosis/api
Sets the working directory inside the container, likely where the API service source code resides.command:
yarn nodemon
Overrides the default container command to runnodemonvia yarn, enabling automatic restarts on code changes for development.volumes:
- ../../..:/app
Mounts the host directory three levels above the compose file into /app in the container, enabling live synchronization of code and resources.networks:
Connects the service to thegnosisnetwork defined in the networks section.
Networks Section
gnosis
An externally managed Docker network named
gnosis_defaultis referenced here.external: true indicates Docker Compose will use an existing network rather than create a new one.
This allows multiple services or projects to share the network for communication and service discovery.
Usage Example
To start the API service using this configuration, run the following command in the directory containing this `docker-compose.yml`:
docker-compose up
This will launch the containerized API service and attach it to the `gnosis_default` network. Traefik will automatically route requests to `http://api.gnosis.localhost` to this container on port 3000.
For development, the mounted volume and `nodemon` command enable live code reloads.
Important Implementation Details
Traefik Integration:
The service uses Traefik labels to declaratively configure routing rules. Traefik listens on the Docker network and directs incoming HTTP requests to the appropriate container based on hostname matching.Volume Mounting for Development:
The mounted volume ensures that code changes in the host file system are reflected immediately inside the container, supporting rapid development cycles.External Network Usage:
By connecting to an externally defined network, this service can communicate with other containers or services that share thegnosis_defaultnetwork, enabling microservice interactions or shared infrastructure components.Command Override with
nodemon:
Running the container withyarn nodemoninstead of a standard start command allows automatic server restarts on source code changes, which is particularly useful during development.
Interaction with Other Parts of the System
With Traefik (Reverse Proxy):
The labels in theapiservice allow Traefik to automatically discover the service and route HTTP requests based on hostname rules. This setup abstracts container ports and IPs from clients, enabling seamless service discovery and load balancing.With Source Code and Development Environment:
The volume mount links the container with the local project source, allowing developers to work on the codebase while running it inside the container environment.With Other Services on the
gnosis_defaultNetwork:
This service can communicate with other containers attached to the same external network, facilitating microservice communication or access to shared infrastructure components like databases or caches.
Visual Diagram
The following flowchart illustrates the relationships and workflows within this `docker-compose.yml` configuration:
flowchart TD
subgraph Docker Compose Services
api[API Service Container<br/>(image: unchained-local-node)]
end
subgraph Docker Network
gnosis[External Network:<br/>gnosis_default]
end
subgraph Host System
env[.env File]
src[Host Source Code:<br/>../../..]
traefik[Traefik Reverse Proxy]
client[Client Browser or App]
end
api -->|Connected to| gnosis
traefik -->|Listens on gnosis network| gnosis
traefik -->|Routes requests based on hostname| api
client -->|HTTP requests to api.gnosis.localhost| traefik
api -->|Reads env vars| env
api -->|Mounted volume| src
Summary
This `docker-compose.yml` defines a single API service container configured for development and integrated with Traefik for reverse proxy routing. It mounts local source code for live reloads, connects to an external Docker network for inter-service communication, and manages environment variables through `.env`. This setup supports modular, scalable development and deployment workflows by leveraging Docker containerization and Traefik’s dynamic routing capabilities.