What should I put for Docker CMD and ENTRYPOINT for Flask app running “python myapp.py images/*”
What should I put for Docker CMD and ENTRYPOINT for Flask app running “python myapp.py images/*”
I am trying to run a Flask app using Docker.
Normally, to execute the Flask app, I run this inside of my Terminal:
python myapp.py images/*
I am unsure of how to convert that to Docker CMD syntax (or if I need to edit ENTRYPOINT).
Here is my docker file:
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential hdf5-tools
COPY . ~/myapp/
WORKDIR ~/myapp/
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["myapp.py"]
Inside of requirements.txt:
flask
numpy
h5py
tensorflow
keras
When I run the docker image:
person@person:~/Projects/$ docker run -d -p 5001:5000 myapp
19645b69b68284255940467ffe81adf0e32a8027f3a8d882b7c024a10e60de46
docker ps:
Up 24 seconds 0.0.0.0:5001->5000/tcp hardcore_edison
When I got to localhost:5001 I get no response.
Is it an issue with my CMD parameter?
EDIT:
New Dockerfile:
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential hdf5-tools
COPY . ~/myapp/
WORKDIR ~/myapp/
EXPOSE 5000
RUN pip install -r requirements.txt
CMD ["python myapp.py images/*.jpg "]
With this new configuration, when I run:
docker run -d -p 5001:5000 myapp
I get:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: "python myapp.py images/*.jpg ": stat python myapp.py images/*.jpg : no such file or directory": unknown.
When I run:
docker run -d -p 5001:5000 myapp python myapp.py images/*.jpg
I get the Docker image to run, but now when I go to localhost:5001, it complains that the connection was reset.
docker run -d -p 5001:5000 myapp python myapp.py images/*
CMD
["myapp.py", "images/*"]
3 Answers
3
The important thing is that you need a shell to expand your command line, so I’d write
CMD python myapp.py images/*
When you just write CMD like this (without the not-really-JSON brackets and quotes) Docker will implicitly feed the command line through a shell for you.
(You also might consider changing your application to support taking a directory name as configuration in some form and “baking it in” to your application, if these images will be in a fixed place in the container filesystem.)
I would only set ENTRYPOINT when (a) you are setting it to a wrapper shell script that does some first-time setup and then exec "$@"
; or (b) when you have a FROM scratch
image with a static binary and you literally cannot do anything with the container besides run the one binary in it.
exec "$@"
FROM scratch
When I change CMD to that, I get: docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: "python myapp.py images/*.jpg ": stat python myapp.py images/*.jpg : no such file or directory": unknown.
– God Complex
Aug 28 at 18:43
I'm glad you've already solved this issue. I put up this answer just for those who still have the same confusions like you do about ENTRYPOINT
and CMD
executives.
In a Dockerfile, ENTRYPOINT
and CMD
are two similar executives, but still have strong difference between them. The most important one(only seems to me) is that CMD
could be overwritten but ENTRYPOINT
not.
To explain this, I may offer you guys the command blow:docker run -tid --name=container_name image_name [command]
As we can see, command
is optional, and it(if exists) could overwrite CMD
defined in Dockerfile.
Let's back to your issue. You may have two ways to achieve your purpose-->
ENTRYPOINT
CMD
ENTRYPOINT
CMD
CMD
ENTRYPOINT
docker run -tid --name=container_name image_name [command]
command
CMD
ENTRYPOINT ["python"]
CMD ["/path/to/myapp.py", "/path/to/images/*.jpg"]
CMD python /path/to/myapp.py /path/to/images/*.jpg
To understand the first one, you may take CMD
as arguments for ENTRYPOINT
.
CMD
ENTRYPOINT
A simple example below.
Dockerfile-->FROM ubuntu:18.04
ENTRYPOINT ["cat"]
CMD ["/etc/hosts"]
FROM ubuntu:18.04
ENTRYPOINT ["cat"]
CMD ["/etc/hosts"]
Build image named test-cmd-show and start a container from it.docker run test-cmd-show
This would show the content in /etc/hosts
file. And go on...docker run test-cmd-show /etc/resolv.conf
And this would show us the content of /etc/resolv.conf
file. And go on ...docker run test-cmd-show --help
This would show the help information for command cat
.
docker run test-cmd-show
/etc/hosts
docker run test-cmd-show /etc/resolv.conf
/etc/resolv.conf
docker run test-cmd-show --help
cat
Fantastic, right?
Somehow, we could do more research though this functionality.
Add a relevant question: What's the difference between CMD and ENTRYPOINT?
One issue I found was that the app wasn't accessible to Docker. I added this to app.run:
host='0.0.0.0'
According to this:
Deploying a minimal flask app in docker - server connection issues
Next, Docker panics when you add a directory to the CMD parameters.
So, I removed ENTRYPOINT and CMD and manually added the command to the Docker run:
docker docker run -d -p 5001:5000 myappdocker python myapp.py images/*.jpg
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
First try:
docker run -d -p 5001:5000 myapp python myapp.py images/*
if that works, then change yourCMD
to["myapp.py", "images/*"]
– Javier Buzzi
Aug 28 at 18:12