Skip to content

Instantly share code, notes, and snippets.

@usrbinkat
Last active November 8, 2024 02:14
Show Gist options
  • Save usrbinkat/494adddf1e153abfcc8b8bb3e1a9f404 to your computer and use it in GitHub Desktop.
Save usrbinkat/494adddf1e153abfcc8b8bb3e1a9f404 to your computer and use it in GitHub Desktop.
Minecraft on Fedora in Docker Compose
version: "3.8"
services:
minecraft:
image: itzg/minecraft-server
container_name: minecraft_server
ports:
- "32767:25565" # Map host port 32767 to container port 25565
environment:
EULA: "TRUE"
TYPE: "VANILLA"
VERSION: "1.21.3"
LEVEL_TYPE: "DEFAULT"
GAMEMODE: "survival"
DIFFICULTY: "easy"
MAX_PLAYERS: "20"
SEED: "-8969774262643053512"
MOTD: "Eli Morgan's Family & Friends Minecraft World!"
OPS: "usrbinkat, gardengnomegal8"
WHITELIST: "usrbinkat, gardengnomegal8, microbeaster, paddyjohn"
OVERRIDE_SERVER_PROPERTIES: "TRUE"
MAX_WORLD_SIZE: "100000"
FORCE_GAMEMODE: "TRUE"
MAX_BUILD_HEIGHT: "512"
SPAWN_PROTECTION: "16"
VIEW_DISTANCE: "16"
PVP: "TRUE"
MEMORY: "8G"
ENABLE_WHITELIST: "TRUE"
ENFORCE_WHITELIST: "TRUE"
ENABLE_ROLLING_LOGS: "TRUE"
volumes:
- minecraft_data:/data
volumes:
minecraft_data:
driver: local

Minecraft Server Setup with Docker and Docker Compose

Welcome! This project provides a straightforward way to set up a customized Minecraft server using Docker and Docker Compose. By following this guide, you can create a fun and secure environment for you and your friends to enjoy Minecraft together.

Table of Contents

Introduction

This project leverages Docker to run a Minecraft server with specific configurations tailored for a private group. Using Docker Compose ensures that the server setup is reproducible, manageable, and easy to deploy.

Features

  • Minecraft Version: 1.20.4 (VANILLA)
  • Game Mode: Survival
  • Difficulty: Easy
  • Custom World Seed: your-custom-seed
  • Whitelist Enabled: Only specified players can join
  • Operators (Admins): Customizable list
  • Memory Allocation: 8G
  • Data Persistence: World data stored in a Docker volume
  • Custom MOTD: "Welcome to Our Minecraft Server!"

Prerequisites

  • Operating System: Fedora (or any Linux distribution)
  • Docker: Installed and running
  • Docker Compose: Installed

Installation

Follow the steps below to set up the Minecraft server on your Fedora system.

1. Update System Packages

Open a terminal and run:

sudo dnf update -y && sudo dnf upgrade -y

2. Install Docker and Docker Compose

Install Docker and Docker Compose:

sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-compose

3. Start Docker Service

Enable and start the Docker service:

sudo systemctl enable docker
sudo systemctl start docker

Verify Docker is running:

docker --version
docker compose version

4. Create Docker Compose File

Create a directory for the server and navigate into it:

mkdir minecraft-server
cd minecraft-server

Create a docker-compose.yaml file with the following content:

version: "3.8"

services:
  minecraft:
    image: itzg/minecraft-server
    container_name: minecraft_server
    ports:
      - "25565:25565"  # Map host port 25565 to container port 25565
    environment:
      EULA: "TRUE"
      TYPE: "VANILLA"
      VERSION: "1.20.4"
      LEVEL_TYPE: "DEFAULT"
      GAMEMODE: "survival"
      DIFFICULTY: "easy"
      MAX_PLAYERS: "20"
      SEED: "your-custom-seed"
      MOTD: "Welcome to Our Minecraft Server!"
      OPS: "admin1,admin2"
      WHITELIST: "player1,player2,player3"
      OVERRIDE_SERVER_PROPERTIES: "TRUE"
      MAX_WORLD_SIZE: "100000"
      FORCE_GAMEMODE: "TRUE"
      MAX_BUILD_HEIGHT: "512"
      SPAWN_PROTECTION: "16"
      VIEW_DISTANCE: "16"
      PVP: "TRUE"
      MEMORY: "8G"
      ENABLE_WHITELIST: "TRUE"
      ENFORCE_WHITELIST: "TRUE"
      ENABLE_ROLLING_LOGS: "TRUE"
    volumes:
      - minecraft_data:/data  # Named volume for data persistence

