Setup a dedicated Valheim Server with Docker
Prerequisites
If you do not have Docker installed already here is a guide on how to install Docker on Ubuntu 22.04 LTS or Debian 12: Guide
First login to your server via SSH:
ssh [email protected]
user@linux-server's password: ********
user@linux-server:~$
Then become root so that you can easily edit any file:
user@linux-server:~$ sudo su
[sudo] password for user: ********
root@linux-server:/home/user# cd
root@linux-server:~#
I recommend putting your Docker Compose file and persistent data in a dedicated folder in /opt.
mkdir /opt/valheim-server
cd /opt/valheim-server
Install Valheim dedicated server
Now that we are in the correct directory we can start to create the files needed for Docker Compose.
Then we are going to create a file called valheim.env:
vim valheim.env
Next we are going to insert the following into the file:
SERVER_NAME=YOUR_SERVER_NAME
WORLD_NAME=YOUR_WORLD_NAME
SERVER_PASS=your-server-password
SERVER_PUBLIC=true
Make sure to edit these values so they are correct for your needs.
Example:
SERVER_NAME=Best-Vahleim-Server
WORLD_NAME=world1
SERVER_PASS=super-$ecert-P@ssw0rd
SERVER_PUBLIC=true
Now we can create another file called docker-compose.yml:
vim docker-compose.yml
Insert the following:
version: "3.9"
services:
dedicated-server:
image: ghcr.io/lloesche/valheim-server
cap_add:
- sys_nice
volumes:
- ./config:/config
- ./data:/opt/valheim
ports:
- "2456-2458:2456-2458/udp"
- "9001:9001/tcp"
env_file:
- ./valheim.env
restart: always
stop_grace_period: 2m
Now we can start our server with this command:
docker compose up
This will pull the requiered Docker images and create the containers, networks and directories as stated in the docker-compose.yml file.
The output should looke something like this:
[+] Running 8/8
✔ dedicated-server 7 layers [⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled
✔ f03b40093957 Pull complete
✔ c3b8e20248d6 Pull complete
✔ ffdaa2ab5f37 Pull complete
✔ 45743077b2cb Pull complete
✔ db3f23abdb79 Pull complete
✔ 35939bb5b3f5 Pull complete
✔ 50fb25403e58 Pull complete
[+] Running 2/2
✔ Network valheim-server_default Created
✔ Container valheim-server-dedicated-server-1 Created
Attaching to valheim-server-dedicated-server-1
Once the preperations are complete Docker Compose will automatically attach to the container so we can see the logs. Because this is the first time we are starting this server it will take some thing depending on your WAN Bandwith.
As soon as this message appears:
Game server connected
we can press CTRL + C to stop the container. DO NOT PRESS CTRL + C a second time, because this will force stop the server and could lead to data loss.
Now that we have a running Valheim server we can either start the server up and play in the newly created world or import our own world.
Import existing world
If you want to import you own world you can put the world files into the directory /opt/valheim-server/config/worlds_local on your server. Also do not forget to set the WORLD_NAME variable in the valheim.env file to the name of your imported world or valheim will again start with the empty new world. And then start the server like this: to run this command you need to be in the /opt/valheim-server directory
docker compose up -d
With the option -d we say that it should run as a daemon, in other words as a service or in the background. This means you can safely disconnect from your SSH session and even reboot your server and the Valheim server will always start and shutdown safely.
Create new world
If you don't want to import your world or just want to create a new one you can just start the server without importing anything. Then Valheim will create a new World with the WORLD_NAME specified in the valheim.env file.