Hosting An OpenTTD Server Using Docker

Foreword

I often have a hard time following documentation and wish there was someone who would simplify the process and just put thing in a simple to follow process.

Here I will share my learning journey about hosing my own official OpenTTD Server. Because I have used Docker in the past I will use this opportunity to teach you about how great it is for this scenario, (Also, I am not a Dev-Ops engineer)

I will not be teaching you how to use docker, only how to use it to fulfill this scenario. I will also be assuming that you already have debian based linux server to deploy the container from, and that you are connected to the host computer’s Command Line Interface(CLI).

Too Long Didn’t Read (TLDR):

Copy and paste these lines to a debian CLI

Update

Remove Docker(if present)

Install Docker

Create Directory & Config

Setup Container

Getting Started

So in order to get started we will need to make sure that all our packages are up to date and we can do this by running the following command:

If you encounter any permission errors you may need to use the sudo command and use the following commands instead:

sudo apt-get update && sudo apt-get upgrade -Y

Once successful we can move on to install the newly require packages to run our docker container.

#1 Docker

Installing docker is rather straight forward if you follow their guide here, (keep in mind we are using debian)

Cleaning out any old docker packages currently installed.

Docker requires the following packages to be installed before you can install docker & we need git to download the config file.

Add docker’s official GPG key to the system

Now we can choose what package version we want to use: (stable)

Update our package manage to update the index with the new source:

Install our chosen docker build:

#2 OpenTTD

You can use the config file I have prepared for this guide, or you can just modify the one generated when the game 1st runs.

Start by cloning the config to the tmp directory

Then copy the config to the games volume directory

We will be using this container repository, its very well put together by bateu from docker hub here.

This is the full command that we will be using to test our container:

sudo docker run -it --rm -e PUID=$(id -u $(whoami)) -e PGID=$(id -g $(whoami)) -e “loadgame=last-autosave” -v ~/.openttd-server:/home/openttd/.openttd -p 3979:3979/tcp -p 3979:3979/udp --name openttd-server bateau/openttd:1.11.2

I will go ahead and break the it down for you so you can see what we are doing:

These are standard docker commands, you can and should read more about them here. We will later replace -it with -d and--rm with--restart unless-stopped but we only do that when we know everything is ready to run.

According to the documentation we need the openttd user to share the same UID & GID as the volume owner. We can grab the currently logged in user and match it with the container.

To determine which UID and GID to pass to the container we use the id command along with the whoami command.

I combined the commands using the bash inline variables $()

-e PUID=$(id -u $(whoami)) -e PGID=$(id -g $(whoami))

We will also need to assign a docker volume to be used for storing the server files:

-v ~/.openttd-server:/home/openttd/.openttd

If you are running multiple instances make sure to change the first parameter in the -v tag to be unique, this will prevent servers sharing config and save data.

-v ~/.openttd-{INSERT UNIQUE IDENTFIER}:/home/openttd/.openttd

Again following the documentation we see that we can set which save we want to load, I will set this to the load the last auto-save.
-e “loadgame=last-autosave”

Now we need to tell docker what port we want the container to run on.
We do this by binding an unuserd port on the server to the docker container’s application, (in this case openTTD). I found those here:

-p 3979:3979/tcp -p 3979:3979/udp

If you want to change what port the container is bound to you can again change the first parameter in the -p tag:

-p {INSERT_SERVER_PORT}:3979/tcp -p {INSERT_SERVER_PORT}:3979/udp

We can keep track of each instance of our games by assigning a unique name to each instance.

--name openttd-{UNIQUE_NAME}

Finally we tell docker exactly what we want it to run inside our container.
Luckily thanks to the Docker image linked above we do not need to get into writing dockerfiles

bateau/openttd:1.11.2

As later versions come out you may want to update to a more recent docker container image, simply change the version tag:

bateau/openttd:{IMAGE_VERSION_TAG}

3# Testing

If you have made it this far congratulations, we are now nearing the point where you will be heading into the wild to explore your own errors, and issues. But remember the mighty search engine will be there to assist.

Okay great, lets run this bad boy…

If everything went successfully you should see the log files streaming in front of you.

You can stop the container by pressing Ctr + C on the keyboard.

#4 Deploy

After testing that our container runs successfully we want to run it in a detached state, freeing up our CLI to do other things.

We will be replacing -it with -d, and --rm with --restart unless-stopped to always keep the server up and running.

To start/restart/stop your server you can run the following command with the container name:

#5 Logs

If you would like to view the log from your container you can run the following command:

in order to steam the log you can add the -f or--follow tag to the command:

#6 Config

Great now that you have your test container ready, you can dive into the config file and configure your server. We will be using nano to modify the file.

However you can use whatever text editor you like. If you want to learn to use nano you can read this guide.

You can locate the config file inside the defined game volume directory (remember to match the directory with the one assigned to the container)

You can also find any additional game files inside this directory, should you want to add mods/additional content.

After changing the config you will want to restart your container

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cornelis Terblanche

Cornelis Terblanche

Robotics & Automation enthusiast, with a brief history of working in the software development sector. A large passion for entrepreneurship and open sourcing.