Setting Up a Minecraft Server in a Docker Container on Linux

Are you aware that you can host a Minecraft server within a Docker container on Linux? While it may seem daunting at first, deploying a Minecraft server in a Docker environment is straightforward. In this guide, we’ll take you through the steps to get started on your system.

Installing Docker on Your Linux Server

To run a Minecraft server using Docker on Linux, the first step is to install Docker itself. Begin by opening a terminal window on your Linux machine. Then, follow the installation steps tailored for your specific Linux distribution.

Installing Docker on Ubuntu/Debian

For users of Ubuntu or Debian, you need to install essential packages to set up the Docker repository. First, run the following command:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Next, download the Docker GPG key using the curl command, which you will use to interact with the Docker repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Once the key is added, enable the Docker repository on your system:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update your system’s package list, and install Docker CE (Community Edition):

sudo apt update
sudo apt install docker-ce

Installing Docker on Arch Linux

For Arch Linux users, Docker installation is quite simple. Use the following command to install it:

sudo pacman -S docker

After installation, enable and start Docker using Systemd:

sudo systemctl enable --now docker

Installing Docker on Fedora

If you are using Fedora, begin by installing the dnf-plugins-core package with:

sudo dnf install dnf-plugins-core

Next, add the Docker repository to your system:

sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

Finally, install Docker and enable it via Systemd:

sudo dnf install docker-ce docker-ce-cli containerd.io
sudo systemctl enable --now docker

Installing Docker on OpenSUSE

For OpenSUSE systems, use the following commands to add the relevant virtualization repository and install Docker:

sudo zypper addrepo https://download.opensuse.org/repositories/Virtualization:containers/openSUSE_Leap_15.5/Virtualization:containers.repo
sudo zypper install docker
sudo systemctl enable --now docker

Installing Docker on RHEL/Rocky/Alma

For RHEL, Rocky, or Alma Linux users, follow the same instructions as for Fedora, as the commands remain identical.

Creating a Docker Compose File for Your Minecraft Server

To facilitate the server setup, utilizing a Docker Compose file is highly recommended. This file allows you to predefine several configurations, such as server memory, Minecraft version, and more.

Begin by creating a dedicated "minecraft" folder to hold your Docker Compose file and any associated documentation:

mkdir -p ~/minecraft

Then enter the newly created directory:

cd ~/minecraft

Now, create a Docker Compose file named docker-compose.yml:

touch docker-compose.yml

Open the file in a text editor (like Nano) and insert the following configuration:

version: '3'
services:
  minecraft-server:
    image: itzg/minecraft-server
    environment:
      - EULA=TRUE
      - VERSION=1.20.4
    ports:
      - "25565:25565"
      - "25575:25575"
    mem_limit: 6g

Customizing Your Compose File

The default version specified in the Compose file is 1.20.4. If you need to update Minecraft to a newer release, simply edit the VERSION value. Conversely, if you want to revert to an earlier version, adjust it accordingly in the file.

You can also customize the storage location for your server data. For instance, to store all Minecraft data in /var/mc-data/, modify the Compose file to look like this:

version: '3'
services:
  minecraft-server:
    image: itzg/minecraft-server
    environment:
      - EULA=TRUE
      - VERSION=1.20.4
    ports:
      - "25565:25565"
      - "25575:25575"
    mem_limit: 6g
    volumes:
      - /var/mc-data/:/data

Ensure that the directory /var/mc-data/ exists on your host server. If it doesn’t, you can create it with this command:

sudo mkdir -p /var/mc-data/

Deploying Your Minecraft Server

Once your compose file is fully configured, deploying your Minecraft server is easy. Make sure you are inside the ~/minecraft directory where your docker-compose.yml file resides. Run the following command to start your server:

sudo docker compose up -d

Running this command will prompt Docker to read your configuration file, pull the necessary Minecraft image and server version, and deploy everything accordingly, ensuring that server data is persistent in /var/mc-data/ on the host machine.

By following these steps, you can successfully create and host your very own Minecraft server in a Docker container on a Linux system – opening a world of creativity and exploration in the beloved Minecraft universe.

By Alex Reynolds

Tech journalist and digital trends analyst, Alex Reynolds has a passion for emerging technologies, AI, and cybersecurity. With years of experience in the industry, he delivers in-depth insights and engaging articles for tech enthusiasts and professionals alike.