Home Creators Posts Import Register Favorites Logout
haven't archived this post yet. have a subscription? use the importer!

Content

Hi all, 

Here's a post that contains the answer of a good a Patron asked (thanks Isaac!).

How do you run multiple Docker bot instances with the same Docker file.

Actually I run Docker compose for this since it allows to use a specific docker-compose.yml file to be configured with everything the bot environment needs and then simply run it as a service in the background with one command. 

Btw. For my setup I assume the following

You are running a Linux system here. It should also work with Windows but I have not tested this. 

I also assume that you already have some experience with running freqtrade with docker. If not, you can always follow their procedure on: https://www.freqtrade.io/en/stable/docker_quickstart/  

You already have a config.json file and a strategy.py file that are both tested and working. In the config the web environment is enabled in my case.

Finally you also have installed Docker and Docker-compose on your system. 

My exact setup is as follows:

I have a directory called /opt/freqtrade. You can make this by doing:

cd /opt

mkdir freqtrade

cd freqtrade

Then create a docker-compose.yml file with your favorite text editor and add the following to the file:

docker-compose.yml file

---
version: '3'

services:

  bot1:

    image: freqtradeorg/freqtrade:stable

    restart: always

    container_name: ft_bot1

    volumes:

      - "/opt/freqtrade/user_data:/freqtrade/user_data"

    ports:

      - "0.0.0.0:8181:8080"

    environment:

      - TZ=Europe/Amsterdam

    command: >

      trade

      --logfile /freqtrade/user_data/logs/ft_bot1.log

      --db-url sqlite:////freqtrade/user_data/ft_bot1.sqlite

      --config /freqtrade/user_data/config_bot1.json

      --strategy skz_f

  bot2:

    image: freqtradeorg/freqtrade:stable

    restart: always

    container_name: ft_bot2

    volumes:

      - "/opt/freqtrade/user_data:/freqtrade/user_data"

    ports:

      - "0.0.0.0:8282:8080"

    environment:

      - TZ=Europe/Amsterdam

    command: >

      trade

      --logfile /freqtrade/user_data/logs/ft_bot2.log

      --db-url sqlite:////freqtrade/user_data/ft_bot2.sqlite

      --config /freqtrade/user_data/config_bot2.json

      --strategy ClucMay72018

Explanation

Now that do these lines mean:

--- version: '3' # Version of the docker-compose file format

services: # The services section where you define all docker running containers

bot1: # Name of the service

image: freqtradeorg/freqtrade:stable # The docker image that you are going to use for this container

restart: always # Determining the restart after a reboot or errors 

container_name: ft_bot1 # The container name for this instance

volumes: # here you determine the location where docker should keep its persistend data. NOT DOING THIS WILL LEAD TO LOSS OF DATA AFTER A RELOAD

- "/opt/freqtrade/user_data:/freqtrade/user_data" # Local data location (in my case absolute): container data location

ports: # when running the freqtrade website determine here port forwarding

- "0.0.0.0:8181:8080" # 0.0.0.0 = reachable from everywhere! 8181 = remote port 8080 = interna; docker container port. This way you dont have to change port numbers in the config.json file

environment: # environment variables

- TZ=Europe/Amsterdam # My timezone

command: > # this is the actual command that docker will use to start the bot in the container

trade # We are going to trade!

--logfile /freqtrade/user_data/logs/ft_bot1.log # an external logfile that I can monitor for bot action

--db-url sqlite:////freqtrade/user_data/ft_bot1.sqlite # the database name that is going to be used

--config /freqtrade/user_data/config_bot1.json # The config that I will use for this bot instance

--strategy skz_f # The strategy that I am using in the bot instance

You see that these last command lines are very similar to starting a bot without Docker. The difference here is that the location of all files seem to be located internally in the bot image, but because of the volume section above, their actually located in a local and persistent data directory.

Adding your configs and strategies

If you have working configs and strategy files. You will have to add them to the correct directories. Assuming that you did not start the bot with docker-compose or docker yet, you will still have to add these to the correct locations.

Therefore you will have to check the following before actually starting the bot(s):

Is there already a user_data directory in the /opt/freqtrade directory? If not, make it since docker-compose will look for a config file over there (according to my compose file above...).

mkdir user_data

It should now be: /opt/freqtrade/user_data

Move your config.json file over there and change the docker-compose file accordingly.

Is there a strategies directory in your /opt/freqtrade/user_data/ directory?

If not create that one as well with:

mkdir strategies

It should now be: /opt/freqtrade/user_data/strategies

And then add your strategy to that directory and change the docker-compose file accordingly.

