flask not found in Docker - docker

In the Docker file, I get this error
ImportError: No module named flask
Here is the Docker file
FROM continuumio/anaconda3
MAINTAINER kumar
COPY ./flask_master /usr/local/python
EXPOSE 5000
WORKDIR /usr/local/python
RUN apt-get update && apt-get install -y python3-pip
RUN pip3 install -r requirements.txt
CMD python flaskPredictAPI.py
Here is the requirements.txt
flask
flasgger
joblib
But when I run it, I get this
docker run -i -t -p 5000:5000 randomforestapi
Traceback (most recent call last):
File "flaskPredictAPI.py", line 1, in
from flask import Flask, request
ImportError: No module named flask

Are you able to build your docker image successfully? Did you checked if during build the requirements.txt gets executed without any error? I would suggest you generate your requirements.txt using below command in your project folder.
pip freeze > requirements.txt
It would look something like this
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1

Related

Docker: unable to acces other files in directory

I'm new to docker and I'm trying to create a docker image in which a python 2.7 script runs. This file imports functions and classes from other files in the same directory. Building the docker image already works, but running it gives an error that the imported files in the python 2.7 script don't exist.
This is the dockerfile I'm using
FROM python:2.7
WORKDIR C:\something\something\print
RUN apt-get update
RUN apt-get install python-cairo
RUN apt-get install python-gi
RUN apt-get install python-gi-cairo
RUN apt-get install -y gir1.2-gtk-3.0
RUN apt-get install -y mapnik-utils
RUN pip install bs4
RUN pip install image
RUN pip install mapnik
RUN pip install numpy
RUN pip install psycopg2
COPY test_server.py ./
CMD [ "python", "./test_server.py"]
And this is the error I receive after I do "docker run"
Traceback (most recent call last):
File "./test_server.py", line 13, in <module>
from utils import client, reprojector, formats
ImportError: No module named utils
The module named utils is a folder in the WORKDIR I specified in the dockerfile, but I don't understand why it can't be accessed. What am I doing wrong here?

Docker Build Failing with Given as STDIN as input

Why Docker image build is getting failed when build with - ?
Host Details
- docker desktop community 2.1.0.5 for Windows
- Windows 10
Dockerfile:
FROM ubuntu:latest
MAINTAINER "rizwan#gm.com"
RUN apt-get update \
&& apt-get install -y python3-pip python3-dev \
&& cd /usr/local/bin \
&& ln -s /usr/bin/python3 python \
&& pip3 install --upgrade pip
WORKDIR /app
COPY . /app
COPY requirements.txt /app/requirements.txt
RUN pip3 --no-cache-dir install -r requirements.txt
EXPOSE 5000
CMD ["python3", "my_service.py","--input-path= /input.csv", "--output-path=/output.csv"]
Folder Structure
-Root
-Application.py
-Dockerfile
-requirements.txt
COMMAND
- Failing : docker build - < Dockerfile
Message: ERROR: Could not open requirements file: [Errno 2] No such
file or directory: 'requirements.txt'
- Successful: docker build .
When you run
docker build - < Dockerfile
it sends only the Dockerfile to the Docker daemon, but no other files. When you tell Docker to COPY a file into the image, you haven't actually sent it the file. It's very similar to including everything in your source tree in the .dockerignore file.
Typically you'll send Docker the current directory as the context directory instead:
docker build . # knows to look for Dockerfile by default

Having trouble with my Dockerfile and setting correct working directory

This is my first Dockerfile and this is what I have so far.
FROM python:3.6-stretch
# install build utilities
RUN apt-get update && \
apt-get install -y gcc make apt-transport-https ca-certificates build-essential
# check our python environment
RUN python3 --version
RUN pip3 --version
# set the working directory for containers
WORKDIR /usr/src/toxic-content-monitoring
# Installing python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy all the files from the project’s root to the working directory
COPY src/ /src/
RUN ls -la /src/*
# Running Python Application
CMD ["python3", "/src/main.py"]
But when I am trying to run the docker image the files inside the data folder cannot be found.
Here is a picture of the my project structure.
Changed it to what was suggested and still getting an error.
FROM python:3.6-stretch
# install build utilities
RUN apt-get update && \
apt-get install -y gcc make apt-transport-https ca-certificates build-essential
# check our python environment
RUN python3 --version
RUN pip3 --version
# set the working directory for containers
WORKDIR /usr/src/toxic-content-monitoring
# Installing python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy all the files from the project’s root to the working directory
COPY src/ /src/
COPY data /data/
RUN ls -la /src/*
# Running Python Application
CMD ["python3", "/src/main.py"]
This is the error message.
docker run toxic-content-monitoring:0.1
wiki.en.vec: 6.60GB [10:05, 10.9MB/s]
0%| | 0/2519371 [00:00<?, ?it/s]Skipping token 2519370 with 1-dimensional vector ['300']; likely a header
100%|██████████| 2519371/2519371 [08:36<00:00, 4878.67it/s]
Traceback (most recent call last):
File "/src/main.py", line 11, in <module>
from preprocessor import normalize_comment, clean_text
File "/src/preprocessor.py", line 42, in <module>
word_file = open(link, "r")
FileNotFoundError: [Errno 2] No such file or directory: 'data/identity_hateWordFile.txt'
I believe your Dockerfile is missing COPY data/ /data/ command in it. You are only doing a copy of src folder and not data. Please include COPY data /data/ right after COPY src/ /src/ line in your Dockerfile and give it a try.

Python custom module not found after running DockerFile

I am new to Docker and am currently having issues importing local python files into my Flask application using a Dockerfile
File hierarchy:
survey_tool
cli
survey_input.py
web
app.py
Current Dockerfile:
FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY cli /app
COPY web /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
In my application, I am trying to run a method from another python file (called gui_prompt).
When I try to build the app, the app immediately crashes and gives me this stack trace when running the docker logs file on the container id:
File "app.py", line 3, in <module>
from cli.survey_input import gui_prompt
ImportError: No module named cli.survey_input
Am I not importing the cli folder correctly?

Docker: Permissions error on OpenShift Origin 3.6

I'm trying to run a pretty simple Flask API in OpenShift Origin 3.6. I can build this container just fine locally and remotely, but when I go to deploy on OpenShift, I get permissions errors on the RUN chmod -R 777 ... lines. Has anyone found a way around this? I wonder if my corporate environment doesn't allow this type of copying, but it's all within a container...
Edit: providing a completely minimal example
Directory structure:
project
├── Dockerfile
└── app
└── api.py
Dockerfile to build base image:
FROM docker.io/ubuntu:16.04
RUN apt-get update && apt-get install -y --no-install-recommends \
cmake curl git make gunicorn nginx python3 python3-pip python3-setuptools build-essential \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
RUN pip3 install --upgrade pip
RUN pip3 install pandas numpy scikit-learn scipy xgboost flask-restful nltk gunicorn
RUN mkdir -p /home/app
WORKDIR /
RUN python3 -c 'import nltk; nltk.download("punkt")'
RUN mv /root/nltk_data /home/app
I then run docker build . -t project:latest --no-cache. Next, the Dockerfile that uses the base image from above to deploy the actual application (I basically just comment out the "base image" lines from above and uncomment these ones from below, using the same Dockerfile file):
FROM project:latest
COPY app /home/app
RUN chmod -R 777 /home/app
WORKDIR /home/app
EXPOSE 5000
CMD ["python3", "api.py"]
I build the container to be deployed using docker build . -t project:app --no-cache.
api.py:
import time
if __name__ == '__main__':
while True:
print('This app is running')
time.sleep(30)

Resources