Docs / Containers & Docker / Building Efficient Dockerfiles for Node.js Apps

Building Efficient Dockerfiles for Node.js Apps

By Admin · Mar 29, 2026 · Updated Apr 23, 2026 · 7 views · 4 min read

Building Efficient Dockerfiles for Node.js Apps is a common requirement for VPS administrators. This guide provides practical instructions that you can follow on Ubuntu 22.04/24.04 or Debian 12, though most steps apply to other distributions as well.

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: dockerfile: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.

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"]

The output should show the service running without errors. If you see any warning messages, address them before proceeding to the next step.

  • Review log files weekly for anomalies
  • Monitor disk space usage and set up alerts
  • Keep your system packages updated regularly

Configuring Volumes and Networks

The dockerfile 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.


# Container management commands
docker compose up -d
docker compose logs -f app
docker compose exec app sh
docker system prune -af --volumes  # Caution: removes unused data

These commands should be run as root or with sudo privileges. If you're using a non-root user, prefix each command with sudo.

Configuration Options

Performance benchmarks show that properly tuned dockerfile can handle significantly more concurrent connections than the default configuration. The key improvements come from adjusting worker processes and connection pooling.

Managing Container Lifecycle

If you encounter issues during setup, check the system logs first. Most problems can be diagnosed by examining the output of journalctl or the application-specific log files in /var/log/.


# docker-compose.yml
version: '3.8'
services:
  app:
    image: dockerfile: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:

These commands should be run as root or with sudo privileges. If you're using a non-root user, prefix each command with sudo.

Common Issues and Solutions

  • Connection timeout: Verify your firewall rules allow traffic on the required ports. Use ss -tlnp to confirm the service is listening on the expected port.
  • Service won't start: Check the logs with journalctl -xe -u dockerfile. Common causes include port conflicts, missing configuration files, or insufficient permissions.
  • Slow performance: Check for disk I/O bottlenecks with iostat -x 1 and network issues with mtr. Review application logs for slow queries or requests.

Conclusion

This guide covered the essential steps for working with dockerfile on a VPS environment. For more advanced configurations, refer to the official documentation. Don't hesitate to reach out to our support team if you need help with your specific setup.

Was this article helpful?