2022-05-24 12:08:01 +01:00
---
title : Minecraft Server on Docker ⛏ 🐳
2024-07-15 00:29:09 +01:00
date : 2022-05-24
2022-05-24 12:08:01 +01:00
author : "ahmza"
2022-05-27 12:37:52 +01:00
draft : false
2024-01-05 23:54:51 +00:00
weight :
2022-05-24 12:08:01 +01:00
2024-07-15 00:29:09 +01:00
tags : [ "games" , "docker" ]
2022-05-24 12:08:01 +01:00
description : "Ever wanted to build a Minecraft server in docker !! Start here"
---
## **Introduction**
2024-01-05 23:54:51 +00:00
Minecraft is a great game and better with friends. A dedicated server allows you
to do that[^1].
2022-05-24 12:08:01 +01:00
2024-01-05 23:54:51 +00:00
A Minecraft server does not require a ton of resources to run, but from my
experience the more resources you can throw at it the better when you start to
introduce mods and more players.
2022-05-24 12:08:01 +01:00
2024-01-05 23:54:51 +00:00
I run our server on my main PC. This allows me to leverage the system resources
in my PC 🦾 leaving the [Pi
Cluster ](https://blog.ahmza.com/posts/building-a-docker-swarm-with-pis/ ) system
resources to handle the rest of the stuff in my lab.
2022-05-27 12:37:52 +01:00
## **Prerequisites**
- Docker
- Docker Compose
## **Requirements**[^2]
#### **Minimum (1-5 Players)**
- Intel Pentium 4 or AMD Athlon based CPU
- 512 MB RAM
- 2 GB Storage space
#### **Recommended (10-15 Players)**
- Intel Core or AMD K8 based CPU
- 3 GB RAM
- 16 GB Storage Space
#### **Optimal (20+ Players)**
- Intel i5/i7 or AMD Ryzen 5/7 Based CPU
- 6 GB RAM
- 35 GB Storage Space
## **Building the Container**
1. **Create a directory in which you will keep the Minecraft server files**
2024-01-05 23:54:51 +00:00
```bash
# Make the folder
mkdir minecraft-server
# Ender the folder
cd minecraft-server
```
2022-05-27 12:37:52 +01:00
2. **Create an environment file to be used by the Docker Container**
2024-01-05 23:54:51 +00:00
- The _.env_ file allows us to specify variables used in the build process.
```bash
# Create a .env file
touch .env
# Open in a text editor
vim .env
# Enter the following variables
CONTAINER_NAME = minecraft-server
SERVER_MEM = 4096 #change this to your desired value (in MB)
SERVER_JAR = minecraft_server.<version>.jar # change the version
```
2022-05-27 12:37:52 +01:00
3. **Create the docker-compose.yml file**
2024-01-05 23:54:51 +00:00
- The _docker-compose.yml_ file specifies the container that we want to build [^3]
```bash
# Create the file
touch docker-compose.yml`
# Open in a text editor
vim docker-compose.yml
```
```yaml
version : "3"
services :
minecraft-server :
build :
context : .
args :
SERVER_MEM : ${SERVER_MEM}
SERVER_JAR : ${SERVER_JAR}
volumes :
- .server-data:/server
ports :
- 25565 : 25565
- 25575 : 25575
container_name : ${CONTAINER_NAME}
restart : unless-stopped
volumes :
server-data :
```
2022-05-27 12:37:52 +01:00
4. **Create the Dockerfile file**
2024-01-05 23:54:51 +00:00
- The Dockerfile builds the container image.
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
```bash
# Create the file
touch Dockerfile`
# Open the file in a text editor
vim Dockerfile
```
```Dockerfile
FROM eclipse-temurin:17-jre
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
ARG SERVER_MEM
ARG SERVER_JAR
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
ENV SERVER_MEM = ${ SERVER_MEM }
ENV SERVER_JAR = ${ SERVER_JAR }
WORKDIR /server
CMD echo "Minecraft Server is starting\n" && java -Xms128M -Xmx${ SERVER_MEM } M -jar ${ SERVER_JAR } nogui
```
2022-05-27 12:37:52 +01:00
## **Setup the Server files**
We’ re almost there, just a few steps left !!
2024-01-05 23:54:51 +00:00
The curious among you may have realised when trying to start the container that
it fails with the error below:
2022-05-27 12:37:52 +01:00
```bash
Minecraft Server is starting
Error: Unable to access jarfile server.jar
```
This is because we have not downloaded the server jar file yet, that is
our next step.
2024-01-05 23:54:51 +00:00
1. **Navigate to one of the following sites:** [^4]
- [Vanilla Minecraft Server ](https://www.minecraft.net/en-us/download/server ) - instructions found here[^5]
- [Fabric Mod loader server ](https://fabricmc.net/use/server/ ) - instructions found here [^6]
2022-05-27 12:37:52 +01:00
2. **Create a folder in the Minecraft-server folder called .server-data**
2024-01-05 23:54:51 +00:00
```bash
mkdir .server-data
cd .server-data
```
2022-05-27 12:37:52 +01:00
3. **Download the jar file - This will depend on the option above so read the instructions**
4. **Go back to the root minecraft-server folder**
2024-01-05 23:54:51 +00:00
```bash
cd ..
```
2022-05-27 12:37:52 +01:00
5. **Run the container**
2024-01-05 23:54:51 +00:00
This will create our eula.txt as well as other files needed.
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
```bash
docker compose up # use CTRL+C to exit
```
2022-05-30 19:23:23 +01:00
2024-01-05 23:54:51 +00:00
```bash
minecraft-server | Starting net.minecraft.server.Main
minecraft-server | [ 18:05:50] [ ServerMain/INFO] : You need to agree to the EULA in order to run the server. Go to eula.txt for more info.
minecraft-server exited with code 0
```
6. **Accept the EULA**
```bash
# enter the .server-data folder
cd .server-data
# edit eula.txt in a text editor
vim eula.txt
# change `eula=false` to `eula=true`
eula = true
```
7. **While in the .server-data folder make any changes you want**
- Notable files:
- **server.properties** - properties like server name, game mode, world type etc…
- **ops.json** - list of server operators (easier to set ingame)
- **whitelist.json** - list of players allowed to join the server everyone else rejected access.
8. **Run the container**
```bash
# -d flag allows the container to run in detached mode i.e in the background
docker compose up -d
```
```bash
# monitor the logs as the server starts. CTRL+C to exit
docker logs minecraft-server --follow
# If you see the bottom line then your server is up and running
[ 18:17:01] [ Server thread/INFO] : Time elapsed: 25529 ms
[ 18:17:01] [ Server thread/INFO] : Done ( 34.571s) ! For help, type "help"
```
## Conclusion
2022-05-30 19:23:23 +01:00
2024-01-05 23:54:51 +00:00
Well done you have setup a Minecraft server, now go play !!
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
There is a lot of cool stuff you can do from this point, for example installing
mods or creating scripts to manage the server.
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
[^1]:
Hosting your own dedicated server allows you to modify every aspect of the
server as you see fit. I.E you can modify the server-properties, you can
implement a whitelist not to mention being able to add any mods you want
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
[^2]: Requirements Gathered from here [https://minecraft.fandom.com/wiki/Server/Requirements/Dedicated#Unix\_.28Linux.2C_BSD.2C_macOS.29 ](https://minecraft.fandom.com/wiki/Server/Requirements/Dedicated#Unix_.28Linux.2C_BSD.2C_macOS.29 )
[^3]:
Yaml is very picky 😞 when it comes to formatting so if you encounter any
errors double check your formatting !!
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
[^4]:
I have left Forge out as it is a more involved process plus Fabric is
better 😜
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
[^5]:
Right click the green link that says something like minecraft_server.x.xx.x.jar and copy the link address
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
`curl -o server.jar \<link you copied here\>`
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
[^6]:
Select the Minecraft version, Fabric Loader Version and the Installer Version of your choice. I tend to go with the latest.
2022-05-27 12:37:52 +01:00
2024-01-05 23:54:51 +00:00
`Copy the curl command under CLI download`