Run a Minecraft Server with Docker Compose


This post will show how to setup your own minecraft game server on docker using docker-compose.
Requirements
As we will run minecraft as a docker container, we require docker and docker-compose. You can follow the links to their website for instructions to install it.
In summary it will be the following to install docker:
curl https://get.docker.com | bash
sudo usermod -aG docker $USER
And the following to install docker-compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Note that I installed version 1.28.5, in future this may be outdated, so I recommend getting the latest version from their website.
You can verify if docker and docker-compose was installed by running:
docker --version
docker-compose --version
Configuration
Create the project directory:
mkdir -p ~/minecraft
Change to the directory:
cd ~/minecraft
Create the docker-compose.yml file and open it with your editor of choice, then provide this content:
version: "3.8"
services:
minecraft-server:
image: itzg/minecraft-server:latest
container_name: minecraft-server
ports:
- 25565:25565
environment:
SERVER_NAME: "Minecraft-Server"
MOTD: "Forge Minecraft Server"
EULA: "TRUE"
TYPE: "FORGE"
VERSION: "1.17.1"
MODE: "survival"
MEMORY: "1G"
LEVEL_TYPE: "DEFAULT"
ENABLE_RCON: "true"
RCON_PASSWORD: password
RCON_PORT: 28016
SERVER_PORT: 25565
ENABLE_WHITELIST: "true"
WHITELIST: "${WHITELISTED_PLAYERS}"
OPS: "${OPS_PLAYERS}"
MAX_PLAYERS: 20
ANNOUNCE_PLAYER_ACHIEVEMENTS: "true"
SPAWN_ANIMALS: "true"
SPAWN_MONSTERS: "true"
PVP: "true"
LEVEL: "cold"
TZ: "Asia/Kolkata"
GUI: "FALSE"
MODS_FILE: /extras/mods.txt
REMOVE_OLD_MODS: "true"
restart: unless-stopped
user: "${UID}:${GID}"
volumes:
- ./data:/data
- ./mods.txt:/extras/mods.txt:ro
logging:
driver: "json-file"
options:
max-size: "1m"
We want the user creating the data directory to be the same user that we are using to avoid permission issues, so we need to create a .env file and set the UID and GID values to the userid and groupid of our user:
bashCopyecho UID=$(id -u) > .env
echo GID=$(id -g) >> .env
Yours will most probably differ, but mine look like this:
cat .env
UID=501
GID=20
Next we need to set the whitelisted and ops players in WHITELISTED_PLAYERS and OPS_PLAYERS so the end result will look something like this:
UID=501
GID=20
WHITELISTED_PLAYERS=player2,player3
OPS_PLAYERS=tag1
If you don't want mods, you can remove MODS_FILE and REMOVE_OLD_MODS entry, but in this case I will be installing the Fast Leaf Decay mod from curseforge.com.
Create the file ~/minecraft/mods.txt and add the jar that you want to add to the file, as example:
https://media.forgecdn.net/files/3399/353/FastLeafDecay-26.jar
Start the Minecraft Server
Once that is in place, start the minecraft server:
docker-compose up -d
While the server is booting, you can follow the logs:
docker-compose logs -f minecraft-server
Once the server is booted, we can verify by using the healthcheck command:
docker exec -it minecraft-server mc-monitor status
You should see output similar to:
localhost:25565 : version=1.17.1 online=0 max=20 motd='Forge Minecraft Server'
Minecraft Command Line
We can access the minecraft command line with the following:
docker-compose exec minecraft-server rcon-cli
To list our whitelisted players:
/whitelist list
To restart the server:
/stop
Install Minecraft
Install minecraft java edition on your PC from: https://www.minecraft.net/en-us/download
Once installed, install the forge client: https://files.minecraftforge.net/net/minecraftforge/forge/
Open minecraft and you should see the forge under our installations. When we run our forge installation, you'll be able to add your server in the multiplayer section.
Note on Forge Mods
For forge mods to work properly you need the mods on the server and client side, for more info, see this issue: https://github.com/itzg/docker-minecraft-server/issues/1086
Backups
Create the directories for the script and backup locations:
sudo mkdir -p /opt/backups /opt/scripts
Change the permissions of those directories:
sudo chown -R "$(id -u):$(id -g)" /opt
Create the backup script in /opt/scripts/backup_minecraft.sh with the following content:
#!/usr/bin/env bash
tar -zcvf /opt/backups/minecraft/backup-$(date +%F).tar.gz ~/minecraft
find /opt/backups/minecraft/ -type f -name "backup-*.tar.gz" -mtime +7 -exec rm {} \;
This script will compress the minecraft project directory in a archive and delete any backups older than 7 days, which we will run under a cronjob.
Change the permissions of our backup script to make it executable:
chmod +x /opt/scripts/backup_minecraft.sh
Open crontab with crontab -e and add the cron expression to backup daily at 00:00:
00 00 * * * /opt/scripts/backup_minecraft.sh
Subscribe to my newsletter
Read articles from Dhairya Patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Dhairya Patel
Dhairya Patel
A DevOps Engineer with 2 year of experience as infrastructure support (AWS, Linux, Azure), DevOps (Build, CICD & Release Management)