Hosting An OpenTTD Server Using Docker

Cornelis Terblanche
6 min readMay 20, 2021

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

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

Remove Docker(if present)

apt-get remove docker docker-engine docker.io containerd runc

Install Docker

sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release gitsudo curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgecho "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io

Create Directory & Config

git clone https://github.com/KR34T1V/OpenTTD-base-config.git /tmp/openttdmkdir -p ~/.openttd-server/ && cp /tmp/openttd/openttd.cfg ~/.openttd-server

Setup Container

sudo docker run -d --restart unless-stopped -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

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:

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

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.

apt-get remove docker docker-engine docker.io containerd runc

Dependencies

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

apt-get install apt-transport-https ca-certificates curl gnupg \
lsb-release git

Add docker’s official GPG key to the system

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

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

echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

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

sudo apt-get update

Install our chosen docker build:

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

#2 OpenTTD

Optional Config

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

git clone https://github.com/KR34T1V/OpenTTD-base-config.git /tmp/openttd

Then copy the config to the games volume directory

mkdir -p ~/.openttd-server/ && cp /tmp/openttd/openttd.cfg ~/.openttd-server

Docker Image

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:

sudo docker run -it --rm

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.

Environment Variables

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”

Ports

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

Container Naming

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

--name openttd-{UNIQUE_NAME}

Docker Image

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…

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

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.

sudo docker run -d --restart unless-stopped -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

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

sudo docker start openttd-serversudo docker restart openttd-serversudo docker stop opentsudo docker logs openttd-servertd-server

#5 Logs

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

sudo docker logs openttd-server

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

sudo docker logs openttd-server -f

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

nano ~/.openttd-server/openttd.cfg

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

sudo docker restart openttd-server

--

--

Cornelis Terblanche

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