Difference between revisions of "Docker"

From Bebot Wiki 2
Jump to navigationJump to search
Line 6: Line 6:
  
  
Generally, you could consider Docker is no less than amazing if you'd run only one (or very few) bot(s), same if you'd want a very quick discovery on Bebot's functions. In such case Docker's unbeatable on many aspects :
+
Generally, you could consider Docker is no less than amazing if you'd run only one (or very few) bot(s), same if you'd want a very quick discovery on Bebot's functions. In such case Docker is unbeatable on many aspects :
  
 
With very few actions from you, thanks to Docker's automations based on simple lightweight textfiles, you would configure then install and quickly run some sort of isolated "VMs" bringing your bot(s) and DataBase to life.
 
With very few actions from you, thanks to Docker's automations based on simple lightweight textfiles, you would configure then install and quickly run some sort of isolated "VMs" bringing your bot(s) and DataBase to life.
  
  
But beyond, as if you wanted to optimizely run multiple bots from an unique PHP-code installation with all pointing towards same Database or needing some proxy (eg Aocp), it'd possibly not be the best choice as each instance would then weight full Docker image(s)/container(s) in RAM/CPU.
+
But beyond, eg if you wanted to optimizely run multiple bots from an unique PHP-code installation with all pointing towards same Database or needing some proxy (eg Aocp), it'd possibly not be the best choice as each instance would then weight full Docker image(s)/container(s) in RAM/CPU.
  
 
For such more complicated cases we'd rather point you at the classical way explained with details within both Bebot README and http://wiki.bebot.link/index.php/Installation
 
For such more complicated cases we'd rather point you at the classical way explained with details within both Bebot README and http://wiki.bebot.link/index.php/Installation
Line 31: Line 31:
  
 
Just make sure you're properly installed then continue to next section.
 
