Tfovid
Tfovid

Reputation: 843

Docker file to run Python with Pandas

I have created the following Dockerfile

FROM python
COPY . /home
CMD pip install pandas
CMD mkdir /home/report
CMD mkdir /home/data
CMD python /home/hello.py

where hello.py is the simple Python script

name = input('What is your Name? ')
print('Nice to meet you', name)

from pandas import read_csv
mydf = read_csv('mycsv.csv')
print(mydf.head())

I then build the Docker image with docker build -t myexample . and run it with docker run -it myexample bash so as to interact with it via the shell. The building goes fine and upon running it I presented with the shell prompt, but then:

So, in summary, it seems that none of the CMD statements were taken into account. What am I doing wrong?

Upvotes: 5

Views: 15628

Answers (2)

Ricardo Branco
Ricardo Branco

Reputation: 6079

FROM python
RUN pip install --no-cache-dir pandas && \
    mkdir /home/report /home/data && \
    chmod +x /home/hello.py
COPY . /home
VOLUME /home/report /home/data
WORKDIR /home
ENTRYPOINT /home/hello.py

Notes:

  • As a general rule, it's better to collapse 2 RUN statements into one so one layer is created.
  • I suggest you use a better directory than /home
  • Use volumes to store data and logs.

Upvotes: 3

Robert
Robert

Reputation: 36773

When you build an image use RUN to execute commands. Then, use CMD only once to declare the command that will start the container after the build (so there is only CMD):

Dockerfile:

FROM python
RUN pip install pandas
RUN mkdir /home/report
RUN mkdir /home/data

COPY . /home
WORKDIR /home
CMD python /home/hello.py

mycsv.csv:

a,b,c
1,2,3
4,5,6

Build with: docker build . -t pandas-test

Run:

▶ docker run -it pandas-test
What is your Name? Tfovid
Nice to meet you Tfovid
   a  b  c
0  1  2  3
1  4  5  6

Upvotes: 4

Related Questions