Docker & Containerization
Docker & Containerization
To ensure a consistent environment across development, staging, and production, the testing-api is fully containerized using Docker. This approach encapsulates the application, its dependencies, and its runtime configuration into a single immutable image.
Prerequisites
Before proceeding, ensure you have the following installed on your system:
- Docker Desktop or Docker Engine (v20.10+)
- Docker Compose (v2.0+)
Building the Image
To build the local Docker image for the testing-api, execute the following command from the root directory:
docker build -t testing-api:latest .
This process will:
- Pull the required base image.
- Install all necessary system and application dependencies.
- Compile/transpile source code (if applicable).
- Prepare the application for execution.
Running the Container
Once the image is built, you can start the API using the docker run command. Ensure you map the internal port (default is usually 8080 or 3000 depending on your local config) to your host machine.
docker run -p 8080:8080 --env-file .env testing-api:latest
The API will now be accessible at http://localhost:8080.
Orchestration with Docker Compose
For a more streamlined setup—especially when managing connected services like databases or caches—it is recommended to use Docker Compose.
1. Configuration
A standard docker-compose.yml is provided in the repository to orchestrate the API lifecycle:
version: '3.8'
services:
api:
build: .
ports:
- "8080:8080"
env_file:
- .env
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
restart: always
2. Deployment Commands
To start the entire stack in the background:
docker-compose up -d
To view real-time logs:
docker-compose logs -f api
To stop and remove containers:
docker-compose down
Environment Variables
The containerized API relies on environment variables for configuration. When running via Docker, these should be defined in a .env file or passed directly to the container. Key variables include:
| Variable | Description | Default |
| :--- | :--- | :--- |
| NODE_ENV | Deployment environment (development/production) | development |
| PORT | The internal port the API listens on | 8080 |
| API_KEY | Secret key for Supervised AI platform integration | required |
Production Best Practices
When deploying the testing-api container to production:
- Use Specific Tags: Avoid using
:latest. Use semantic versioning tags (e.g.,:1.0.2). - Resource Limits: Define CPU and Memory limits in your orchestration tool to prevent resource exhaustion.
- Read-Only Filesystem: For enhanced security, run the container with a read-only root filesystem where possible.