11

I am attempting to put an existing python function into a docker container. The python code takes a CSV file (that will be in the same directory as the dockerfile) as input data and does some calculations. The py file looks like this and is named "PracticeDeploy.py":

import pandas as pd
import pickle
from OptimizationFunction import main_barrel

# Read in model
gbrModel = pickle.load(open('/src/ValDiffGBR.pkl', 'rb'))
file_name = str(input())
data=pd.read_csv('/src/'+file_name)
new_data = data.dropna(how='any')
preds = main_barrel(gbrModel, new_data)

Within my docker directory, I already have a subfolder which contains my first dockerfile with all libraries installed. Also in my directory, I have the py file, the CSV, and the model I import in the above py file. I am now trying to build another dockerfile that will take the CSV name as input and then run the python code.

Here is the dockerfile:

FROM [my repository] as builder
ARG DATA_FILE

RUN mkdir src
WORKDIR /src
COPY . /src

ENTRYPOINT ["PracticeDeploy.py"]

I build like this:

sudo docker build --rm -f Dockerfile -t "first_docker" --build-arg DATA_FILE='/src/[csv_name].csv' .

I attempt to run like this:

sudo docker run --rm first_docker

However I get this error:

docker: Error response from daemon: OCI runtime create 
failed: container_linux.go:348: starting container process 
caused "exec: \"PracticeDeploy.py\": executable file not 
found in $PATH": unknown.

Should I only insert arguments when running? Am I approaching this correctly? I am very new to docker and am completely stumped.

3
  • ENTRYPOINT ["./PracticeDeploy.py"] should work. Commented Nov 30, 2018 at 16:19
  • 1
    Try adding "python" to your Entrypoint. Your PracticeDeploy.py is not executable nor does it has #!/usr/bin/env python. Commented Nov 30, 2018 at 16:19
  • Someone else has used CMD instead of ENTRYPOINT to run a python file with success here: stackoverflow.com/questions/35766638/…. Here is a blog on how to dockerize your python, they also use CMD: runnable.com/docker/python/dockerize-your-python-application Commented Nov 30, 2018 at 16:23

1 Answer 1

21

CMD SOLUTION

I would recommend switching from Entrypoint to CMD

CMD [ "python", "./my_script.py" ]

This method can be seen in depth here: https://runnable.com/docker/python/dockerize-your-python-application

Some more complexity (flags etc) can also be handled with CMD as can be seen here : how to pass command line arguments to a python script running in docker

ENTRYPOINT SOLUTION

ENTRYPOINT ["python", "app.py"]

This style of solution is explained in depth here: https://lostechies.com/gabrielschenker/2016/08/21/container-entrypoint/

The difference between the two (if you're curious and don't know)

CMD commands can be overwritten from the command line. CMD is effectively the default value of your container's command.

ENTRYPOINT commands are not overwritten from the command line.

CMD and ENTRYPOINT are similar, but I prefer command because it enables me to change the flags or command at run time if preferred, while keeping the same dockerfile that can be run without a command if desired.

Here is a longer form discussion of the difference: http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/

Sign up to request clarification or add additional context in comments.

1 Comment

First link on runnable.com is dead. Take this link: docker.com/blog/how-to-dockerize-your-python-applications

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.