From 552574672249351d93cb03c3255786f36f15bf4f Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Mon, 23 Mar 2020 21:29:52 +0100 Subject: [PATCH 1/2] Meta: Add docker support --- .gitignore | 3 +++ Dockerfile | 24 ++++++++++++++++++++++++ Makefile | 21 +++++++++++++++++++++ README.md | 17 +++++++++++++++-- pom.xml | 8 ++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 72b6c45..558cb0a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ target/ out/ *.iml +# Docker files +.docker + # Created by https://www.gitignore.io/api/eclipse,visualstudiocode # Edit at https://www.gitignore.io/?templates=eclipse,visualstudiocode diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0a06f60 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM openjdk:11-slim +LABEL "tjbot"="true" + +ARG USER_ID + +# The user id is injected at build time +RUN useradd --uid $USER_ID tjbot + +# Expose the config dir +VOLUME ["/home/tjbot/config"] + +# We should be able to place temp data in there +# Not *strictly* needed though +RUN chown -R tjbot:tjbot /home/tjbot + +USER tjbot + +WORKDIR /home/tjbot + +# Include backend files +COPY TjBot.jar /home/tjbot/TjBot.jar + +# Execute the server, needs the path to the configuration file passed as an argument +ENTRYPOINT ["java", "-jar", "/home/tjbot/TjBot.jar"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f3b342f --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +USER_ID ?= 1000 + +build: target/TjBot.jar + echo "Build done!" + +target/TjBot.jar: $(shell find -type f -iname '*.java' -or -iname '*sql') pom.xml + mvn package + +clean: + rm -rf .docker + mvn clean + +.PHONY: clean + +.docker: + mkdir .docker + +build-docker: build .docker + cp target/TjBot.jar .docker + cp Dockerfile .docker + (cd .docker && sudo docker build -t tjbot --build-arg USER_ID=$(USER_ID) .) diff --git a/README.md b/README.md index cef4d53..af5137c 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,25 @@ ## About The intention of this bot is to make moderation easier and provide useful commands to manage the server. +## Contributing +Before you contribute to this Repository consider taking a look at the [CONTRIBUTING.md](https://github.com/Together-Java/TjBot/blob/master/CONTRIBUTING.md). + ## Running the bot 1. Copy `src/main/resources/sample-config.yml` and use it to structure your bot config. 2. Start the bot with following arguments: `-c path/to/your/config` -## Contributing -Before you contribute to this Repository consider taking a look at the [CONTRIBUTING.md](https://github.com/Together-Java/TjBot/blob/master/CONTRIBUTING.md). +## Running the bot in docker +### On Linux +1. Run `make build-docker USER_ID=` to build the image +2. Run the docker image. The `WORKDIR` of the image is `/home/tjbot/`. + ```sh + sudo docker run \ # Run it + --rm \ # Delete it on exit + -it \ # Wire up std in and out (for testing) + -v "your config dir:/home/tjbot/config" \ # Mount in your config dir + tjbot \ # Name of the image + -c config/sample-config.yml \ # See 'Running the bot', Step 2. + ``` ## Notice Please keep your private bot token secret. **Do not include your bot token in any committed or pushed file.** diff --git a/pom.xml b/pom.xml index 75393ef..4266043 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,14 @@ ${project.name} + + + + org.togetherjava.discordbot.BotMain + + + From 71935ed7395e94771d9b3f3e4c7ca1a523004621 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Mon, 23 Mar 2020 21:32:57 +0100 Subject: [PATCH 2/2] docs: Explain need for UID in Dockerfile --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index af5137c..dc6cd5a 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,9 @@ Before you contribute to this Repository consider taking a look at the [CONTRIBU ## Running the bot in docker ### On Linux -1. Run `make build-docker USER_ID=` to build the image +1. Run `make build-docker USER_ID=` to build the image. + The user id is needed, as the container does not run as root and needs to read the + config file you mount in. 2. Run the docker image. The `WORKDIR` of the image is `/home/tjbot/`. ```sh sudo docker run \ # Run it