skip to Main Content

I have tried to containerize my python code , this is working, building the docker image, running the container and invoking the python code in my app.py file. next, i want to be able to upload the sample.txt file that my python code reads from the root of the application to process some data, sample.txt in the example below. but this file needs to be updated and i want to be able to update this file and run the container, without building the image. how can i create/mount a directory and put the csv file there and give the path to my python code?

app.py

import pandas as pd
print pd.read_csv('sample.csv')
...

docker file

# Dockerfile
FROM debian:10.3-slim

WORKDIR /app

RUN apt-get update && apt-get -y dist-upgrade

RUN apt-get -y install apt-utils 
    build-essential 
    python3 
    gcc 
    python3-dev 
    python3-pip 
    python3-numpy 
    python3-pandas

COPY requirements.txt /app/requirements.txt
RUN pip3 install -r requirements.txt

# Copy the python script
COPY modbus_to_influx.py /app/modbus_to_influx.py

CMD ["python3", "app.py"]

requirements.txt

numpy 
pandas

2

Answers


  1. When you docker run, you can (bind) mount folders/files on the host as folders/files on the container:

    docker run 
    --interactive --tty --rm 
    --volume=/path/to/host/file:/path/to/container/file 
    your-container
    

    For example if you had example.csv on the host’s root directory and you want it to be accessible to your Python app in the container in /data, you would:

    docker run 
    --interactive --tty --rm 
    --volume=/example.csv:/data/example.csv 
    your-container
    

    NOTE That the file name must be included in the host-path and container-path elements of --volume=host-path:container-path permitting you to rename the host’s file (example.csv) to something else on the container too.

    Login or Signup to reply.
  2. Or you can use docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
    https://docs.docker.com/engine/reference/commandline/cp/

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