Difference between revisions of "Docker"

From Bebot Wiki 2
Jump to navigationJump to search
Line 73: Line 73:
  
  
NOTE : if you already have a SQL server you can use it & skip to "Run" below. Otherwise you must run also a container for Database.
+
NOTE : if you already have a SQL server you can use it & skip to "DB prep" below. Otherwise you must also run another container for DataBase.
  
The principle is then nearly the same. First obtain the base files by doing :
+
The principle is therefore nearly the same. First obtain the base files by doing :
  
 
<pre>docker pull mariadb:latest</pre>
 
<pre>docker pull mariadb:latest</pre>

Revision as of 23:39, 8 December 2023

Docker

So using Docker can save you some installation hassle, but you'll need to have it 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/

Note : if you're NOT a PHP/SQL expert this will save you time but beware that understanding Docker also requires some practice of its own ...


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) & go in it to create these 2 text files :


"Dockerfile" (with no extension)

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/php8
USER bebot
WORKDIR /BeBot


"docker-entrypoint.sh"

#!/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/php8  StartBot.php "$@"
        EXITCODE=$?
        trap - TERM
done
exit $EXITCODE


Then in command line, you'll move into that folder to send the following command :

docker build -t bebot-imagename .

(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" + several blue lines starting with "=>".


NOTE : if you already have a SQL server you can use it & skip to "DB prep" below. Otherwise you must also run another container for DataBase.

The principle is therefore nearly the same. First obtain the base files by doing :

docker pull mariadb:latest

(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 :

docker image ls


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 & our SQL).

For the SQL there are 3 possibilites :

1: you have an external server ; make sure it's reachable (eg : ping its ip) from your current host, also check its facial port responds.

2: you have a localhost service on host ; so you usually will go for default ip 127.0.0.1 on usual port 3306.

3: you're about to run a container for that ; be sure to check what ip/port are set when you'll RUN it (below)


ATTENTION : in all 3 cases 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 documentation and adapt it to you context. But for generic example :

mysql (to enter prompt)

> CREATE DATABASE bebotdb;

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

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


Bot config
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.


For SQL (optional) :

docker run --name mariadb-containername -e MYSQL_ROOT_PASSWORD=mypass -p 33306:3306 -d mariadb --restart=always

(you can change "containername" for anything you like but then you'll replace it below accordingly)


For Bebot (mandatory) :

Windows :

docker run -d --rm --name bebot-containername --memory=128M -v C:\Folder\befolder\conf:\bebot\Conf -v C:\Folder\befolder\conf\log:\bebot\log -v C:\Folder\befolder\conf\Commodities:\bebot\Commodities bebot-buildname

(you must replace "buildname" and "C:\Folder\befolder" accordingly to your system ; "containername" is free to choose ; all path use antislashes \)

Mac/Linux :


(you must replace "buildname" and "/path/to/befolder" accordingly to your system ; "containername" is free to choose ; all path use slashes /)


Control

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

To show a list of running container(s) :

docker ps

To see realtime life of the Bebot :

docker logs -f <full-containername>

(Ctrl+c to exit) To enter a given container interactively :

docker exec -it <full-containername>

Once inside, you command line window becomes the bot itself ; so you can see what the bot does realtime (sent & received datas, etc).

NOTE : if it's bot's 1st run & you didn't edit Conf/ files earlier, it's time to fill all infos (account/pass, char, game server, mysql credentials, etc).

If the bot is set correctly it should go online in the game and become reachable/responsive as expected.

To exit the entered container :

Ctrl+p Ctrl+q

To stop a given container :

docker stop <full-containername>

(or Ctrl+c while entered in interactive mode upper)


Conclusion

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