Managing docker-compose effectively is a crucial skill for any system administrator. This tutorial provides step-by-step instructions for multi-service configuration, along with best practices for production environments.
Prerequisites
- Docker Engine 24.0+ installed
- Docker Compose v2 installed
- Basic familiarity with the Linux command line
- A registered domain name (for public-facing services)
Creating the Docker Compose File
After applying these changes, monitor the server's resource usage for at least 24 hours to ensure stability. Tools like htop, iostat, and vmstat can provide real-time insights into system performance.
# docker-compose.yml
version: '3.8'
services:
app:
image: docker-compose:latest
restart: unless-stopped
ports:
- "8080:8080"
volumes:
- app_data:/data
environment:
- NODE_ENV=production
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
volumes:
app_data:
Make sure to restart the service after applying these changes. Some settings require a full restart rather than a reload to take effect.
Performance Considerations
It's recommended to test this configuration in a staging environment before deploying to production. This helps identify potential compatibility issues and allows you to benchmark performance differences.
Building the Container Image
After applying these changes, monitor the server's resource usage for at least 24 hours to ensure stability. Tools like htop, iostat, and vmstat can provide real-time insights into system performance.
# Multi-stage Dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]
Note that file paths may vary depending on your Linux distribution. The examples here are for Debian/Ubuntu; adjust paths accordingly for RHEL/CentOS-based systems.
Security Implications
The docker-compose configuration requires careful attention to resource limits and security settings. On a VPS with limited resources, it's important to tune these parameters according to your available RAM and CPU cores.
Summary
You've successfully configured docker-compose on your VPS. Remember to monitor performance, keep your software updated, and maintain regular backups. If you run into issues, consult the official documentation or open a support ticket for assistance.