Run your own Terraria Server! A demonstration of the portability and friendliness of containers — Part 1

Joshua Miller
9 min readMay 12, 2020

A fishing enthusiast’s* guide to quickly setting up a persistent Terraria Server, and then getting a bit extra with it (e.g. custom URL)

My rendition of the Amazon Lightsail, Docker, and RaspberryPi icons done in Terraria (I am not an artist)

As alluded to in the title, this is going to be a multi-article article (maa? 🐑):

  • Part 1 (this part) focuses on the who and why of this series. It will also go over the parts of how that don’t cost money. Gotta love free.
  • Part 2 will contain the parts of this article that do cost some money, but may provide a better long term solution if you are really interested in running your own server (whether it be for games, websites, or services). This part will also start with setting up a free domain name (URL) for your server.

Article Organization

  • Why did I write this?
  • Who is this article really for?
  • Containers??
  • Steps to set up a server
  • You have a server! Now what?
  • Bonus Lesson: Container resource limits
  • *So… how am I a fishing enthusiast?
  • In closing

For those that just care about how to set up your own server, skip to the Steps to Set up a server and You have a server! Now what? sections. Or don’t. You do you. Alternatively, you can find a shorter version of the instructions on GitHub (JoshuaTheMiller/Terraria).

Why am I writing this?

With the pending release of the “final” Terraria update on May 16, 2020, I thought it would be a perfect time to share some of the lessons learned and steps I’ve taken around hosting my own server!

Who are these articles for?

  1. Technologists interested in cloud platforms, specifically AWS (as of now), and containers.
  2. Gamers wanting to run their own server.
  3. Those who just like reading about technology.

Containers??

Containers are awesome. They allow you to configure and run an application the same way anywhere you want. Want to run a website locally the exact same way you’d run it on some cloud platform? Awesome, that’s a one line command. Want to run a Terraria Server at home for your family, and then move it to another host with no change in configuration? You can do that too. Are you cheap like me, and move your applications to whichever cloud platform seems to be the cheapest? Well, containers make that almost trivial.

If that got your attention, good! It still doesn’t really explain what a container is though. Docker, one of the most popular containerization platforms, summarizes it well:

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. ~ Docker

Hopefully this helps to illustrate why I like containers.

In other words, containers allow you to run isolated services with minimal setup very efficiently.

Steps to set up a server

This section will be broken up into a few more pieces:

  1. Getting Docker installed on your computer.
  2. Getting Terraria up and running.
  3. Setting up port forwarding so others can join your server.

1. Install Docker

I won’t go into too much detail on this, as it is documented very well elsewhere:

2. Run the Terraria container

Running a container can be as simple as copy+pasting

One of the beautiful parts of using containers is how easy it makes running headless applications (i.e. applications that run in the background, like a server)! Just run the following command from the command line, and then wait a few minutes (if you are using Linux based OSes, you may have to prefix commands with sudo):

docker run -d -p 7777:7777 --memory=500m --mount source=terraria,target=/world --name="terraria" trfc/terraria:latest -autocreate 1 -world /world/Terrarium.wld -password PleaseChange!

Doing so will pull the container image (i.e. download it) and run it with the following parameters and options:

  • -d run the container in detached mode so you can go about your day.
  • -p 7777:7777 map port 7777 to the container’s port 7777 so that you can connect to the server (since the command does not change the server from its default port).
  • --memory=500m set the max amount of memory to use so that your system doesn’t crash because of out of memory exceptions.
  • --mount source=terraria,target=/world create (or attach to if existing) a volume so that your world persists even when the container shuts down.
  • --name=”terraria” name the running container “terraria”
  • trfc/terraria:latest use the latest version of the trfc/terraria image (my image!)
  • -autocreate 1 -world /world/Terrarium.wld create a small world named Terrarium OR use an existing world named Terrarium (default Terraria server command)
  • -password PleaseChange! sets the server password to “PleaseChange!” (custom command courtesy of this image)

To update to a newer version of the image, run docker pull trfc/terraria:latest before running the command above (if you have ran it before).

3. Forwarding your Port

Exact steps can be different per router. Plenty of information for your specific setup should be easily discoverable with a quick web search. Also, please review the comments I made about handing out your IP address under the Bonus Lessons section (long story short, be careful about it).

Before we get too far along in this section, it is important to have an appreciation of port forwarding… To quote Wikipedia, “port forwarding or port mapping is an application of network address translation (NAT) that redirects a communication request from one address and port number combination to another.” If that explanation doesn’t help, I found that CCTV Camera Pros has a pretty good YouTube video about it. While it seems to focus on cameras, the concepts still apply. In the end, two important things to know are:

  • Only expose what you intend to expose, otherwise you may cause yourself issues.
  • Port forwarding is just one way to allow others to connect to your server.