volumes:
  minecraft_data:
    driver: local

Alternatively, you can create the file using a single command:

cat <<EOF > docker-compose.yaml
[PASTE THE ABOVE YAML CONTENT HERE]
EOF

5. Start the Minecraft Server

Run the following command to start the server in detached mode:

docker compose up -d

Verify that the server is running:

docker ps

You should see a container named minecraft_server in the list.

Usage

Viewing Server Logs

To monitor the server logs in real-time:

docker logs minecraft_server -f

Press Ctrl+C to exit the log view.

Restarting the Server

If you make changes to the docker-compose.yaml file or need to restart the server:

docker restart minecraft_server

Stopping the Server

To stop the server:

docker compose down

How to Connect to the Server

For Players

  1. Ensure You're Whitelisted

    Make sure your Minecraft username is included in the whitelist. The current whitelist includes:

    • player1
    • player2
    • player3

    If your username is not listed, contact one of the server operators to be added.

  2. Launch Minecraft

    Open your Minecraft client. Ensure you're using version 1.20.4 to match the server.

  3. Add the Server

    • Click on Multiplayer.
    • Click on Add Server.
    • Enter the following details:
      • Server Name: Custom Minecraft Server
      • Server Address: <Server IP Address>:25565
        • Replace <Server IP Address> with the public IP address or hostname of the server.
    • Click Done.
  4. Connect to the Server

    • Select the server from your list.
    • Click Join Server.

Notes on Server IP Address

  • Local Network: If you're on the same network as the server, use the server's local IP address.
  • Remote Connection: If connecting over the internet, ensure that the server's firewall allows incoming connections on port 25565, and that port forwarding is set up correctly on the router.

Server Administration

For Operators (Admins)

Operators have elevated permissions and can perform administrative tasks.

Managing the Server Container

  • Start the Server:

    docker compose up -d
  • Stop the Server:

    docker compose down
  • Restart the Server:

    docker restart minecraft_server

Accessing Server Console

While you cannot interact with the Minecraft server console directly via Docker, you can execute commands within the container:

docker exec -it minecraft_server rcon-cli

Note: You need to enable rcon in the server settings to use rcon-cli. Alternatively, you can manage the server using in-game commands if you have operator privileges.

Updating Server Configuration

  1. Edit docker-compose.yaml

    Modify any necessary environment variables under the environment section.

  2. Apply Changes

    Restart the server to apply new configurations:

    docker compose down
    docker compose up -d

Managing Whitelist and Operators In-Game

As an operator, you can manage the whitelist and other players directly from within the game.

  • Add a Player to Whitelist:

    /whitelist add <username>
    
  • Remove a Player from Whitelist:

    /whitelist remove <username>
    
  • Add an Operator:

    /op <username>
    
  • Remove an Operator:

    /deop <username>
    

Backing Up World Data

It's important to regularly back up your world data.

  1. Create a Backup

    docker run --rm -v minecraft_data:/data -v $(pwd):/backup ubuntu tar cvf /backup/minecraft_backup.tar /data

    This command will create a minecraft_backup.tar file in your current directory.

  2. Restore from Backup

    To restore from a backup, stop the server, remove the existing volume, create a new volume, and extract the backup into it.

    docker compose down
    docker volume rm minecraft-server_minecraft_data
    docker volume create minecraft-server_minecraft_data
    docker run --rm -v minecraft-server_minecraft_data:/data -v $(pwd):/backup ubuntu tar xvf /backup/minecraft_backup.tar -C /
    docker compose up -d

Data Persistence

The server uses a Docker volume named minecraft_data to store world data and configurations. This ensures that your game progress is preserved across server restarts and Docker image updates.

Troubleshooting

Common Issues

  • Server Fails to Start

    • Check the logs for error messages:

      docker logs minecraft_server
    • Ensure that no other application is using port 25565.

  • Cannot Connect to the Server

    • Verify the server is running and listening on the correct port.
    • Ensure your client is running Minecraft version 1.20.4.
    • Check firewall and router settings to ensure the server is accessible.
  • Whitelist Issues

    • Make sure your username is correctly spelled in the whitelist.
    • Contact an operator to verify your whitelist status.

Cleaning Up Docker Resources

If you need to remove unused Docker resources:

docker system prune --all --force

Warning: This command will remove all unused containers, networks, images, and optionally, volumes. Use with caution.

Acknowledgments

Disclaimer

This server setup is intended for private use among friends or small communities. Please ensure you have the proper permissions to host and manage a Minecraft server and adhere to Minecraft's End User License Agreement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment