Docker使用不同的python作为系统

我正在按照Docker的入门指南来使用docker和一个python应用程序,但是当docker得到这个命令的时候:

docker run -p 80:80 username/repo:tag 

我收到以下错误信息:

 Traceback (most recent call last): File "app.py", line 1, in <module> from flask import Flask ImportError: No module named flask 

当我运行which flaskwhich python时候,我已经安装了Flask

 /usr/local/bin/flask /usr/local/bin/python 

返回。 当我然而执行sudo pip install Flask ,我得到

 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) 

这显然是一个不同的目录。 我最初的想法是,我从两个不同的目录使用python,这就是为什么我不能运行docker命令。 但我也是一个小白,并不真正知道如何开始故障排除和解决这个问题。 如果有人给我一些指点,我会非常感激。提前感谢。

编辑这是我的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 

不是问题的直接答案,但是这可以为您节省很多时间。

每个docker命令都会为图像添加一个新图层。 在构build图像时,docker工程师将尝试找出需要重新构build的图层。 您可能会在每次构build时更改应用中的文件。 这是第一层,所以你最终不得不在每次构build时安装需求。 这可以增加很多额外的等待。

我们先复制一下requirements.txt然后安装需求。 然后,该层将被caching,直到我们更改要求。

 # 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 

在构builddockerfile时,尝试将其创build的图层可视化,以及如何有助于减less构build时间。

pip install指令应该被打包,并且您可能希望将您的ADD放在WORKDIR之前,也似乎没有正确的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 ?