minecraft

How to run Minecraft server in Docker

Introduction

Minecraft is a popular game played by millions across the world. Inspired by the recent tutorials I have done on Docker and that my youngest child asked if I could set up a server for him and his friends I decided to see if I could get it to run on my Docker Server

The following tutorial will get a simple Minecraft Server up and running in no time but if you want to publish your server for public access on the internet you might want to look into securing your server and how to correctly configure it.

For learning purposes though, this will give you a great start.

The Easy Way

Later in this tutorial, I will go through how to build your own docker container but if all you want is to get your container up and running as quickly as possible that you can use the container I created for this tutorial which is now published on Docker Hub.

Prerequisites

This container was build and tested on an Ubuntu 18.04 Server built using the tutorial I wrote on this site here [Install Docker on Ubuntu 18.04]

It should work on any Docker Server but if you do not have one, or know how to install one, you can follow this guide to get up and running quickly.

Running a Static Container

By far the quickest way to get your Minecraft Container up and running is to log into your Docker Server and use the Image I created as follows.

sudo docker run -ti -d -p25565:25565 -e EULA=true --name minecraft-server dbarszczak/minecraft-server:1.0.0

This will start the container in daemon mode (-d) exposing port 25565 on the host machine to port 25565 in the container (-p25565:25565).

The Minecraft Server Jar requires a text file (eula.txt) to be created with the first line containing (eula=true) in order to accept the End User License Agreement. To try honour this I made the container require the Environment Variable EULA be required in order to start (-e EULA=true).

If you want to access the container so you can issue commands to the Minecraft console you can simply run the following. Keep in mind that depending on the specification of your server it may take a few minutes to startup.

docker attach minecraft-server

Running with Docker Compose

After I created the container I decided to create a Docker Compose file that would allow me to manage my service a little easier. You can use the example I created by running the following commands.

Firstly, create a directory to store our docker-compose.yml and change to that directory.

mkdir minecraft-compose
cd minecraft-compose

Download the latest docker compose.yml from the Gut Hub Repository.

wget https://raw.githubusercontent.com/dbarszczak/minecraft-server/master/docker-compose.yml

You can check that the container is configured to your specifications by having a look at the contents of the docker-compose.yml and adjusting to your needs. Once you are happy you can start your stack with the following.

sudo docker-compose up -d

Again, if you need access to the Minecraft Console you can attach to the container using the following. Remember that your container has just started it might take a moment to respond.

sudo docker attach minecraft-compose_minecraft_1

If you attach to the container and you do not see a prompt, type the command (help) which should force the Minecraft Console to display its help page.

The Advanced Way

My simple configuration should be enough to get anyone up and running but sometime you might prefer to, or require your own Build method.

The best place to start would be to start with my Git repository and make changes as required.

git clone https://github.com/dbarszczak/minecraft-server.git

Let’s have a look at the project and see what’s going on. I’m going to start by taking a look at the Dockerfile.

FROM openjdk:8

ENV XMX=1024M
ENV XMS=1024M

ADD http://launcher.mojang.com/v1/objects/bb2b6b1aefcd70dfd1892149ac3a215f6c636b07/server.jar /usr/src/minecraft/
COPY entrypoint.sh /usr/src/minecraft/entrypoint.sh
COPY eula.txt /usr/src/minecraft/eula.txt

WORKDIR /usr/src/minecraft/

EXPOSE 25565/tcp

ENTRYPOINT [ "./entrypoint.sh" ]

At the time of writing, the version of Minecraft Server recommended OpenJDK 8 or later. After I had a quick look on Docker Hub I would an official OpenJDK 8 Docker Image so I decided to use that as the base.

FROM openjdk:8

The next thing I do in the file is to configure a couple of environment variables. This is because Java allows you to control the amount of RAM your application will use.

The amount of ram the application will start with:

ENV XMS=1024M

And the maximum amount of RAM the application is allowed to use:

ENV XMX=1024M

We later use these values in the entrypoint.sh file to start the Minecraft Server.

The next few lines add or copy files into the container when we build it.

# Download the latest Minecraft server.jar.
ADD http://launcher.mojang.com/v1/objects/bb2b6b1aefcd70dfd1892149ac3a215f6c636b07/server.jar /usr/src/minecraft/

# Copy entrypoint.sh into the container.
COPY entrypoint.sh /usr/src/minecraft/entrypoint.sh

# Copy eula.txt into the container.
COPY eula.txt /usr/src/minecraft/eula.txt

The next line tells the container which directory will be the working directory when the container starts.

WORKDIR /usr/src/minecraft/

We need to let docker know which port we plan to expose and this is done with the following line.

EXPOSE 25565/tcp

The very last thing is to let the container know which file should be run when the container is started. In this case, because we need to use environment variables, we are going to create a entrypoint.sh and create a script to run the Minecraft Server.

ENTRYPOINT [ "./entrypoint.sh" ]

Now that we have a Dockerfile we can focus on getting the rest of the files we need. The first file we want to create is entrypoint.sh which will be responsible for starting our application within the container.

#!/bin/bash

# Prepare the command.
CMD="java -Xmx${XMX} -Xms${XMS} -jar /usr/src/minecraft/server.jar nogui"

# Output the command to the logs.
echo "Attempting to run: ${CMD}"

if [[ $EULA != "true" ]]
  then
    echo "Please accept the EULA by adding -e EULA=true"
    exit
fi

# Run the command.
${CMD}

The Minecraft Server requires a file to confirm that you accept the End User License Agreement so we need to create that file eula.txt

eula=true

That’s everything you need to create a Minecraft Server Container and from this point all you need to do it build it.

docker build -t minecraft-server:1.0.0 .

Summary

This is a full little project to get a Minecraft Server running in Docker and I think for the personal server this would work quite nicely but if you wanted a full production environment you would need to take further security methods and DDoS protection needed to run a public Minecraft server.

The said, using Docker with some kind of management service that stops a container when it’s not used and starts it when someone tried to log in could be quite a nice feature that I might look into.

Let me know in the comments if you liked this post or if you have questions, concerns or improvements.

Leave a Reply

Your email address will not be published. Required fields are marked *

Close