skip to Main Content

So, I’m new to Docker. I want to use Ta-Lib and freqtrade libraries for a bot I want to build. The problem is that I don’t want to build the Dockerfile over and over again. I have been copying my python code in the Dockerfile as shown below but as soon as I change the python code, I have to rebuild the Dockerfile because I changed the copied python script. Each build takes over 10 minutes to complete and this makes my life very difficult to test the bot. Is there a way that I build the Dockerfile with all the dependencies and requirements first and then simply run my script using the image made? So that I don’t have to rebuild the entire thing just after adding one line of code?
The Dockerfile is as shown below

FROM python:3.8

COPY trader1.py ./

RUN pip install numpy 
RUN pip install python-binance
RUN pip install pandas
RUN pip install backtrader
RUN pip install matplotlib
RUN pip install websocket_client

# TA-Lib
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && 
  tar -xvzf ta-lib-0.4.0-src.tar.gz && 
  cd ta-lib/ && 
  ./configure --prefix=/usr && 
  make && 
  make install

RUN rm -R ta-lib ta-lib-0.4.0-src.tar.gz
RUN pip install freqtrade

Thank you in advance!

2

Answers


  1. First, you should copy your python file as late as possible.

    FROM python:3.8
    
    RUN pip install numpy 
    RUN pip install python-binance
    RUN pip install pandas
    RUN pip install backtrader
    RUN pip install matplotlib
    RUN pip install websocket_client
    
    # TA-Lib
    RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && 
      tar -xvzf ta-lib-0.4.0-src.tar.gz && 
      cd ta-lib/ && 
      ./configure --prefix=/usr && 
      make && 
      make install
    
    RUN rm -R ta-lib ta-lib-0.4.0-src.tar.gz
    RUN pip install freqtrade
    
    COPY trader1.py ./
    

    The docker containers are layered, every line is one layer, when recreating a container, docker rebuild the container only from the changing layer.

    Second, you could create a volume
    If you use docker-compose, you could create a volume in your current folder, or subfolder, in order to have your file automatically synced with your host. (then you don’t need the COPY)

    Login or Signup to reply.
  2. You can mount the code as a volume, from then you only need to rebuild the docker image when the requirements are changed.

    Docker tutorial about volumes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search