beardedio / terraria

Docker images to run a Terraria Server. Images with TShock Server or Vanilla Server are available.
MIT License
116 stars 55 forks source link

Clean shutdown #4

Open bdr99 opened 4 years ago

bdr99 commented 4 years ago

When I stop this container, does it perform a clean shutdown of the server? In other words, does it command the Terraria server to save the world file before killing the process? Or is it possible that world data could be lost upon stopping the container if the world file was not saved recently?

If not, then can the container be modified to include this feature so that world data will not be lost?

docker-compose config:

version: '3.5'
services:
  terraria:
    image: beardedio/terraria
    container_name: terraria
    environment:
      - world=world.wld
    ports:
      - 7777:7777
    volumes:
      - ./config:/config
    restart: unless-stopped
    stdin_open: true
    tty: true
JustinGrote commented 4 years ago

The world definitely saves periodically, and as far as I can tell as long as I do a "soft" shutdown of the container it saves the world before exit.

hskrtich commented 4 years ago

The container is just a wrapper for the server code itself. So how ever the server handles a shutdown is how that works. I assume it will attempt to send a normal shutdown command and it should only issue a kill command if the process doesn't close in a certin amount of time.

hskrtich commented 4 years ago

After some testing I dont think ether Vanilla or Tshock will do a save before closing. I will re-open this and see if I can add it to the startup script. Tho It may take a while.

bdr99 commented 4 years ago

Thanks for looking into this! Based on my own testing, I agree. It doesn't seem to do a save before closing, it seems to just kill the process. It would be nice if we could send the "exit" command (which saves and exits the server) to the Terraria server process when the container is stopped.

AustinMcPhail commented 4 years ago

How is this looking? Came across this the other day and was super happy to see it being talked about. Was interested in trying my hand at it, but if it is already in the works or anything then I'll leave it to whomever.

lpeabody commented 4 years ago

Super subscribed! Appreciate the work on this.

hskrtich commented 4 years ago

@AustinMcPhail I have not had time to work on this feature, feel free to take a crack at it.

kevinkjt2000 commented 4 years ago

I was looking at this too, and think it should be possible to install screen into the container for running the server. There is an AUR package that uses screen as part of running as a systemd service https://aur.archlinux.org/cgit/aur.git/tree/terraria-server@.service?h=terraria-server

The relevant bits are:e

# Screen is used, because the terraria server binary crashes if called on its own.
ExecStart=/usr/bin/screen -DmS terraria-server-%i /usr/bin/terraria-server -config /opt/terraria-server/%i.conf
# Send a warning message to all players and the exit command through screen to let the program terminate itself gracefully
ExecStop=/usr/bin/screen -S terraria-server-%i -X stuff "say Server shutting down. Saving world.\015"
ExecStop=/usr/bin/screen -S terraria-server-%i -X stuff "exit\015"

I bet this could be adapted into a startup script for docker, so I am going to give this a try today.

Edit: I give up; all this pseudo TTY attach stuff is outside of my expertise.

Noutbuk commented 4 years ago

I finally settled with the following scripts: https://gist.github.com/Noutbuk/85d455a90fa68862c9437678f89d27d7 I don't know if this is the recommended way, but works well for a small server with some friends. Maybe they are useful for some of you as well.