Back to articles
DevOps & Systems

Secrets to Running 50 Docker Containers Smoothly on a 4GB RAM VPS

To run 50 Docker containers smoothly on a 4GB RAM VPS, system resource optimization, efficient memory and CPU management, and tools like Docker Compose, cgroups, and monitoring...

DPTCloud Engineering4 min read
Secrets to Running 50 Docker Containers Smoothly on a 4GB RAM VPS

To run 50 Docker containers smoothly on a 4GB RAM VPS, system resource optimization, efficient memory and CPU management, and tools like Docker Compose, cgroups, and monitoring systems are essential. This guide outlines practical strategies, optimal configurations, and verification commands to achieve maximum performance on limited resources, suitable for small and medium businesses deploying multiple services on VPS platforms.

System Resource Optimization

On a 4GB RAM VPS, resource allocation is critical. Start by checking available resources with:

free -h
top

Ensure the system isn’t overloaded. Disable unnecessary services like Apache or MySQL if not in use. Optimize the kernel by adjusting parameters such as vm.swappiness to reduce swap usage. Edit /etc/sysctl.conf:

vm.swappiness=10
vm.vfs_cache_pressure=50

Apply changes with sysctl -p. Use docker stats to monitor running container performance. Optimize memory by limiting memory for each container:

docker run -d --name mycontainer --memory 512m --memory-swap 512m myimage

Docker Compose Configuration and Monitoring

Use Docker Compose to manage multiple containers. Create a docker-compose.yml file with resource limits for each service:

version: '3.8'
services:
  app1:
    image: myapp:latest
    mem_limit: 512m
    cpu_shares: 512
    restart: unless-stopped
  app2:
    image: myapp:latest
    mem_limit: 512m
    cpu_shares: 512
    restart: unless-stopped

Run docker-compose up -d to start. Use Prometheus and Grafana for system-wide monitoring. Install Prometheus with:

docker run -d --name prometheus -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

Refer to the guide on configuring Prometheus for Docker at Optimizing Business Costs: Self-Hosted Apache Superset on 4GB RAM VPS.

Container Management and Automation

To manage 50 containers, automate deployment and monitoring. Use Ansible or Bash scripts to manage containers. For example, a script to restart crashed containers:

#!/bin/bash
for container in $(docker ps -q); do
  if ! docker inspect $container >/dev/null; then
    docker start $container
  fi
done

Run the script periodically with cron. Configure Docker to automatically restart on failure:

docker run -d --restart unless-stopped --name mycontainer myimage

Testing and Optimization

Test performance using the following commands:

  • docker stats: Monitor CPU, RAM, and I/O
  • docker system df: Check disk usage
  • docker container prune: Remove unused containers
  • docker image prune: Remove unused images

Run docker system prune -a to clean up everything.

Troubleshooting and Tips

If containers crash or hang, check logs with docker logs <container_id>. If RAM is full, increase swap space:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Add to /etc/fstab to persist swap after reboot. If CPU is overloaded, reduce cpu_shares for less critical containers.

Reference Resources

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478