Setup a Minecraft server on AWS EC2 with Docker

Build a Minecraft Server on AWS EC2 Using Docker

Introduction

Minecraft🔗 is one of the world’s most famous sandbox games. In the past, when playing this game with friends, we connect through a personal computer’s local area network (LAN), using Hamachi🔗 to simulate the LAN connection, but there were obviously various limitations.

This article explores the aim of quickly setting up a private MC server using different technologies (AWS EC2, Docker):

  1. Stable independent dedicated servers
  2. Broader and faster scalability
  3. More flexible and faster server building

Create EC2 Virtual Server

Amazon Elastic Compute Cloud🔗 (EC2) allows us to set up machines with specific configurations in the cloud, which can be adjusted according to demand. The following process is similar to the official blog example🔗:

  1. Log in to AWS Console🔗
  2. Select the region where the server will be deployed (Tokyo ap-northeast-1 Asian server) in the upper right corner
  3. Search for the EC2 service and enter Dashboard, then click Launch Instance
  4. Give the new EC2 instance a name (minecraft) and select the operating system image (Ubuntu Server 64-bit x86)
  5. Choose the EC2 instance type; upgrading the hardware specifications later is not an issue🔗 (t3.small)
  6. Create a new key pair🔗 for convenient future SSH access to the server (default, name is arbitrary)
  7. In Network settings, turn off Auto-assign public IP (optional; to ensure the server IP is stable rather than temporarily assigned, you can set Elastic IPs later)
  8. In Network settings, add Security group rule 2 (TCP, 25565, 0.0.0.0/0) to allow the Minecraft port 25565 to pass through
  9. In Configure store, select the appropriate capacity (20 GB)

Final Specifications Example
Final Specifications Example

Different Instance Cost Comparison Table
Different Instance Cost Comparison Table (according to AWS Cost optimize your Minecraft Java EC2 Server article)

Set Up Elastic IPs for Fixed External IP

If you previously turned off Auto-assign public IP, you will need to use Elastic IPs to achieve a fixed external IP.

  1. Allocate Elastic IP address (default Allocate)
  2. Associate Elastic IP address (select the instance created earlier)

Once completed, the Public IPv4 address should be present in the instance details.

Set Up Elastic IP

Access EC2 Instance

You can connect using various methods; here we’ll directly use EC2 Instance Connect in the browser. If connecting via SSH, the key pair set earlier will come into play.

  1. Select the instance
  2. Connect
  3. EC2 Instance Connect

Connect to Instance

Configure EC2 Instance

  1. Update packages sudo apt update
  2. Install Docker - Install using the apt repository🔗
  3. Manage Docker as a non-root user🔗

Run Minecraft Docker Image

Terminal window
# Create and enter mc-docker directory
mkdir mc-docker
cd mc-docker
# Create docker-compose.yml
nano docker-compose.yml

Set up the corresponding Docker image according to itzg/docker-minecraft-server🔗.

docker-compose.yml
services:
minecraft:
image: itzg/minecraft-server
container_name: minecraft
ports:
- "25565:25565"
environment:
EULA: "TRUE"
VERSION: "1.21.5"
volumes:
- mc-data:/data
restart: unless-stopped
volumes:
mc-data:

Run:

Terminal window
docker compose up -d
docker compose logs -f

Summary

If you’re not familiar with Minecraft servers, you can first set up a Docker container locally to try it out. Once confirmed, it can be easily deployed on EC2.

Further Reading