Cannot run Flask on Docker (ModuleNotFoundError)
I have been trying to run my Python API (using Flask) with Docker for a while, but keep running into this issue:

ModuleNotFoundError: No module named 'flask'

Running this on Mac OS X (10.14.5) with Docker version 19.03.1, build 74b1e89.

My Dockerfile looks like this:

FROM pytorch/pytorch:1.1.0-cuda10.0-cudnn7.5-runtime

RUN apt-get update &amp;&amp; apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential
RUN python3 --version
RUN pip3 --version

COPY . /app
WORKDIR /app

RUN pip3 install -r requirements.txt
RUN pip3 install flask # just to be sure Flask gets installed
RUN which flask
RUN ls -alh

ENTRYPOINT ["python3"]
CMD ["api.py"]


Note that I'm extending the PyTorch docker image, because my API will be running an ML model using PyTorch. I tried starting from other images such as ufoym/deepo but with the same Flask error.

I stripped my api.py file to look like this (running it locally is no problem):

from flask import Flask, request, jsonify
#import classifier # commented out for now because I just want the API to work first

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def process():
    # TODO
    return 'test'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')


When I build my Docker image, I get the following output:

$ docker build -t flower-image-classifier:latest .
Sending build context to Docker daemon  269.9MB
Step 1/14 : FROM pytorch/pytorch:1.1.0-cuda10.0-cudnn7.5-runtime
 ---&gt; 299bfb9e54db
Step 2/14 : RUN apt-get update &amp;&amp; apt-get install -y python3
 ---&gt; Running in f1d7db593022
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [908 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]
Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [573 kB]
Get:11 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6117 B]
Get:12 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1292 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [980 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]
Get:18 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B]
Fetched 16.0 MB in 9s (1744 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  dh-python file libmagic1 libmpdec2 libpython3-stdlib libpython3.5-minimal
  libpython3.5-stdlib mime-support python3-minimal python3.5 python3.5-minimal
Suggested packages:
  python3-doc python3-tk python3-venv python3.5-venv python3.5-doc
  binfmt-support
The following NEW packages will be installed:
  dh-python file libmagic1 libmpdec2 libpython3-stdlib libpython3.5-minimal
  libpython3.5-stdlib mime-support python3 python3-minimal python3.5
  python3.5-minimal
0 upgraded, 12 newly installed, 0 to remove and 29 not upgraded.
Need to get 4885 kB of archives.
After this operation, 28.4 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-minimal amd64 3.5.2-2ubuntu0~16.04.5 [524 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 python3.5-minimal amd64 3.5.2-2ubuntu0~16.04.5 [1598 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial/main amd64 python3-minimal amd64 3.5.1-3 [23.3 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial/main amd64 mime-support all 3.59ubuntu1 [31.0 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial/main amd64 libmpdec2 amd64 2.4.2-1 [82.6 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-stdlib amd64 3.5.2-2ubuntu0~16.04.5 [2134 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 python3.5 amd64 3.5.2-2ubuntu0~16.04.5 [165 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/main amd64 libpython3-stdlib amd64 3.5.1-3 [6818 B]
Get:9 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 dh-python all 2.20151103ubuntu1.1 [74.1 kB]
Get:10 http://archive.ubuntu.com/ubuntu xenial/main amd64 python3 amd64 3.5.1-3 [8710 B]
Get:11 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libmagic1 amd64 1:5.25-2ubuntu1.2 [216 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 file amd64 1:5.25-2ubuntu1.2 [21.2 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 4885 kB in 7s (663 kB/s)
Selecting previously unselected package libpython3.5-minimal:amd64.
(Reading database ... 11013 files and directories currently installed.)
Preparing to unpack .../libpython3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package python3.5-minimal.
Preparing to unpack .../python3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking python3.5-minimal (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package python3-minimal.
Preparing to unpack .../python3-minimal_3.5.1-3_amd64.deb ...
Unpacking python3-minimal (3.5.1-3) ...
Selecting previously unselected package mime-support.
Preparing to unpack .../mime-support_3.59ubuntu1_all.deb ...
Unpacking mime-support (3.59ubuntu1) ...
Selecting previously unselected package libmpdec2:amd64.
Preparing to unpack .../libmpdec2_2.4.2-1_amd64.deb ...
Unpacking libmpdec2:amd64 (2.4.2-1) ...
Selecting previously unselected package libpython3.5-stdlib:amd64.
Preparing to unpack .../libpython3.5-stdlib_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package python3.5.
Preparing to unpack .../python3.5_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking python3.5 (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package libpython3-stdlib:amd64.
Preparing to unpack .../libpython3-stdlib_3.5.1-3_amd64.deb ...
Unpacking libpython3-stdlib:amd64 (3.5.1-3) ...
Selecting previously unselected package dh-python.
Preparing to unpack .../dh-python_2.20151103ubuntu1.1_all.deb ...
Unpacking dh-python (2.20151103ubuntu1.1) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Setting up libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Setting up python3.5-minimal (3.5.2-2ubuntu0~16.04.5) ...
Setting up python3-minimal (3.5.1-3) ...
Selecting previously unselected package python3.
(Reading database ... 11957 files and directories currently installed.)
Preparing to unpack .../python3_3.5.1-3_amd64.deb ...
Unpacking python3 (3.5.1-3) ...
Selecting previously unselected package libmagic1:amd64.
Preparing to unpack .../libmagic1_1%3a5.25-2ubuntu1.2_amd64.deb ...
Unpacking libmagic1:amd64 (1:5.25-2ubuntu1.2) ...
Selecting previously unselected package file.
Preparing to unpack .../file_1%3a5.25-2ubuntu1.2_amd64.deb ...
Unpacking file (1:5.25-2ubuntu1.2) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Setting up mime-support (3.59ubuntu1) ...
Setting up libmpdec2:amd64 (2.4.2-1) ...
Setting up libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Setting up python3.5 (3.5.2-2ubuntu0~16.04.5) ...
Setting up libpython3-stdlib:amd64 (3.5.1-3) ...
Setting up libmagic1:amd64 (1:5.25-2ubuntu1.2) ...
Setting up file (1:5.25-2ubuntu1.2) ...
Setting up python3 (3.5.1-3) ...
running python rtupdate hooks for python3.5...
running python post-rtupdate hooks for python3.5...
Setting up dh-python (2.20151103ubuntu1.1) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Removing intermediate container f1d7db593022
 ---&gt; 562a488914b2
Step 3/14 : RUN apt-get install -y python3-pip
 ---&gt; Running in 3884de8e3194
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libexpat1 libexpat1-dev libpython3-dev libpython3.5 libpython3.5-dev
  python-pip-whl python3-dev python3-pkg-resources python3-setuptools
  python3-wheel python3.5-dev
Suggested packages:
  python-setuptools-doc
The following NEW packages will be installed:
  libexpat1-dev libpython3-dev libpython3.5 libpython3.5-dev python-pip-whl
  python3-dev python3-pip python3-pkg-resources python3-setuptools
  python3-wheel python3.5-dev
The following packages will be upgraded:
  libexpat1
1 upgraded, 11 newly installed, 0 to remove and 28 not upgraded.
Need to get 40.7 MB of archives.
After this operation, 62.4 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libexpat1 amd64 2.1.0-7ubuntu0.16.04.4 [71.4 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libexpat1-dev amd64 2.1.0-7ubuntu0.16.04.4 [115 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5 amd64 3.5.2-2ubuntu0~16.04.5 [1360 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-dev amd64 3.5.2-2ubuntu0~16.04.5 [37.3 MB]
Get:5 http://archive.ubuntu.com/ubuntu xenial/main amd64 libpython3-dev amd64 3.5.1-3 [6926 B]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 python-pip-whl all 8.1.1-2ubuntu0.4 [1110 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 python3.5-dev amd64 3.5.2-2ubuntu0~16.04.5 [413 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/main amd64 python3-dev amd64 3.5.1-3 [1186 B]
Get:9 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 python3-pip all 8.1.1-2ubuntu0.4 [109 kB]
Get:10 http://archive.ubuntu.com/ubuntu xenial/main amd64 python3-pkg-resources all 20.7.0-1 [79.0 kB]
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 python3-setuptools all 20.7.0-1 [88.0 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial/universe amd64 python3-wheel all 0.29.0-1 [48.1 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 40.7 MB in 1min 5s (624 kB/s)
(Reading database ... 12000 files and directories currently installed.)
Preparing to unpack .../libexpat1_2.1.0-7ubuntu0.16.04.4_amd64.deb ...
Unpacking libexpat1:amd64 (2.1.0-7ubuntu0.16.04.4) over (2.1.0-7ubuntu0.16.04.3) ...
Selecting previously unselected package libexpat1-dev:amd64.
Preparing to unpack .../libexpat1-dev_2.1.0-7ubuntu0.16.04.4_amd64.deb ...
Unpacking libexpat1-dev:amd64 (2.1.0-7ubuntu0.16.04.4) ...
Selecting previously unselected package libpython3.5:amd64.
Preparing to unpack .../libpython3.5_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking libpython3.5:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package libpython3.5-dev:amd64.
Preparing to unpack .../libpython3.5-dev_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking libpython3.5-dev:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package libpython3-dev:amd64.
Preparing to unpack .../libpython3-dev_3.5.1-3_amd64.deb ...
Unpacking libpython3-dev:amd64 (3.5.1-3) ...
Selecting previously unselected package python-pip-whl.
Preparing to unpack .../python-pip-whl_8.1.1-2ubuntu0.4_all.deb ...
Unpacking python-pip-whl (8.1.1-2ubuntu0.4) ...
Selecting previously unselected package python3.5-dev.
Preparing to unpack .../python3.5-dev_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking python3.5-dev (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package python3-dev.
Preparing to unpack .../python3-dev_3.5.1-3_amd64.deb ...
Unpacking python3-dev (3.5.1-3) ...
Selecting previously unselected package python3-pip.
Preparing to unpack .../python3-pip_8.1.1-2ubuntu0.4_all.deb ...
Unpacking python3-pip (8.1.1-2ubuntu0.4) ...
Selecting previously unselected package python3-pkg-resources.
Preparing to unpack .../python3-pkg-resources_20.7.0-1_all.deb ...
Unpacking python3-pkg-resources (20.7.0-1) ...
Selecting previously unselected package python3-setuptools.
Preparing to unpack .../python3-setuptools_20.7.0-1_all.deb ...
Unpacking python3-setuptools (20.7.0-1) ...
Selecting previously unselected package python3-wheel.
Preparing to unpack .../python3-wheel_0.29.0-1_all.deb ...
Unpacking python3-wheel (0.29.0-1) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Setting up libexpat1:amd64 (2.1.0-7ubuntu0.16.04.4) ...
Setting up libexpat1-dev:amd64 (2.1.0-7ubuntu0.16.04.4) ...
Setting up libpython3.5:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Setting up libpython3.5-dev:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Setting up libpython3-dev:amd64 (3.5.1-3) ...
Setting up python-pip-whl (8.1.1-2ubuntu0.4) ...
Setting up python3.5-dev (3.5.2-2ubuntu0~16.04.5) ...
Setting up python3-dev (3.5.1-3) ...
Setting up python3-pip (8.1.1-2ubuntu0.4) ...
Setting up python3-pkg-resources (20.7.0-1) ...
Setting up python3-setuptools (20.7.0-1) ...
Setting up python3-wheel (0.29.0-1) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Removing intermediate container 3884de8e3194
 ---&gt; 388a6aa0bb5f
Step 4/14 : RUN apt-get install -y build-essential
 ---&gt; Running in 2e8c8dc95d9e
Reading package lists...
Building dependency tree...
Reading state information...
build-essential is already the newest version (12.1ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 28 not upgraded.
Removing intermediate container 2e8c8dc95d9e
 ---&gt; 3c08d3467777
Step 5/14 : RUN python3 --version
 ---&gt; Running in 027dcf1dc8b6
Python 3.6.8 :: Anaconda, Inc.
Removing intermediate container 027dcf1dc8b6
 ---&gt; 0a8389f2fe13
Step 6/14 : RUN pip3 --version
 ---&gt; Running in 1df781383970
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
Removing intermediate container 1df781383970
 ---&gt; ad30a1cd5323
Step 7/14 : COPY . /app
 ---&gt; db6769b7ba80
Step 8/14 : WORKDIR /app
 ---&gt; Running in c234276bf1f6
Removing intermediate container c234276bf1f6
 ---&gt; face402e69b6
Step 9/14 : RUN pip3 install -r requirements.txt
 ---&gt; Running in d89df8c75e5a
Collecting Flask==1.1.1 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Collecting numpy==1.16.4 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/bb/ef/d5a21cbc094d3f4d5b5336494dbcc9550b70c766a8345513c7c24ed18418/numpy-1.16.4-cp35-cp35m-manylinux1_x86_64.whl (17.2MB)
Collecting Pillow==6.1.0 (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/d6/98/0d360dbc087933679398d73187a503533ec0547ba4ffd2115365605559cc/Pillow-6.1.0-cp35-cp35m-manylinux1_x86_64.whl (2.1MB)
Collecting Werkzeug&gt;=0.15 (from Flask==1.1.1-&gt;-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
Collecting itsdangerous&gt;=0.24 (from Flask==1.1.1-&gt;-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click&gt;=5.1 (from Flask==1.1.1-&gt;-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Jinja2&gt;=2.10.1 (from Flask==1.1.1-&gt;-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting MarkupSafe&gt;=0.23 (from Jinja2&gt;=2.10.1-&gt;Flask==1.1.1-&gt;-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/6e/57/d40124076756c19ff2269678de7ae25a14ebbb3f6314eb5ce9477f191350/MarkupSafe-1.1.1-cp35-cp35m-manylinux1_x86_64.whl
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, Flask, numpy, Pillow
Successfully installed Flask-1.1.1 Jinja2-2.10.1 MarkupSafe-1.1.1 Pillow-6.1.0 Werkzeug-0.15.5 click-7.0 itsdangerous-1.1.0 numpy-1.16.4
You are using pip version 8.1.1, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container d89df8c75e5a
 ---&gt; 95766b1e174e
Step 10/14 : RUN pip3 install flask
 ---&gt; Running in 97e806168780
Requirement already satisfied (use --upgrade to upgrade): flask in /usr/local/lib/python3.5/dist-packages
Requirement already satisfied (use --upgrade to upgrade): itsdangerous&gt;=0.24 in /usr/local/lib/python3.5/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): Jinja2&gt;=2.10.1 in /usr/local/lib/python3.5/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): Werkzeug&gt;=0.15 in /usr/local/lib/python3.5/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): click&gt;=5.1 in /usr/local/lib/python3.5/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): MarkupSafe&gt;=0.23 in /usr/local/lib/python3.5/dist-packages (from Jinja2&gt;=2.10.1-&gt;flask)
You are using pip version 8.1.1, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 97e806168780
 ---&gt; dd8d93e7b8bc
Step 11/14 : RUN which flask
 ---&gt; Running in 0ac4678d5ecd
/usr/local/bin/flask
Removing intermediate container 0ac4678d5ecd
 ---&gt; e9325cc606ec
Step 12/14 : RUN ls -alh
 ---&gt; Running in e249ecafd7fa
total 1.8M
drwxr-xr-x 6 root root  4.0K Aug  4 14:29 .
drwxr-xr-x 1 root root  4.0K Aug  4 14:30 ..
-rw-r--r-- 1 root root   11K Aug  2 11:15 .DS_Store
drwxr-xr-x 5 root root  4.0K Jul 28 11:45 .git
-rw-r--r-- 1 root root    18 Aug  2 13:52 .gitignore
-rw-r--r-- 1 root root   878 Aug  4 14:27 Dockerfile
-rw-r--r-- 1 root root 1004K Jul 24 12:49 Image Classifier Project.html
-rw-r--r-- 1 root root  690K Jul 24 12:50 Image Classifier Project.ipynb
-rw-r--r-- 1 root root  1.1K Feb  6  2018 LICENSE
-rw-r--r-- 1 root root   686 Aug  2 14:04 README.md
drwxr-xr-x 2 root root  4.0K Aug  4 13:05 __pycache__
-rw-r--r-- 1 root root  1018 Aug  4 14:12 api.py
drwxr-xr-x 2 root root  4.0K Jul 20 12:32 assets
-rw-r--r-- 1 root root  2.2K Feb  6  2018 cat_to_name.json
drwxr-xr-x 2 root root  4.0K Jul 24 12:06 checkpoints
-rw-r--r-- 1 root root   245 Aug  2 21:48 environment.yml
-rw-r--r-- 1 root root   921 Aug  4 09:11 environment0.yml
-rw-r--r-- 1 root root  8.6K Aug  4 13:02 flower_classifier.py
-rw-r--r-- 1 root root  1.1K Aug  2 19:17 predict.py
-rw-r--r-- 1 root root    41 Aug  4 12:54 requirements.txt
-rw-r--r-- 1 root root  1.6K Aug  2 18:58 train.py
Removing intermediate container e249ecafd7fa
 ---&gt; dfe9737330ac
Step 13/14 : ENTRYPOINT ["python3"]
 ---&gt; Running in 57782c4a0d03
Removing intermediate container 57782c4a0d03
 ---&gt; 7e6b0d1d5656
Step 14/14 : CMD ["api.py"]
 ---&gt; Running in 8400de10dc2d
Removing intermediate container 8400de10dc2d
 ---&gt; 33733bebf60d
Successfully built 33733bebf60d
Successfully tagged flower-image-classifier:latest


When I run the container, I get the following error:

$ docker run -p 5000:5000 flower-image-classifier
Traceback (most recent call last):
  File "api.py", line 1, in &lt;module&gt;
    from flask import Flask, request, jsonify
ModuleNotFoundError: No module named 'flask'


My question to the Docker/Python experts here: how is it possible that flask is not found, when pip has clearly installed it according to the logs?



Note that I also tried starting from the continuumio/miniconda3 image at first, just like in this issue: Docker Flask ModuleNotFoundError: No module named &#39;flask&#39;

I created this environment.yml:

name: fic
channels:
  - pytorch
  - defaults
dependencies:
  - flask=1.1.1
  - numpy=1.16.4
  - numpy-base=1.16.4
  - pillow=6.1.0
  - python==3.6.9
  - pytorch=1.1.0
  - torchvision=0.3.0
  - pip:
    - pip==19.2.1
prefix: //anaconda3/envs/fic


Then I modified my Dockerfile a bit to extend from that image, and to run this to install the dependencies with Conda:

RUN echo "source activate $(head -1 ./environment.yml | cut -d' ' -f2)" &gt; ~/.bashrc
ENV PATH /opt/conda/envs/$(head -1 ./environment.yml | cut -d' ' -f2)/bin:$PATH


Unfortunately, this downloads &amp; extracts everything every time I build the Docker image, which takes a while and the downloads often randomly fail: CondaError: Downloaded bytes did not match Content-Length.

And when it finally built successfully, I still got the same ModuleNotFoundError for Flask.

What is wrong here? Thanks for reading

you need just to specify the correct Python version, so you need just to change your entrypoint to:

ENTRYPOINT ["python3.5"]