Starting the bots

To start the bots, you should be in the same directory as where the docker-compose.yml file resides!

Start this file and see its output to the screen, use the command:

docker-compose up

If you haven't already downloaded the image, this will be the first time you will see it downloading. After this download, the bot instances will start and show the output to the screen.

If everything goes well, then you already could see some trading action on your screen. If not, then investigate the output on your screen carefully to see where things go wrong. It could be that the configuration files or strategy file is not seen. Check the directories to see if everything os configured correctly

Now it is time to start the docker images in the background with daemon mode. 

Stop the bots with CTRL+C and start them start them again, but this time in the background:

docker-compose up -d

The bots will start in the background and you can check the output of these by looking in their respective logs like this:

tail -f /user_data/logs/ft_bot1.log

If everything goes well you will see the output of the bot instance to this log. If you have errors, again check what's happening and fix these errors.

And always ALWAYS test this with the dry-run option enabled in the config json file of your bot!!

Last tips

I gave you my personal docker-compose file here with my strategies that I am currently testing. 

Additional settings are in the config.json file itself to keep things simple for me.

Your specific setup is not the same as mine so keep the correct datastructure and filenames in mind. Otherwise this will not work (correctly).

Also, do not make 10 bots in one single file but be aware of the capabilities of your system. Otherwise your system wil halt and you wil have to restart it (personal experience). Start with 2 bot instances and then slowly increase the amount of bots used. The system should still be responsive and capable of handling data quickly. 5 bots configured but none of them working swiftly has no use here.

I am trying out multiple different strategies here so that I diversify my portfolio. My older Freqtrade instruction videos on youtube also explain how you can set up your bots to use specific amounts/parts/percentages of your account balance to that bot's do not get in each others way and all think they have the complete account balance to their disposal. This could lead to very unpredictable behaviour.

Good luck with your BOT ARMY!

Files

Previews only

Comments

CryptoTee

Do you have a specific link or title for this? : "My older Freqtrade instruction videos on youtube also explain how you can set up your bots to use specific amounts/parts/percentages of your account balance"

Dan

how does this compare to running a container for each bot? is it better to rung multiple bots in the same container vs multiple containers? Also do you use default config.json and just change exchange/whitelist, pair lists, api server and a copy of the same config.json for every strategy or do you make changes for each strategy?

Dutch Algotrading

You can have different combinations here. For example you can configure the strategy and config though the docker-compose file but you can also use docker-compose and the ft config and in the ft-config point to a strategy. Also you can use different docker compose files for different bots, but then you will have to duplicate the data dirs. IN the case I'm describing here, you only need one FT directory and no duplication. Hope this helps...

Joan Vidal

I'm new on this world... I'm tryng to make multiple bots instance (2 at first). I have mounted docker-compose yml as you explain . Also both config.json 1,2 and strategies folder . I'm using vps aws , conected by putty and winscpp for handle directory. My question is about opt/freqtrade/ . I should have in this directory , user_data inside of this conf.json 1.2 ; strategyes; docker-compose .yml , sqlite 1 and two.. Som more folder? Where supost to be rest of freqtrade folders ? also i have other folders called Nningh and dhiparam to conect with protocols https.. I'm really thankful with you if could help on that matter in order to have rigth directory to run bots. Many thanks Joan

Dutch Algotrading

Little bit difficult to explain what to change since I do not really understand your setup. But if you watch my other video on how to build your own bot (the long video) then at least you can run one instance with docker and build more from that baseline.

Joan Vidal

hello , I have already done the entire installation following all your instructions. They start the containers but I get this error: from freqtrade.main import main ft_bot2 | ModuleNotFoundError: No module named 'freqtrade' I've checked everything and it seems fine. I have created my tree: opt/ freqtrade/docker-compose.yml /Dockerfile/user_data:config_1.json;config_b.json sqlite , and rest of folders..... i don't know where are mistakes. best

Joan Vidal

Ok I’ll sent later on my tree Linux operation I’ve system plus docker-compose.!

Joan Vidal

version: "2.4" services: freqtrade1: image: freqtradeorg/freqtrade:develop_plot restart: unless-stopped container_name: freqtrade1 volumes: - "./user_data:/freqtrade/user_data" ports: - "0.0.0.0:8080:8080" command: > trade --logfile /freqtrade/user_data/logs/freqtrade1.log --db-url sqlite:////freqtrade/user_data/tradesv3_freqtrade1.sqlite --config /freqtrade/user_data/configfreqtrade1.json --strategy CruceMaGold freqtrade2: image: freqtradeorg/freqtrade:develop_plot restart: unless-stopped container_name: freqtrade2 volumes: - "./user_data:/freqtrade/user_data" ports: - "0.0.0.0:8083:8080" command: > trade --logfile /freqtrade/user_data/logs/freqtrade2.log --db-url sqlite:////freqtrade/user_data/tradesv3_freqtrade2.sqlite --config /freqtrade/user_data/configfreqtrade2.json --strategy CruceMaGold networks: - freqtradebot