So, how can you go about port forwarding? Start by gathering some information:

  1. The IP address of the computer your server is running on.
  2. The port your server is exposed on (if you used the command above, it is port 7777).
  3. The IP address of your router.
  4. The brand of your router.
Finding my IP Address info, and my router

If you are lucky, this can all be accomplished with one command:

In the Gif above, you see me highlight Default Gateway, and then navigate to it in my web browser. While your numbers may be different, Default Gateway should be your router’s IP address (alternatively, many routers have their default IP address physically listed on them). Another item to look for is IPv4 Address. This will be the address of the computer you are running the command on. Make sure to run the command on the same computer your server is on.

Configuring port forwarding on my Asus router

Now that you have your IP address noted down, you’ll have to go to your router’s administrative portal to configure port forwarding. To do so, open up a web browser and type in the value for Default Gateway (for me, it was 192.168.50.1). I highly recommend looking at your router’s documentation for how to configure this. The Gif above this section shows what configuring port forwarding looked like on my Asus router.

Please note that when you set up port forwarding, you will need to allow both UDP and TCP connections for Terraria.

The last step in all of this is to find your public IP address. This is what your friends will use to connect to your server from wherever they are. WhatIsMyIP.com is a very popular site for finding this information, but feel free to use what you prefer.

At this point, you should be good to go! Share your public IP address with only those you trust, and play on!

You have a server! Now what?

Now that you have a server, you probably will want to do at least two things….

Accessing the admin authorization code

Running the following command will show you your Terraria Server’s auth code (as long as you haven’t changed any of the default settings of the server):

docker run --rm --mount source=terraria,target=/world --name="volumeinspect" trfc/vimtainer cat /world/authcode.txt

This will spin up another image of mine, print out the contents of the authcode file, and then remove the container.

Shutting down and restarting the server

The easiest and safest way to shut down and restart your server, at least as far as this tutorial is concerned, is from within the game world itself. To do this you are going to need either the auth code from above, or admin privilege in the game.

A demonstration of shutting down the server from in game.

Once in game, go into admin mode by hitting enter, then typing /auth <authcode> , where “<authcode> is your code from above. Then, hit enter again, and type /exit . This will shut down the server, which causes the container to also stop as the server was the last running process.

Since the container stopped, you will have to start it again. If you used the command from earlier, this can be accomplished by typing, docker start terraria.

If you removed/deleted the container, no worries! Since all of your settings were persisted in the volume that was created upon container run, all you have to do is run the command from earlier again.

Bonus Lesson: Container resource limits (Docker edition)

Containers, by default, have no resource constraints. This means that they can consume as much of a given resource (e.g. RAM, CPU, etc) that they are allowed to. This can lead to some really strange errors, and to your server and your computer crashing. What is the lesson here? Limit resource consumption (you can always change it later). Some helpful flags when using docker run are as follows:

  • --memory=<value> where <value> is something like 4m (4 megabytes, the lowest allowed value), or 2g (2 gigabytes)
  • --cpus=<value> where <value> is something like “1.5” or “1”

From my experience so far, --memory=500m is good enough for a small world with 4 players. I have not played around with the cpu setting enough to make any recommendation (I’ll update this if I ever get around to it).

If you go and reread the instructions for running containers from the earlier sections, you’ll see that I added memory constraints to all of the commands.

Check out the Runtime options with Memory, CPUs, and GPUs article on docs.docker.com for more information.

So… how am I a fishing enthusiast?

If you are thinking I’m talking about real life, I’m not (sorry mom). For some reason I enjoy sitting at my computer and fishing in Terraria… So much so that my significant other only knows this game as, “that one fishing game.”

This GIF of me fishing is cut very short... I accidentally stayed here for an hour after recording it.

List of key takeaways

After reading this, you may have a better understanding around the following:

  • How to start a Terraria server
  • How to print out the contents of a file that exists in a Docker volume (authcode)
  • That resource limits can and should be placed on Docker containers (memory )
  • That a thing called port forwarding exists, and it is cool but care needs to be taken.
  • I’m not a real fisherman.

In closing

  • Congrats on making it this far in the article!
  • My sincere thanks to redigit and the whole Terraria team for making something that so many people, including myself, love 💘
  • Keep an eye out for the next article in this series if you are interested in steps for hosting on RaspberryPi and AWS!

--

--

Joshua Miller

Just a guy trying to share some knowledge about various topics that he finds interesting.