diff options
-rw-r--r-- | pydis_site/apps/content/resources/guides/python-guides/docker-hosting-guide.md | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/pydis_site/apps/content/resources/guides/python-guides/docker-hosting-guide.md b/pydis_site/apps/content/resources/guides/python-guides/docker-hosting-guide.md index a42d11c1..5fb55caf 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/docker-hosting-guide.md +++ b/pydis_site/apps/content/resources/guides/python-guides/docker-hosting-guide.md @@ -54,14 +54,16 @@ $ sudo sh get-docker.sh To tell Docker what it has to do to run the application, we need to create a file named `Dockerfile` in our project's root. -1. First we need to specify the *base image*, which is the OS that the docker container will be running. Doing that will make Docker install some apps we need to run our bot, for +1. First we need to specify the *base image*, which is the OS that the docker container will be running. Doing that will + make Docker install some apps we need to run our bot, for example the Python interpreter ```dockerfile FROM python:3.10-bullseye ``` -2. Next, we need to copy our Python project's external dependencies to some directory *inside the container*. Let's call it `/app` +2. Next, we need to copy our Python project's external dependencies to some directory *inside the container*. Let's call + it `/app` ```dockerfile COPY requirements.txt /app/ @@ -102,7 +104,8 @@ Now update the project on your VPS and we can run the bot with Docker. $ docker build -t mybot . ``` -- the `-t` flag specifies a **tag** that will be assigned to the image. With it, we can easily run the image that the tag was assigned to. +- the `-t` flag specifies a **tag** that will be assigned to the image. With it, we can easily run the image that the + tag was assigned to. - the dot at the end is basically the path to search for Dockerfile. The dot means current directory (`./`) 2. Run the container @@ -111,10 +114,13 @@ $ docker build -t mybot . $ docker run -d --name mybot mybot:latest ``` -- `-d` flag tells Docker to run the container in detached mode, meaning it will run the container but will not give us any output from it. If we don't -provide it, the `run` will be giving us the output until the application exits. Discord bots aren't supposed to exit after certain time, so we do need this flag -- `--name` assigns a name to the container. By default, container is identified by id that is not human-readable. To conveniently refer to container when needed, -we can assign it a name +- `-d` flag tells Docker to run the container in detached mode, meaning it will run the container but will not give us + any output from it. If we don't + provide it, the `run` will be giving us the output until the application exits. Discord bots aren't supposed to exit + after certain time, so we do need this flag +- `--name` assigns a name to the container. By default, container is identified by id that is not human-readable. To + conveniently refer to container when needed, + we can assign it a name - `mybot:latest` means "latest version of `mybot` image" 3. Read bot logs (keep in mind that this utility only allows to read STDERR) @@ -123,7 +129,8 @@ we can assign it a name $ docker logs -f mybot ``` -- `-f` flag tells the docker to keep reading logs as they appear in container and is called "follow mode". To exit press `CTRL + C`. +- `-f` flag tells the docker to keep reading logs as they appear in container and is called "follow mode". To exit + press `CTRL + C`. If everything went successfully, your bot will go online and will keep running! @@ -140,8 +147,10 @@ services: container_name: mybot ``` -- `version` tells Docker what version of `docker-compose` to use. You may check all the versions [here](https://docs.docker.com/compose/compose-file/compose-versioning/) -- `services` contains services to build and run. Read more about services [here](https://docs.docker.com/compose/compose-file/#services-top-level-element) +- `version` tells Docker what version of `docker-compose` to use. You may check all the + versions [here](https://docs.docker.com/compose/compose-file/compose-versioning/) +- `services` contains services to build and run. Read more about + services [here](https://docs.docker.com/compose/compose-file/#services-top-level-element) - `main` is a service. We can call it whatever we would like to, not necessarily `main` - `build: .` is a path to search from Dockerfile, just like `docker build` command's dot - `container_name: mybot` is a container name to use for a bot, just like `docker run --name mybot` @@ -155,14 +164,21 @@ docker-compose up -d --build Now the docker will automatically build the image for you and run the container. ### Why docker-compose -The main purpose of `docker-compose` is mostly to allow running several images at once within one container. Mostly we don't need this in discord bots. + +The main purpose of `docker-compose` is mostly to allow running several images at once within one container. Mostly we +don't need this in discord bots. For us, it has the following benefits: + - we can build and run the container just with one command -- if we need to parse some environment variables or volumes (more about them further in tutorial) our run command would look like this +- if we need to parse some environment variables or volumes (more about them further in tutorial) our run command would + look like this + ```shell $ docker run -d --name mybot -e TOKEN=... -e WEATHER_API_APIKEY=... -e SOME_USEFUL_ENVIRONMENT_VARIABLE=... --net=host -v /home/exenifix/bot-data/data:/app/data -v /home/exenifix/bot-data/images:/app/data/images ``` -This is pretty long and unreadable. `docker-compose` allows us to transfer those flags into single config file and still use just one short command to run the container. + +This is pretty long and unreadable. `docker-compose` allows us to transfer those flags into single config file and still +use just one short command to run the container. ## Creating Volumes @@ -191,7 +207,8 @@ services: - /home/exenifix/mybot-data:/app/data ``` -The path before the colon `:` is the directory *on server's drive, outside of container*, and the second path is the directory *inside of container*. +The path before the colon `:` is the directory *on server's drive, outside of container*, and the second path is the +directory *inside of container*. All the files saved in container in that directory will be saved on drive's directory as well and Docker will be accessing them *from drive*. |