Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 333
Dana McDowelle Avatar asked Nov 30 '18 16:11

Dana McDowelle


People also ask

Can Docker be used with Python?

Docker is a containerization tool used for spinning up isolated, reproducible application environments. It is a popular development tool for Python developers. The tutorials and articles here will teach you how to include Docker to your development workflow and use it to deploy applications locally and to the cloud.

Can you run Python in a docker container?

It’s becoming extremely important in the industry now for every technologist, being a Quant developer, Data Engineer, Architect or a Data Scientist, to be able to run a Python process within a docker container. In a nutshell, we are going to create a docker file that we will use to build a docker image which we will then run in a docker container.

How to build a docker image from a python script?

CMD [ "python", "./test.py"] After you have created both the Python script and the Dockerfile, you can now use the Docker build command to build your Docker Image. Here -t is for adding tags so as to identify your image easily.

How to run editor in Docker Ubuntu container?

But one thing you should note that any editor is not available in docker ubuntu container thus you have to first install it using the apt-get install command. After install lets create a run.py file and then write the line print (“Hello Data Science Learner”) and run it.

How do I run a docker compose app from a container?

The stdin_open and tty tag is used to keep my container running. The volume tag is used to mount a folder from the host machine to the container. Now run the following command from the same directory where the docker-compose.yml file is located. The following command will start and run the entire app.


1 Answers

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/

like image 148
Rorschach Avatar answered Sep 24 '22 08:09

Rorschach