Setting up a Minecraft Docker Server on Ubuntu 18.04 with Azure

A project I worked on recently was to create a Minecraft server as cheap and efficiently as possible. If you do a quick google search, you’ll find that there are endless options when it comes to hosting Minecraft servers. However, many of the external hosting providers you will find are either very expensive or are low performance, so most people are stuck with hosting the server on their own personal computer via port forwarding. One option I considered for server hosting was using a dedicated VPS. An advantage to using a VPS is that you have full customization over the hardware and system components for your virtual machine. While researching cloud services, I found out on Microsoft’s website that they offered 100$ free credit for VPS services for students.

In addition, other websites like Digital Ocean also offer free credit for anyone with an .edu email. I thought that this would be a fun and interesting project to see how powerful I could make my server for the cheapest cost. I also ended up deploying the server in a docker container for easier management, and to separate it from my other projects.

Creating the VM

To maximize resources and cost, choosing a Linux distro as the host OS is the way to go. I decided to run the server on Ubuntu 18.04 Server.

The best deal I could get for 4gb of RAM was 30$/month. Setting up the Virtual Machine was surprisingly easy and painless, and I’ll probably be using this VM for future projects/blogs.

The next thing you need to consider is how you are going to connect/transfer files to your VPS after its been setup. For Windows Server, you would use RDP on port 3389, but in my case I’m using SSH with password authentication:

If you already have the core files for your MC Server already installed, you can transfer the files to the vm with a command such as scp:

scp file user@[host/ip]:directory/targetfile

I also needed to download a couple of extra files, which you can perform with curl:

curl [URL/download link]

The next set of tasks I needed to accomplish was

  1. Allow an inbound networking rule to allow port 25565 on TCP and UDP
  2. Set the server up in a docker container

Exposing port 25565 on the VPS is a fairly simple task. You can access networking rules in the “Network Security Group” settings on the Azure Panel.

By default, there will be an Implicit deny ACL as well as an ACL to allow SSH/RDP connections. Go ahead and add a new Inbound rule and add port 25565 as shown:

Setting up the server configurations

After you complete this step, you can move on to the final steps for your server. For a fresh Ubuntu install, you will want to update your repositories, and install Java JRE 8.

sudo apt-get update
sudo apt install openjdk-8-jre

Once you’ve installed/transfered the files for your Minecraft server, you need to update the server.jar file with the proper server configurations and version number:

java -Xmx2048M -Xms1024M -jar server[VERSION].jar nogui 

For the java command listed above, -Xmx specifies the maximum RAM usage for the server, and -Xms specifies the minimum RAM usage. the nogui parameter specifies that we are not using a gui. We also need to accept the eula.txt, so nano/vim into the file and set it to true before you turn the server on.

And that’s it! your free Minecraft server should be fully functional at this point, with minimal lag. I even tested this process with a modpack server and it worked perfectly. Optionally, you can run your Minecraft server in a docker container, which will treat the server as a service rather than a java file. If you want, you can follow the steps below to setup Docker

I’m using Docker’s documentation for installing the Docker Engine on Ubuntu. I’m not going to go into much detail with the commands, and you should probably follow the docs for a better explanation. However, I’ll be showing what I used in order to set this up properly.

Deploying server in a docker container

Install packages to allow apt to use a repository over HTTPS:

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

Add Docker’s official GPG key:

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

Verify that you now have the key with the fingerprint:

sudo apt-key fingerprint 0EBFCD88

Setup a stable repository for Ubuntu amd64

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

Install Docker Engine

sudo apt-get install docker-ce docker-ce-cli containerd.io

After we get those prerequisites out of the way, we can configure the Docker Image.

nano Dockerfile

Paste the following:

FROM java:8
WORKDIR /app/
ADD . /app/
CMD java -Xmx2048M -Xms1024M -jar server[VERSION].jar nogui

Build the Docker image

sudo docker build . --tag minecraftserver

Create a Docker volume

sudo docker volume create servervolume

Run the docker image

sudo docker run -p 25565:25565 --mount source=servervolume,target=/app minecraftserver

You’re Minecraft server is now a successfully running service!

Leave a Reply

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