# 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:

```bash
curl https://get.docker.com | bash
sudo usermod -aG docker $USER
```

And the following to install docker-compose:

```bash
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:

```bash
docker --version
docker-compose --version
```

## Configuration

Create the project directory:

```bash
mkdir -p ~/minecraft
```

Change to the directory:

```bash
cd ~/minecraft
```

Create the docker-compose.yml file and open it with your editor of choice, then provide this content:

```bash
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:

```bash
bashCopyecho UID=$(id -u) > .env
echo GID=$(id -g) >> .env
```

Yours will most probably differ, but mine look like this:

```bash
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:

```bash
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:

```bash
https://media.forgecdn.net/files/3399/353/FastLeafDecay-26.jar
```

## Start the Minecraft Server

Once that is in place, start the minecraft server:

```bash
docker-compose up -d
```

While the server is booting, you can follow the logs:

```bash
docker-compose logs -f minecraft-server
```

Once the server is booted, we can verify by using the healthcheck command:

```bash
docker exec -it minecraft-server mc-monitor status
```

You should see output similar to:

```bash
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:

```bash
docker-compose exec minecraft-server rcon-cli
```

To list our whitelisted players:

```bash
/whitelist list
```

To restart the server:

```bash
/stop
```

## Install Minecraft

Install minecraft java edition on your PC from: [https://www.minecraft.net/en-us/download](https://www.minecraft.net/en-us/download)

Once installed, install the forge client: [https://files.minecraftforge.net/net/minecraftforge/forge/](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](https://github.com/itzg/docker-minecraft-server/issues/1086)

## Backups

Create the directories for the script and backup locations:

```bash
sudo mkdir -p /opt/backups /opt/scripts
```

Change the permissions of those directories:

```bash
sudo chown -R "$(id -u):$(id -g)" /opt
```

Create the backup script in /opt/scripts/backup\_minecraft.sh with the following content:

```bash
#!/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:

```bash
chmod +x /opt/scripts/backup_minecraft.sh
```

Open crontab with crontab -e and add the cron expression to backup daily at 00:00:

```bash
00 00 * * * /opt/scripts/backup_minecraft.sh
```
