Reputation: 458
I am following the Docker-getting started guide to using docker with a python application, but when docker gets up to the command:
docker run -p 80:80 username/repo:tag
I'm getting the following error message:
Traceback (most recent call last):
File "app.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
I have installed Flask
for when i run which flask
and which python
/usr/local/bin/flask
/usr/local/bin/python
are returned. When i however perform sudo pip install Flask
, i get
Requirement already satisfied: flask in ./python2.7/site-packages
Requirement already satisfied: click>=2.0 in ./python2.7/site-
packages (from flask)
Requirement already satisfied: Werkzeug>=0.7 in ./python2.7/site-
packages (from flask)
Requirement already satisfied: Jinja2>=2.4 in ./python2.7/site-
packages (from flask)
Requirement already satisfied: itsdangerous>=0.21 in
./python2.7/site-packages (from flask)
Requirement already satisfied: MarkupSafe>=0.23 in ./python2.7/site-
packages (from Jinja2>=2.4->flask)
which is clearly a different directory. My initial thought would be that i'm using python from two different directories and this is why i cant run the docker command. But i am also a noob and don't really know how to start troubleshooting and fixing this. I would very much appreciate it if someone gave me some pointers here.Thanks in advance.
EDIT
Here is my Dockerfile
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt --proxy
https://proxy:8080 --trusted-host pypi.python.org
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
Upvotes: 0
Views: 641
Reputation: 4137
Not a direct answer to the question, but this can save you a lot of time.
Every docker command adds a new layer to the image. When building the image, docker will try to figure out what layer needs re-building. You would be changing the files in your app probably every time you build. This is the first layer so you end up having to install requirements every time you build. This can add a lot of extra waiting.
Let's copy in the requirements.txt
and install the requirements first. Then that layer will be cached until we change the requirements.
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Install any needed packages specified in requirements.txt
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt --proxy https://proxy:8080 --trusted-host pypi.python.org
ADD . /app
WORKDIR /app
EXPOSE 80
When building a dockerfile, try to visualise the layers it creates and how that will be helpful to reduce build time.
Upvotes: 1
Reputation: 1388
The pip install
instruction should be wrapped and you might want to put your ADD
before WORKDIR
, also you don't seem to have a correct ENTRYPOINT
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Copy the current directory contents into the container at /app
ADD . /app
# Set the working directory to /app
WORKDIR /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt --proxy \
https://proxy:8080 --trusted-host pypi.python.org
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
## Where is the ENTRYPOINT ?
Upvotes: 0