Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run container as terraria user #34

Merged
merged 7 commits into from
May 20, 2020
Merged

Conversation

mattlorimor
Copy link
Contributor

@mattlorimor mattlorimor commented May 19, 2020

This potentially closes #17.

I'm not sure if this is how you would like to accomplish this (so I'm creating this as a draft), but there doesn't appear to be any reason to run the container as root at all. It's possible to avoid even having to specify -u [user] when doing a docker run and simply use a non-root user by default.

This PR:

  • adds a terraria user and group
  • chowns the necessary directories
  • does a USER terraria at the end of the Dockerfile

QA

I've built this locally, and the container:

  • is able to create a new world
  • is able to start the created world
  • is able to save the world
  • is able to autocreate a world
  • is able to load an already-created world via the docker run command line -world arg

Dockerfile Outdated Show resolved Hide resolved
@mattlorimor mattlorimor marked this pull request as ready for review May 19, 2020 22:18
@Chaoschaot232
Copy link

Hey @mattlorimor
I like this change but would it be possible to also add uID and gID like e. g. TS3 Docker Image did it (9987)?

Because when I add a volume linked to the /home path and upload the file, it is done as root user and I am not sure if things then go well if the file isn't changed to the correct uID/gID outside the container, e. g. for write access or doesn't this matter?

@ryansheehan ryansheehan merged commit 73b514e into ryansheehan:master May 20, 2020
@ryansheehan
Copy link
Owner

Ugh, so I had merged this, but had to make a commit to undo it. When I was testing, I was getting a lot of crashing due to tshock failing permissions to write to a volume. After some googling, I think it is related to moby/moby#2259 (comment).

No matter what I did to change the permissions on directories that are volumed, they are stuck at root. My linux-foo isn't the strongest though so maybe you guys have a better idea?

@mattlorimor
Copy link
Contributor Author

@ryansheehan - What did the docker run command you were using look like? I'm going to try to replicate this on my end.

@ryansheehan
Copy link
Owner

ryansheehan commented May 20, 2020

docker run --rm -it -p 7777:7777 -v $HOME/Projects/personal/terraria/test/world:/world -v $HOME/Projects/personal/terraria/test/ServerPlugins:/plugins terraria

I dropped a text file into the ServerPlugins directory to test the copy

@mattlorimor
Copy link
Contributor Author

mattlorimor commented May 20, 2020

@ryansheehan

Oddly, I'm not having any issues creating or saving a world when doing this on Windows or my macOS machine.

Additionally, the permissions don't behave exactly the same across my Windows and macOS tests.

ls -alh / on Windows:
image

ls -alh / on macOS:
image

Dummy text file gets copied in properly:
image

Can write to the /world directory:
image

I have no idea why the VOLUME results in differing permissions between my two systems.

I must be missing something simple, here.

These tests I'm doing are on the following versions of Docker desktop...

Windows:
image

macOS:
image

@mattlorimor
Copy link
Contributor Author

The same permissions discrepancies can be seen with a small, test Dockerfile:

FROM alpine
RUN mkdir /test && \
    chown -R nobody:nobody /test
USER nobody
VOLUME ['/test']

@Chaoschaot232
Copy link

Maybe this could help out: https://github.com/TeamSpeak-Systems/teamspeak-linux-docker-images/blob/master/alpine/Dockerfile#L4-L7 - I have no problems with my TS3-Setup

And this one too: https://github.com/TeamSpeak-Systems/teamspeak-linux-docker-images/blob/master/alpine/entrypoint.sh#L4-L8

Hope, that I could help out a bit :)

@mattlorimor
Copy link
Contributor Author

mattlorimor commented May 20, 2020

Thanks for that, @Chaoschaot232.

It should be easy enough to change the groupadd and useradd to force uid and gid through like this:

RUN groupadd -r -g 999 terraria && \
    useradd -m -r -u 999 -g terraria terraria

And modify all the chowns to 999:999 (or whatever uid/gid).

But I think the issue @ryansheehan is encountering is a little different. I'm trying to understand why it sounds like the container process couldn't even write to the volumes when I'm not seeing the same thing. My run command should be functionally the same:

docker run --name terraria --rm -it -p 7777:7777 -v C:/.terraria2/world:/world -v C:/.terraria2/ServerPlugins:/plugins test

@mattlorimor
Copy link
Contributor Author

mattlorimor commented May 20, 2020

I think, at this point, I'd need to see what Ryan's error output looks like.

And what Docker version they're using and on what system (Windows? macOS? Ubuntu?).

@mattlorimor
Copy link
Contributor Author

mattlorimor commented May 21, 2020

If you get a chance, see if this NonRoot branch of mine does what you want: https://github.com/mattlorimor/terraria-1/tree/NonRoot.

It creates the terraria user in the Dockerfile and runs the Dockerfile as root, but executes the TerrariaServer.exe as the terraria user instead of the privileged root user.

The uid and gid default to 7777, but can be configured by passing UID and GID into the container as environment variables when running it.

@ryansheehan
Copy link
Owner

I wish I could move this into a new PR. I'm struggling to keep up with all the TShock updates and issues being posted. Oh and Terraria itself released a 1.4.0.3 update! I'll try and take a look at this soon. When I was last trying, on linux, all volumes connected as root, and no amount of chown/chmod was getting me around the permission errors that TerrariaServer.exe was blowing up on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ReOpen] Enable Docker image to run TShock server as a non-root user
3 participants