Skip to content

Instantly share code, notes, and snippets.

@nullconfig
Last active March 25, 2021 20:39
Show Gist options
  • Save nullconfig/db9e267990616b7e26e03cfc7ec5ae3f to your computer and use it in GitHub Desktop.
Save nullconfig/db9e267990616b7e26e03cfc7ec5ae3f to your computer and use it in GitHub Desktop.
Dedicated Valheim server running inside docker

header

Table of Contents

  1. Installation
  2. Starting and Updating
  3. Troubleshooting
  4. Migrating

Ubuntu docker and docker-compose installation

Info: This tutorial assumes that you are familiar with the linux command line interface and vi. You will need to configure port forwarding or open network ports to allow steam and other players to connect to the server remotely. The server requires UDP ports 2456 - 2458 to be open in order for the connections to work successfully. This tutorial does not cover the networking portion.

Just copy and paste this below to setup prereqs for Ubuntu and docker repo.

sudo apt install apt-transport-https ca-certificates curl software-properties-common
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

Now update apt to pull in the updated repositories, and install docker

sudo apt update
sudo apt install docker-compose docker-ce

Ensure docker will start on boot

sudo systemctl enable docker-ce

Compose file

This setup leverages docker-compose to manage the container. This simplifies and clearly states the intention of this container, and allows it to be ran on any OS with docker and docker-compose installed.

For the compose file there are a few variables that need to be set.

Variable Description
server_name The name of your server to be displayed
world This is the name of the seed. It will generate two files matching
the servers name ending with .db and .fwl
example: server_name.db and server_name.fwl
password password to join the server
public 1 or 0
1 being public and 0 being private

Run sudo vi /opt/valheim-compose.yml to open the file and paste the contents below inside of the file.

version: '3'

services:
  valheim_server:
    image: steamcmd/steamcmd:latest
    container_name: valheim_server
    hostname: valheim_server
    environment:
     - templdpath=$LD_LIBRARY_PATH
     - LD_LIBRARY_PATH=/data/linux64:$LD_LIBRARY_PATH
     - SteamAppId=892970
     - server_name="YOUR VALHEIM SERVER"
     - world="Dedicated01"
     - password=CHANGEME
     - public=1
    ports:
      - 2456-2458:2456-2458/udp
    networks:
      steam-net:
        ipv4_address: 10.0.0.3
    restart: unless-stopped
    command: ["/bin/bash"]    
    volumes: 
      - steam_stuff:/data

volumes:  
  steam_stuff:

networks:
  steam-net:
    ipam:
      driver: default
      config:
        - subnet: 10.0.0.0/28

Create server script

Copy contents below into the script with sudo vi /opt/server_start.sh

#!/bin/bash

ln -s /data/adminlist.txt /root/.config/unity3d/IronGate/Valheim/adminlist.txt
ln -s /data/bannedlist.txt /root/.config/unity3d/IronGate/Valheim/bannedlist.txt
ln -s /data/permittedlist.txt /root/.config/unity3d/IronGate/Valheim/permittedlist.txt
ln -s /data/prefs /root/.config/unity3d/IronGate/Valheim/prefs
ln -s /data/worlds /root/.config/unity3d/IronGate/Valheim/worlds

/data/valheim_server.x86_64 -name $server_name -port 2456 -world $world -password $password -public $public

Make the script executable

sudo chmod +x /opt/server_start.sh

Spinning up the server

Now that the files are in place the container can be started.

Starting the server

sudo docker-compose -f /opt/valheim-compose.yml up -d

Installing the application on the server

This command will be used to update the server later as well.

sudo docker exec -it valheim_server bash -c "steamcmd +login anonymous +force_install_dir /data +app_update 896660 +quit"

Add server specific files

sudo docker exec -it valheim_server bash -c "\
mkdir -p /root/.config/unity3d/IronGate/Valheim 
mkdir -p /data/worlds
touch /data/{adminlist.txt,bannedlist.txt,permittedlist.txt,prefs}
"

Move the server_start.sh script into the container

sudo mv /opt/server_start.sh /var/lib/docker/volumes/opt_steam_stuff/_data/server_start.sh

Systemd files to automatically start the server on boot

Once the script is in place and all prereq files are present on the box you are now ready to start the script which launches the server. The server will managed by docker-compose via systemd. Run vi /etc/systemd/system/valheim.service to create and open the file, and paste in the contents below.

[Unit]
Description=Valheim Server
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
User=root
ExecStartPre=docker-compose -f /opt/valheim-compose.yml up -d
ExecStart=/usr/bin/docker exec valheim_server bash -c "/bin/bash /data/server_start.sh"
ExecStop=docker-compose -f /opt/valheim-compose.yml down

[Install]
WantedBy=multi-user.target

Since this service was just create you do not need to reload the daemon, but out of habit I usually do this anyway with

sudo systemctl daemon-reload

Stop the running container and now restart it via systemd

sudo docker-compose -f /opt/valheim-compose.yml down
sudo systemctl start valheim.service
sudo systemctl enable valheim.service
sudo systemctl status valheim.service

Updating and restarting server

# updating
sudo docker exec -it valheim_server bash -c "steamcmd +login anonymous +force_install_dir /data +app_update 896660 +quit"

# restarting
sudo systemctl restart valheim.service

Troubleshooting

Why isn't my server running?

To run adhoc commands for troubleshooting run the command below to access the command prompt inside the container.

sudo docker exec -it valheim_server bash

You can then verify if the script start by looking for the symlinks. If the symlinks exist launch the server script to see what type of errors appear or verify where the server is failing from the output on the screen. Check for the symlinks to ensure the script was started

sudo docker exec -it valheim_server bash -c "ls -lah /root/.config/unity3d/IronGate/Valheim/"

Manually starting sript for troubleshooting

cd /data
./server_start.sh

Migrating

The server can easily be migrated if created on a local workstation and wanting to move it to a public cloud provider or vice versa granted the new host has docker and docker compose installed.

To migrate the server you'll need gzip installed on both hosts or some other archiving tool. If you setup the systemd file you can shut the server down this way, otherwise manually shutdown the container.

sudo systemctl stop valheim
sudo docker save valheim_container | gzip | ssh <your_username>@<destination_ip_address> 'gunzip | sudo docker load'
zip -r steam_stuff.zip /var/lib/docker/volumes/opt_steam_stuff/_data
scp steam_stuff.zip ssh <your_username>@<destination_ip_address>:/home/<your_username>

Now login into the target machine and unzip the docker volume, and move it into the appropriate location with rsync. Go back over the installation process, and double check all prereqs are in place.

cd /home/<your_username>
unzip opt_steam_stuff.zip
sudo rsync -avr opt_steam_stuff/_data/ /var/lib/docker/volumes/opt_steam_stuff/_data
sudo systemctl start valheim
@nullconfig
Copy link
Author

header

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment