Issues with Running Flask and SQLAlchemy app through Docker

I have a web application written in python, sqlalchemy and flask.

The app along with static files, style sheets etc are stored in a folder called flask-app and the program is to be run from:

python server.py

When i run this from the terminal, it works perfectly well. But it doesn’t work when i run it from docker.

My Dockerfile looks like this:

FROM ubuntu:latest

RUN apt-get update -y
RUN apt-get install -y python-pip python3.5 build-essential

COPY . /server
WORKDIR /server

RUN pip install --upgrade pip 
RUN pip install flask SQLAlchemy

ENTRYPOINT ["python3.5"]
CMD ["server.py"]

I’m running the following:

docker build -t myapp/wine-catalog . 

It runs successfully.

But when i run:

docker run myapp/wine-catalog 

It says that:

Traceback (most recent call last):
  File "server.py", line 1, in <module>
    from flask import Flask, render_template, request, url_for, redirect, flash, jsonify
ImportError: No module named 'flask'

I can’t figure out what is going wrong. Any help?

I’m running Manjaro 4.5.0

Ubuntu has both Python 2 and Python 3 packages. Is it possible that you’re getting the Python 2 pip, and then trying to run a Python 3 interpreter? That is, is there a python3-pip package that you should be using instead?

You also might consider writing a standard setuptools setup.py script for your application that lists out all of its dependencies, and then RUN pip install /server to install the application and its dependencies.

One last note: I would not use an ENTRYPOINT here, for the most basic reason that I’d otherwise tell you to docker run --rm -it myapp/wine-catalog bash to try to get a shell but that won’t work because you’ve told Docker to try to run every command through Python. You could add a manual --entrypoint but that’s something that’s hard to remember. The best thing to do is to make sure your server.py is executable and starts with a “shebang” line #!/usr/bin/env python3, so that you can just run it as CMD; next-best is to CMD ["python3.5", "server.py"].