5

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:

  • No directories report or data have been created under /home.
  • The last command python /home/hello.py does not execute on its own. I have to type it myself to get the script to run.
  • Once I type python /home/hello.py, the first two lines that greet and prompt for my name are executed properly, but then an error says that pandas is unknown.
  • It is not until I install pandas manually that the whole script runs correctly.

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

1
  • Only the last CMD will be valid, you should replace all other CMD by one RUN Commented Jun 3, 2017 at 17:37

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

3
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.

1 Comment

Why do you suggest to use another directory than /home? Could you please give an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.