I am new to docker and I am trying to dockerize a python flask microservice. The docker file builds successfully but on running the container it gives an error :
python3: can't open file 'flask': [Errno 2] No such file or directory
I am assuming there is some mistake in my docker file either in the COPY path, ENTRYPOINT or CMD i.e. the commands that I use to run the flask application. I am not able to figure out the mistake.
The directory structure on Ubuntu machine is:
/home/ubuntu/Docker/auth
The directory auth contains my Dockerfile and all other python flask files:
$ls
Dockerfile run.py views.py resources.py models.py
run.py is the main python flask file for execution. I'm sure there is some syntax error in how I am executing the CMD command for the flask application and it is not able to find run.py for execution. I am not able to correct that error.
The image builds successfully. For running the container I use:
docker build <imageid>
Dockerfile
FROM ubuntu:16.04
MAINTAINER xyz <[email protected]>
RUN apt-get update \
&& apt-get install -y software-properties-common vim \
&& add-apt-repository ppa:jonathonf/python-3.6 \
&& apt-get update -y \
&& apt-get install -y build-essential python3.6 python3.6-dev python3-pip
python3.6-venv \
&& pip3 install --upgrade pip
WORKDIR /auth
COPY . /auth
RUN pip3 install alembic==0.9.9 \
&& pip3 install Flask==1.0.2 \
ENTRYPOINT [ "python3" ]
CMD [ "export","FLASK_APP=run.py" ]
CMD [ "set", "FLASK_APP=run.py" ]
CMD [ "flask", "run", "--host=0.0.0.0" ]
Expected: Application should run on the container. Actual: Python3: can't open file 'flask': [Errno 2] No such file or directory
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.
The directory auth contains my Dockerfile and all other python flask files: run.py is the main python flask file for execution. I'm sure there is some syntax error in how I am executing the CMD command for the flask application and it is not able to find run.py for execution.
The main difference is that the same Python Flask web server is now running within a docker container. We can launch multiple docker containers on different machines and even launch them on the same machine by overriding the port number. That’s all it is.
Docker is a software platform. It enables software developers to develop, ship and run applications within its containers. Containers are lightweight software applications. We are going to build a Docker image in this tutorial. What is a docker file, image and container?
The best use for ENTRYPOINT is to set the image’s main command, allowing that image to be run as though it was that command (and then use CMD as the default flags).
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint
lots of people seem to miss this point about the ENTRYPOINT
and CMD
Dockerfile-instructions.
the ENTRYPOINT
instruction supposed to run some executable, which should run every time you start the container, such as starting your server.
the CMD
supposed to include the flags provided to that executable, so they can be easily overridden when running the container.
i am not sure you are supposed to have more then one CMD
instruction. if you need to run commands during the build process, you can use the RUN
instruction - for example:
RUN mkdir some/dir
now:
run.py is the main python flask file for execution
hence i suggest you define it as your entrypoint:
ENTRYPOINT [ "./run.py" ]
commands that you may also want to run every time the container starts, such as flask run --host=0.0.0.0
you can:
move that command to sit inside the run.py
file
or
keep the CMD [ "flask", "run", "--host=0.0.0.0" ]
line. this command will be passed as an argument to the run.py entrypoint, so you may execute it in there. that way you can easily override the command when running the container with alternative arguments.
this stuff is also in the docs:
Understand how CMD and ENTRYPOINT interact
Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are few rules that describe their co-operation.
Dockerfile should specify at least one of CMD or ENTRYPOINT commands.
ENTRYPOINT should be defined when using the container as an executable.
CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
CMD will be overridden when running the container with alternative arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With