Joan Vidal

Then i have my folder on opt/Freqtrade/ft_userdata/ docker-compose yml /user_data/ freqtrade folders; freqtrade_1.config.json; freqtrade_2 config.json ; sqlite_1 ; sqlite_2 ... Thats all .

Dutch Algotrading

Ok, Try this. Create directory /opt/freqtrade. Go into that dir. Then create docker-compose.yml file (given below) to that directory. Then start to create the userdir (docker-compose run --rm freqtrade create-userdir --userdir user_data). but do all commands in that ft_userdata dir. It seems that the directlry locations are not quite correct. Also why version 2.4 ? In this video I showed how this could be done: https://youtu.be/1S9sGmlZaw8?si=ArPfAA4i6LslZdns&t=328 --- version: '3' services: freqtrade1: image: freqtradeorg/freqtrade:develop_plot restart: unless-stopped container_name: freqtrade1 volumes: - "/opt/freqtrade/user_data:/freqtrade/user_data" ports: - "0.0.0.0:8080:8080" command: > trade --logfile /freqtrade/user_data/logs/freqtrade1.log --db-url sqlite:////freqtrade/user_data/tradesv3_freqtrade1.sqlite --config /freqtrade/user_data/configfreqtrade1.json --strategy CruceMaGold freqtrade2: image: freqtradeorg/freqtrade:develop_plot restart: unless-stopped container_name: freqtrade2 volumes: - "/opt/freqtrade/user_data:/freqtrade/user_data" ports: - "0.0.0.0:8083:8080" command: > trade --logfile /freqtrade/user_data/logs/freqtrade2.log --db-url sqlite:////freqtrade/user_data/tradesv3_freqtrade2.sqlite --config /freqtrade/user_data/configfreqtrade2.json --strategy CruceMaGold networks: - freqtradebot

Joan Vidal

Hi again It doesn’t work .. seems that docker does not find config.json … I try many configuration and saw your video but .. could you share some drawing about directory folders ? Many thanks

Dutch Algotrading

I'll show you what i've got: I have a docker-compose file running in /opt/freqtrade In that same directory is a subdirectory called user_data (/opt/freqtrade/user_data) In that user_data directory I have all configs and other default freqtrade stuff In the /opt/freqtrade dir is a file called docker-compose.yml with the current configuration: --- version: '3' services: freqbot1: # image: freqtradeorg/freqtrade:stable image: freqtrade_custom restart: always container_name: freqbot1 mem_limit: 1g mem_reservation: 1g cpus: 1 volumes: - "/opt/freqtrade/user_data:/freqtrade/user_data" ports: - "0.0.0.0:8181:8080" environment: - TZ=Europe/Amsterdam # - PUID=0 # - PGID=0 command: > trade --logfile /freqtrade/user_data/bot1.log --db-url sqlite:////freqtrade/user_data/bot1.sqlite --config /freqtrade/user_data/bot1_config.json --strategy Obelisk_Ichimoku_ZEMA_v1

Joan Vidal

Someone has some example of conf. JSON for live run.. I get always same warning pairlist handle doesn’t exist.. I havet it in my conf but … I use Binance on Spot.

Dimitrios Gkamtzas

Thanks for the content, for me is Unclear if you are using in the configuration files CORS ORIGIN ? : }, "api_server": { "enabled": true, "listen_ip_address": "0.0.0.0", "listen_port": 8080, "verbosity": "error", "enable_openapi": false, "jwt_secret_key": "somethingrandom2", "CORS_origins": ["http://localhost:8080"], "username": "user", "password": "pwd"

Dimitrios Gkamtzas

It is unclear what are the settings in the 2 config files, specifically the adress of the "CORS_origins": ["http://localhost:8080"] Can you please provide the apis server setting of the 2 config files (without passwords)

Dutch Algotrading

localhost means that you will only approach the server from the same computer. If you want to use another computer to open the webpage, then you'll have to use the ip address of the server here. I added another 'product' to the shop page with the complete configuration (excluding the Freqtrade files) I use for the live streaming forward testing bot. You might want to investigate or use that for your own situation. Hope this helps.

Adam Iulian

Do I have to set up ports in aws instance security groups to?