Installing and Running Freqtrade on Docker Swarm

Installing and managing Freqtrade, a cryptocurrency trading bot, on a Docker Swarm infrastructure might seem challenging at first. However, with the right configuration and a few tips, you can deploy a robust, scalable solution.

· 3 min read
Installing and Running Freqtrade on Docker Swarm

Installing and managing Freqtrade, a cryptocurrency trading bot, on a Docker Swarm infrastructure might seem challenging at first. However, with the right configuration and a few tips, you can deploy a robust, scalable solution. Here’s how I successfully installed and ran Freqtrade on my Docker Swarm cluster.

Why Docker Swarm for Freqtrade?

Docker Swarm provides native clustering and orchestration features, making it an excellent choice for running Freqtrade. With Swarm’s simplicity and scalability, you can manage services across multiple nodes efficiently. Additionally, leveraging Swarm’s deployment constraints ensures resources are optimally utilized.

Freqtrade
Freqtrade is a free and open source crypto trading bot written in Python, designed to support all major exchanges and be controlled via Telegram or builtin Web UI

Setting Up the Environment

Prerequisites

  • A Docker Swarm cluster with at least one manager and one worker node.
  • Access to an NFS (Network File System) share for persistent storage.
  • Basic knowledge of Docker Compose syntax and Freqtrade configuration.

NFS Setup

To ensure data persistence across nodes, an NFS share was used. Here’s how the volume was configured:

driver: local
driver_opts:
  device: :/path/to/nfs/share/freqtrade
  o: addr=your.nfs.server.ip,nolock,soft,rw,nfsvers=4
  type: nfs

Ensure the NFS server is properly configured and accessible from all nodes in your Swarm cluster.


Freqtrade Docker Service Configuration

Below is the Docker Compose configuration used to deploy Freqtrade:

services:
  freqtrade:
    image: freqtradeorg/freqtrade:stable
    restart: unless-stopped
    container_name: freqtrade
    volumes:
      - nfs-freqtrade:/freqtrade/user_data
    ports:
      - "8079:8080"
    command: >
      trade
      --logfile /freqtrade/user_data/logs/freqtrade.log
      --db-url sqlite:////freqtrade/user_data/tradesv3.sqlite
      --config /freqtrade/user_data/config.json
      --strategy Obelisk_Ichimoku_ZEMA_v1
    deploy:
      placement:
        constraints: [node.role == worker]
      replicas: 1
networks:
  backend:
    external: true
volumes:
  nfs-freqtrade:
    driver: local
    driver_opts:
      device: :/path/to/nfs/share/freqtrade
      o: addr=your.nfs.server.ip,nolock,soft,rw,nfsvers=4
      type: nfs

Key Configuration Details:

  1. Image Selection: The freqtradeorg/freqtrade:stable image was chosen for production stability. For development or plotting features, other images are available.
  2. Volume Mounting: The nfs-freqtrade volume ensures that logs, database files, and configuration persist across container restarts and node migrations.
  3. Port Mapping: Port 8079 on the host maps to port 8080 inside the container to expose Freqtrade’s API.
  4. Deployment Constraints: The constraints: [node.role == worker] ensures the service runs only on worker nodes.
  5. Command: Configures Freqtrade to start with the desired strategy and log settings.

Deploying the Service

To deploy Freqtrade on the Swarm cluster, follow these steps:

Monitor Logs: Tail the logs to confirm the bot is operational:

docker service logs -f freqtrade_freqtrade

Verify Deployment: Check the service status:

docker service ls

Ensure the freqtrade service is running with the desired number of replicas.

Deploy the Compose File: Save the configuration in a docker-compose.yml file and deploy it using:

docker stack deploy -c docker-compose.yml freqtrade

Initialize the Swarm Cluster (if not already done):

docker swarm init

Tips for Smooth Operation

  1. Use a Robust NFS Setup: Ensure your NFS server is highly available and performs well under load.
  2. Optimize Freqtrade Configuration: Customize the config.json and strategy files to suit your trading goals.
  3. Enable Monitoring: Integrate a monitoring solution like Grafana and Prometheus to track Freqtrade’s performance.
  4. Backup Data Regularly: Periodically back up the user_data directory to avoid data loss.

Conclusion

Installing Freqtrade on Docker Swarm provides a powerful and scalable setup for automated cryptocurrency trading. By leveraging Docker Swarm’s orchestration capabilities and a reliable NFS setup, you can ensure seamless operation and data persistence. With this guide, you should be well on your way to running a successful trading bot setup!