简体   繁体   中英

Putting a python script into a docker container

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM