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

Improvements and instructions on how to build Docker container #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ COPY package.json package-lock.json /opt/FactorioChatBot/
RUN cd /opt/FactorioChatBot
RUN npm install

# Mount the docker volume to the host
VOLUME /opt/FactorioChatBot
# Mount the docker volume for the config file to the host
VOLUME /opt/FactorioChatBot/config

# Bind the log file which could be created by another container.
VOLUME /opt/factorio/Factorio-server.log

# Run the bot
CMD ["node", "."]
CMD ["node", "."]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,12 @@ Follow the instructions in the output to schedule the service depending on your
`[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u defaultuser --hp /home/defaultuser`

# Running the bot using Docker
Currently you need to build the container yourself as there is no image on Docker Hub (yet)
1. `docker build -t factorio-chat-bot .`
2. `docker run --name FactorioChatBot --restart=always -d -v /path/to/config:/opt/FactorioChatBot/config -v /path/to/factorio/logfile:/opt/factorio/Factorio-server.log factorio-chat-bot `
3. `docker start FactorioChatBot`

Or run it interactive (once):
1. `docker run --rm -it FactorioChatBot -v /path/to/config:/opt/FactorioChatBot/config -v /path/to/factorio/logfile:/opt/factorio/Factorio-server.log`
1 change: 1 addition & 0 deletions config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.json
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ var chokidar = require("chokidar");

var { Rcon } = require("rcon-client");

var config = require("./config.json");
var config;
// Check if we have a local config file or a docker volume
try {
if (fs.existsSync("./config/config.json")) {
config = require("./config/config.json");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why you decided to make config a folder, isn't it fine to keep it in the root directory?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I can explain that:

Docker volumes are intended to bind to a folder (and not a single file). While it is possible to bind a single file this can be confusing. For example if you bind /host/a.txt to /container/a.txt and the file on the host does NOT exists then Docker wil l create a FOLDER called a.txt. Then it tries to start and you can get strange error messages. See for example the problems @ https://stackoverflow.com/questions/42248198/how-to-mount-a-single-file-in-a-volume

But technically it is possible it's just not best practice. I also thought about mounting the entire Factorio directory but that gives a bit more access then I'd like so I kept that to a single file.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, alright.. My own Dockerfile for my bot just binds the entire bot's folder to the docker volume.. Any reasons to not use a /src folder and bind the files to it as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that works for development yes. But someone wanting to run the bot does not have (or needs) the source files. He just downloads/uses a pre-built docker image and only binds the config. (Or use env vars)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I've never ran any docker images without using source files though. But having an entire config folder when the rest is in the root directory sounds weird to me. Guess it's time to move the files to /src and make a /src/config folder for it

} else {
console.log("Unable to find config file, see README.md");
return;
}
} catch (err) {
console.log(err)
return;
}


const bot = new Discord.Client({ intents: ['GUILD_MESSAGES', 'GUILDS'], allowedMentions: { users: [], roles: [] } });

Expand Down Expand Up @@ -205,4 +218,4 @@ async function rconCommand(command) {
} catch (error) {
throw Error(`RCON Error --- Details --- \nNAME: ${error.name} \nDESC: ${error.description}`);
}
}
}