Just make sure you're properly installed then continue to next section.
 
 
===== Build =====
 
 
Once the software is installed, you first have to build an image from 2 docker elements below.
 
 
Create a dedicated folder (let's name it "botfolder" for later reference) and go in it to create these 2 text files :
 
 
 
"Dockerfile" (with no extension)
 
 
<pre>
 
FROM alpine:latest
 
ENTRYPOINT ["/sbin/tini", "-g", "--"]
 
CMD ["/BeBot/docker-entrypoint.sh"]
 
RUN apk --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community/ add \
 
    git php82-cli php82-phar php82-curl php82-sockets php82-pdo php82-pdo_mysql \
 
    php82-mbstring php82-ctype php82-bcmath php82-json php82-posix php82-simplexml \
 
    php82-dom php82-pcntl php82-zip php82-opcache php82-fileinfo php82-mysqli sudo tini
 
RUN adduser -h /BeBot -s /bin/false -D -H bebot
 
RUN git clone https://github.com/bitnykk/BeBot.git
 
COPY docker-entrypoint.sh /BeBot
 
RUN chmod +x /BeBot/docker-entrypoint.sh
 
RUN chown -R bebot:bebot /BeBot
 
RUN chown -R bebot:bebot /BeBot/.git
 
RUN sudo ln -s /usr/bin/php82 /usr/bin/php
 
USER bebot
 
WORKDIR /BeBot
 
</pre>
 
 
 
"docker-entrypoint.sh"
 
 
<pre>
 
#!/bin/ash
 
# shellcheck shell=dash
 
errorMessage() {
 
        echo "$*"
 
        exit 1
 
}
 
EXITCODE=255
 
git pull
 
while [ "$EXITCODE" -eq 255 ]; do
 
        trap "" TERM
 
        # shellcheck disable=SC2086
 
        /usr/bin/php  StartBot.php "$@"
 
        EXITCODE=$?
 
        trap - TERM
 
done
 
exit $EXITCODE
 
</pre>
 
 
 
NB : if you want more details on the PHP modules and config, go at https://www.php.net/docs.php
 
 
 
Then in command line into that "botfolder" you'd send the following command :
 
 
<pre>docker build -t bebot-imagename .</pre>
 
 
(you can change "imagename" for anything you like but then you'll replace it below accordingly)
 
 
 
If all goes fine you should see something like "Building X.Ys (10/10) FINISHED" + <span style="color:#0000FF">several blue lines starting with "=>"</span>.
 
 
 
NOTE : if you already have a SQL server you can use it and skip to "DB prep" below. Otherwise you must also build another container for DataBase.
 
 
The principle is therefore nearly the same. First obtain the base files by doing :
 
 
<pre>docker pull mariadb:latest</pre>
 
 
(you also can choose an X.Y version you'd want instead of "latest" like 10.4 for example, or any other)
 
 
 
That once done you may see your created image(s) by doing :
 
 
<pre>docker image ls</pre>
 
 
(Note that you can also delete some image but it's undoable, so beware : docker image rm <full-imagename>)
 
  
  
 
===== DB prep =====
 
===== DB prep =====
  
Now we have to configure both our SQL (so it's opened to Bebot's requests) and Bebot (so it connects to game server and our SQL).
+
Now we have to determine how to manage our DataBase. For the SQL there are 3 possibilites :
 
 
For the SQL there are 3 possibilites :
 
  
1: you have an external server ; make sure it's reachable (eg : ping its ip) from your host, and check if its facial port is opened.
+
1: you already have an external server ; just make sure it's reachable (eg : ping its ip) from your host, and check if its facial port (usually 3306) is correctly opened.
  
2: you opted for a local service on host ; so you'd usually will go for default ip 127.0.0.1 on usual SQL port (3306).
+
2: you opted for a local service on host ; so you'd usually go for usual SQL port (3306) but instead of 127.0.0.1 default ip we'd advice you default docker0 network ip 172.17.0.1
  
3: you're about to run a container for that ; be sure to check what ip/port are set when you'll "RUN" it (section below).
+
3: you're about to run a container for that ; you can skip this part fully as we'll explain you how to set this up correctly later in "build" section, it's the simplest in fact !
  
  
Here some command or documentation to verify all elements :
+
Here some command or documentation to verify elements for cases 1/2 :
  
 
- IP/Network on Linux/Mac (try from a console : ip a) and Windows (also in console : ipconfig)
 
- IP/Network on Linux/Mac (try from a console : ip a) and Windows (also in console : ipconfig)
  
 
- Ports/Services on Linux/Mac https://vitux.com/find-open-ports-on-debian and Windows https://www.geeksforgeeks.org/how-to-check-open-tcp-ip-ports-in-windows
 
- Ports/Services on Linux/Mac https://vitux.com/find-open-ports-on-debian and Windows https://www.geeksforgeeks.org/how-to-check-open-tcp-ip-ports-in-windows
 +
 +
- also find SQL config (usually .cnf) file(s) and check that your parameter "bind-address" is correct, otherwise change it and restart service
  
  
! ATTENTION ! in all 3 upper cases you will have to :
+
! ATTENTION ! in upper cases 1/2 you will have to :
  
 
- CREATE a DATABASE dedicated for the bot (you can name it as you like but remember it)
 
- CREATE a DATABASE dedicated for the bot (you can name it as you like but remember it)
Line 149: Line 69:
 
mysql (enters prompt, optional parameters are --user --password + for distant server --host)
 
mysql (enters prompt, optional parameters are --user --password + for distant server --host)
  
> CREATE DATABASE bebotdb;
+
> CREATE DATABASE bebotdbname;
  
> CREATE USER 'bebotuzr'@'%' IDENTIFIED BY 'mypass';
+
> CREATE USER 'bebotuzr'@'%' IDENTIFIED BY 'botpass';
  
 
> GRANT ALL PRIVILEGES ON bebotdb.* TO 'bebotuzr'@'%';
 
> GRANT ALL PRIVILEGES ON bebotdb.* TO 'bebotuzr'@'%';
Line 165: Line 85:
  
  
Also find SQL config (usually .cnf) file(s) and check parameter "bind-address" is concordant.
+
If all here is done (or if you're in upper case 3) let's move on forward.
  
  
===== Bot config =====
+
===== Build =====
  
The same way we've prepared our DB for Bebot, we also need to make our Bebot able to connect to the game + to the DB.
+
Once the software is installed, you first have to build runnable images from Bebot Docker preset configs :
  
Also, beware Docker works in a way that makes the bot wipes all its files at every new run (!) so we need to render some persistents.
+
Get them from https://github.com/bitnykk/DockerBeBot/ (download the .zip or use git clone)
  
For that reason we will extract some files within the image we built earlier, and save them at host level to protect them.
 
  
To do this, we will run this long line on Linux from our "botfolder" :
+
Then in command line, enter wanted folder ("botonly" in upper cases 1/2, "botplusdb" in upper case 3).
  
<pre>
+
You could edit Dockerfile and change PHP version for whatever you'd like ; more details on the modules and config at https://www.php.net/docs.php
docker run --rm --entrypoint tar bebot-buildtest czf - Conf Custom Extras/Bank Extras/Scripts log Text > out.tar.gz && tar xzf out.tar.gz -C . && rm out.tar.gz && chmod 777 * -R && mv Conf/Bot.conf.dist Conf/Bot.conf && mv Conf/Mysql.conf.dist Conf/Mysql.conf
 
</pre>
 
  
On Windows a part of it won't work at all, so we will only obtain compress datas into our "botfolder" :
+
In upper case 3, you'd also have to setup your SQL credentials within docker-compose.yml (4 values to modify under "environment" part).
  
<pre>
 
docker run --rm --entrypoint tar bebot-buildtest czf - Conf Custom Extras/Bank Extras/Scripts log Text > out.tar.gz
 
</pre>
 
  
(then we can use any Windows utility, as Winzip or 7-zip etc, to decompress these datas right in place)
+
Once everything seems correct according to your needs, just do :
  
 +
<pre>docker compose build </pre>
  
In any case we end up with several new folders ("Conf", "Custom", "Extras", "log" and "Text") in our "botfolder".
+
If all goes fine you should see stuff as "Building X.Ys (10/10) FINISHED" + <span style="color:#0000FF">several blue lines starting with "=>"</span>.
  
Windows only : set permissive rights on all those folders/files (select all, right-click > Properties > Security) so our container can acces them.
 
  
 +
When it's over you are almost ready for the most exciting part ... but we'll need few more actions first !
  
We now must enter "Conf" folder to edit 2 files for runnability ; leave all quotes/double-quotes as they are, write your values inside of them.
 
  
Windows only : we also need to rename 2 files as Bot.conf and Mysql.conf (remove .dist in names).
+
===== Bot config =====
  
 +
Beware Docker works in a way that makes the bot could wipes some files at reload so we need to render some persistents.
  
In Bot.conf => set your bot account name, pass, char, server, guild if AoC, owner and possibly AO guild (true+number, otherwise false+00000001).
+
For that reason we will extract some files within the image we built earlier, and save them at host level to protect them.
  
In Mysql.conf => set you bot database, user, pass and server (possibly server:port) ; all depends on your SQL situation as seen earlier ...
+
To do this, we will now run this long line on Linux from our chosen folder :
  
 +
<pre>
 +
docker run --rm --entrypoint tar bebot-image czf - Conf Custom Extras/Bank Extras/Scripts log Text > out.tar.gz && tar xzf out.tar.gz -C . && rm out.tar.gz && chown 1000:1000 * -R
 +
</pre>
  
===== Run =====
+
On Windows a part of this shouldn't work at all, so we will only obtain compressed datas into our chosen folder :
  
We're now ready to run our container(s) : 1 only if we want Bebot with our usual SQL server, otherwise 2 for Bebot + SQL both as Docker containers.
+
<pre>
 +
docker run --rm --entrypoint tar bebot-image czf - Conf Custom Extras/Bank Extras/Scripts log Text > out.tar.gz
 +
</pre>
  
 +
(then we can use any Windows utility, as Winzip or 7-zip etc, to decompress these datas right in place)
  
For SQL (optional) :
 
  
<pre>docker run --name mariadb-containername -e MYSQL_ROOT_PASSWORD=mypass -p 3306:3306 -d mariadb-imagename --restart=always</pre>
+
In any case we end up with several new folders ("Conf", "Custom", "Extras", "log" and "Text") in chosen folder.
  
(you can change "containername" for anything you like but remember it ; "imagename" must be an existing one created upper)
+
Windows only : possibly set permissive rights on those folders/files (select all, right-click > Properties > Security) so our container acces them.
  
  
For Bebot (mandatory) :
+
===== Run =====
  
Windows :
+
We're now ready to run our container(s) : 1 only if we want Bebot with our usual SQL server, otherwise 2 for Bebot + SQL both as Docker containers.
<pre>
 
docker run -d --rm --name bebot-containername --memory=128M -v C:\Folder\botfolder\Conf:/BeBot/Conf -v C:\Folder\botfolder\Custom:/BeBot/Custom -v C:\Folder\botfolder\Extras\Bank:/BeBot/Extras/Bank -v C:\Folder\botfolder\Extras\Scripts:/BeBot/Extras/Scripts -v C:\Folder\botfolder\log:/BeBot/log -v C:\Folder\botfolder\Text:/BeBot/Text bebot-imagename
 
</pre>
 
 
 
(replace "imagename" and "C:\Folder\botfolder" depending on your config ; "containername" is free to choose ; all host pathes before ":" use antislashes \)
 
  
Mac/Linux :
 
 
<pre>
 
<pre>
docker run -d --rm --name bebot-containername --memory=50M -v /path/to/botfolder/Conf:/BeBot/Conf -v /path/to/botfolder/Custom:/BeBot/Custom -v /path/to/botfolder/Extras/Bank:/BeBot/Extras/Bank -v /path/to/botfolder/Extras/Scripts:/BeBot/Extras/Scripts -v /path/to/botfolder/log:/BeBot/log -v /path/to/botfolder/Text:/BeBot/Text bebot-imagename
+
docker compose run bebot
 
</pre>
 
</pre>
  
(same than up but all path use slashes / ; also if you have SQL as host local service add this setting before bebot-imagename : --network="host")
+
Your bot should now load (after launching its DB) and, as it's our first run, ask to fill up credentials informations.
  
 +
So give him account name, pass, character, server and also owner and superadmin(s) plus few more question.
  
Your bot should now load and go online to respond commands - if you did it all well ^_^
+
Then you're asked for the SQL part. In upper cases 1/2 you should already have everything needed here.
 
+
For case 3 you are just missing one information (SQL server ip) obtainable by 2 ways :
Otherwise you can use this shape of command to troubleshoot better :
+
- either you open a second console in command line (if you can do this)
 
+
- or you do Ctrl+p then Ctlr+q to detach off container view and be back to prompt
 +
In both cases you will do these 2 commands in order :
 +
<pre>
 +
docker ps
 +
</pre> (to see bebotdb's name or id, any if fine as both can be used below)
 
<pre>
 
<pre>
docker run --name bebot-containername (+ all parameters you need as -v and possibly --network etc ...) bebot-imagename
+
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' bebotdb_name_or_id
 
</pre>
 
</pre>
 +
Once you got that, you can go back into bot's view (first console if you have 2, or use "docker attach bebot" command if you detached).
 +
  
So you'll see what bot does realtime in your console. Then do Ctlr+c to exit (but that will shutdown the bot container).
+
After finishing up its setting, the bot will start and create all his DB entries, then come online !
  
  

Revision as of 23:59, 15 December 2023


Preamble

So using Docker can save you some installation hassle, especially if you're NOT a PHP/SQL expert it will save you huge time ... but beware that understanding Docker also requires some practice of its own !


Generally, you could consider Docker is no less than amazing if you'd run only one (or very few) bot(s), same if you'd want a very quick discovery on Bebot's functions. In such case Docker is unbeatable on many aspects :

With very few actions from you, thanks to Docker's automations based on simple lightweight textfiles, you would configure then install and quickly run some sort of isolated "VMs" bringing your bot(s) and DataBase to life.


But beyond, eg if you wanted to optimizely run multiple bots from an unique PHP-code installation with all pointing towards same Database or needing some proxy (eg Aocp), it'd possibly not be the best choice as each instance would then weight full Docker image(s)/container(s) in RAM/CPU.

For such more complicated cases we'd rather point you at the classical way explained with details within both Bebot README and http://wiki.bebot.link/index.php/Installation


Now you're aware of this all, and if you feel ready for it, let's proceed into Docker below.


Install

Before anything else, you'll need to have Docker installed properly - depending on system hosting your bot(s) :

Windows => https://docs.docker.com/desktop/install/windows-install/

Linux => https://docs.docker.com/desktop/install/linux-install/

Mac => https://docs.docker.com/desktop/install/mac-install/


Just make sure you're properly installed then continue to next section.


DB prep

Now we have to determine how to manage our DataBase. For the SQL there are 3 possibilites :

1: you already have an external server ; just make sure it's reachable (eg : ping its ip) from your host, and check if its facial port (usually 3306) is correctly opened.

2: you opted for a local service on host ; so you'd usually go for usual SQL port (3306) but instead of 127.0.0.1 default ip we'd advice you default docker0 network ip 172.17.0.1

3: you're about to run a container for that ; you can skip this part fully as we'll explain you how to set this up correctly later in "build" section, it's the simplest in fact !


Here some command or documentation to verify elements for cases 1/2 :

- IP/Network on Linux/Mac (try from a console : ip a) and Windows (also in console : ipconfig)

- Ports/Services on Linux/Mac https://vitux.com/find-open-ports-on-debian and Windows https://www.geeksforgeeks.org/how-to-check-open-tcp-ip-ports-in-windows

- also find SQL config (usually .cnf) file(s) and check that your parameter "bind-address" is correct, otherwise change it and restart service


! ATTENTION ! in upper cases 1/2 you will have to :

- CREATE a DATABASE dedicated for the bot (you can name it as you like but remember it)

- make an USER with PASSWORD and expected source (eg : @'%' to accept external requests)

- also GRANT that USER all PRIVILEGES on the DATABASE you created upper


If needed, you may have to read some SQL doc, as https://dev.mysql.com/doc/ and adapt it to you context.


But for generic example it will usually look like the following :

mysql (enters prompt, optional parameters are --user --password + for distant server --host)

> CREATE DATABASE bebotdbname;

> CREATE USER 'bebotuzr'@'%' IDENTIFIED BY 'botpass';

> GRANT ALL PRIVILEGES ON bebotdb.* TO 'bebotuzr'@'%';


If you ain't sure, you can verify you work is fine by doing :

> SHOW DATABASES; (should show the DB you created upper among list)

> SELECT User, Host FROM mysql.user; (should show your upper user among list)

> SHOW GRANTS FOR 'bebotuzr'@'%'; (should show the upper GRANT among list)


If all here is done (or if you're in upper case 3) let's move on forward.


Build

Once the software is installed, you first have to build runnable images from Bebot Docker preset configs :

Get them from https://github.com/bitnykk/DockerBeBot/ (download the .zip or use git clone)


Then in command line, enter wanted folder ("botonly" in upper cases 1/2, "botplusdb" in upper case 3).

You could edit Dockerfile and change PHP version for whatever you'd like ; more details on the modules and config at https://www.php.net/docs.php

In upper case 3, you'd also have to setup your SQL credentials within docker-compose.yml (4 values to modify under "environment" part).


Once everything seems correct according to your needs, just do :

docker compose build 

If all goes fine you should see stuff as "Building X.Ys (10/10) FINISHED" + several blue lines starting with "=>".


When it's over you are almost ready for the most exciting part ... but we'll need few more actions first !


Bot config

Beware Docker works in a way that makes the bot could wipes some files at reload so we need to render some persistents.

For that reason we will extract some files within the image we built earlier, and save them at host level to protect them.

To do this, we will now run this long line on Linux from our chosen folder :

docker run --rm --entrypoint tar bebot-image czf - Conf Custom Extras/Bank Extras/Scripts log Text > out.tar.gz && tar xzf out.tar.gz -C . && rm out.tar.gz && chown 1000:1000 * -R

On Windows a part of this shouldn't work at all, so we will only obtain compressed datas into our chosen folder :

docker run --rm --entrypoint tar bebot-image czf - Conf Custom Extras/Bank Extras/Scripts log Text > out.tar.gz

(then we can use any Windows utility, as Winzip or 7-zip etc, to decompress these datas right in place)


In any case we end up with several new folders ("Conf", "Custom", "Extras", "log" and "Text") in chosen folder.

Windows only : possibly set permissive rights on those folders/files (select all, right-click > Properties > Security) so our container acces them.


Run

We're now ready to run our container(s) : 1 only if we want Bebot with our usual SQL server, otherwise 2 for Bebot + SQL both as Docker containers.

docker compose run bebot

Your bot should now load (after launching its DB) and, as it's our first run, ask to fill up credentials informations.

So give him account name, pass, character, server and also owner and superadmin(s) plus few more question.

Then you're asked for the SQL part. In upper cases 1/2 you should already have everything needed here. For case 3 you are just missing one information (SQL server ip) obtainable by 2 ways : - either you open a second console in command line (if you can do this) - or you do Ctrl+p then Ctlr+q to detach off container view and be back to prompt In both cases you will do these 2 commands in order :

docker ps

(to see bebotdb's name or id, any if fine as both can be used below)

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' bebotdb_name_or_id

Once you got that, you can go back into bot's view (first console if you have 2, or use "docker attach bebot" command if you detached).


After finishing up its setting, the bot will start and create all his DB entries, then come online !


Control

Now to check if your container(s) run properly, and to control things, you have a set of commands.

To show a list of running container(s) : docker ps (can use -a parameter to also see stopped ones)

To see realtime log of the Bebot : docker logs -f <full-containername> (Ctrl+c to exit which will NOT shutdown bot/container)

To enter a given container interactively : docker exec -it <full-containername> sh (Ctrl+p Ctrl+q to exit without shutting down neither)

To stop a given container : docker stop <full-containername> (bot should go offline as expected)

To start again a previously stopped container : docker start <full-containername> (otherwise daemon may throw run error of "already in use")

To delete a buggy container : docker rm -f <full-containername> (means full data loss, not undoable, so beware !)


If Bebot git code was patched, you simply restart container and it should auto-update : docker restart <full-containername>

Alternative manual way : enter the container, do "git pull" then exit and !restart the bot from ingame, which should provide same result.


You may setup some task(s) (crontab on Linux/Mac / Task Manager > Startup on Windows) to run some container(s) right at host startup.

Sources : https://manpages.debian.org/bullseye/cron/crontab.5.en.html / https://www.howtogeek.com/208224/how-to-add-a-program-to-startup-in-windows/

Same way you should strongly save your SQL datas frequently as Docker won't do it for you ; can use task(s) based on mysqldump for that.

Source : https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html


Conclusion

This guide is a work in progress, kept as simple as possible, and will evolve as we find improvements or tricks.

For more details and options you can refer to Docker official documentation here https://docs.docker.com/