Lazytainer - start & stop minecraft servers

tags

Introduction

In the previous article Sablier - start & stop containers we used Sablier to manage idle docker containers. While Sablier offers container management capabilities for HTTP services, Minecraft’s TCP-based communication makes it challenging to effectively manage Minecraft servers. Lazytainer provides an elegant solution for automatically starting and stopping Minecraft server containers based on player activity.

How Lazytainer Works

Lazytainer monitors network traffic and manages Docker containers based on activity patterns. When players attempt to connect to your Minecraft servers, Lazytainer automatically starts the required containers. After a period of inactivity, it gracefully stops them to conserve resources.

Implementation Details

The setup consists of three main components: the Lazytainer service, a Velocity proxy (mc-proxy), and the Minecraft server containers. The Velocity and Minecraft servers setup was covered in Setting Up a Minecraft Server Network with Velocity; A Comprehensive Guide. Here’s the detailed configuration:

services:
  lazytainer:
    image: ghcr.io/vmorganp/lazytainer:master
    environment:
      VERBOSE: false
    ports:
      - 25565:25565
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    labels:
      - lazytainer.group.minecraft.sleepMethod=stop
      - lazytainer.group.minecraft.ports=25565
      - lazytainer.group.minecraft.minPacketThreshold=2
      - lazytainer.group.minecraft.inactiveTimeout=1800 # 30min
    restart: unless-stopped
    network_mode: bridge
  
  mc-proxy:
    image: itzg/mc-proxy
    environment:
      TYPE: VELOCITY
      DEBUG: false
      ENABLE_RCON: true
    volumes:
      # Mount your configuration files here
    network_mode: service:lazytainer
    restart: unless-stopped
  
  mc-lobby:
    image: itzg/minecraft-server
    environment:
      SERVER_PORT: 25566
      RCON_PORT: 25576
      # Additional server configuration here
    volumes:
      # Mount your server data here
    labels:
      - lazytainer.group=minecraft
    network_mode: service:lazytainer
    restart: unless-stopped

Network Architecture

The setup employs a specific networking strategy to ensure proper communication:

  1. Lazytainer operates in bridge network mode, serving as the central communication hub
  2. All Minecraft servers connect to Lazytainer’s network using network_mode: service:lazytainer
  3. Each server requires a unique port since they share the same network namespace

Velocity Proxy Configuration

To properly route traffic to the different servers, configure your velocity.toml file as follows:

[servers]
lobby = "localhost:25566"
smp = "localhost:25567"
skyblock = "localhost:25568"

User Experience

This configuration creates a seamless experience for players:

  1. The mc-proxy remains active at all times, ensuring the server always appears online
  2. When a player attempts to connect while servers are stopped, they’ll receive a message indicating that the lobby server is starting
  3. Lazytainer automatically starts the required servers, typically taking about a minute
  4. After 30 minutes of inactivity (configurable via inactiveTimeout), servers automatically stop to conserve resources

Key Benefits

  • Automatic resource management based on player activity
  • Seamless server startup when players connect
  • Efficient resource utilization during inactive periods
  • Maintained server visibility even when instances are stopped

Would you like me to expand on any particular aspect of this setup?