Reputation: 843
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:
report
or data
have been created under /home
.python /home/hello.py
does not execute on its own. I have to type it myself to get the script to run.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.So, in summary, it seems that none of the CMD
statements were taken into account. What am I doing wrong?
Upvotes: 5
Views: 15628
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:
Upvotes: 3
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