Home > database >  How to make a Dockerfile and run it for multiple Python files?
How to make a Dockerfile and run it for multiple Python files?

Time:01-24

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!

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

You can make use of docker's layer caching by moving this line COPY trader1.py ./ to the very end of your Dockerfile or right before RUN pip install freqtrade if it is needed there. Now, when you change your code, all the layers above will be taken from the cache and building your image will become much faster. When you change a layer (a line or instruction in a Dockerfile) all layers below that one have to be rebuilt. You change it by either rewriting it or by changing what is used in that line, like your code. That means that when you write a docker file you should order your lines from don't change very often to change frequently if possible.

  •  Tags:  
  • Related