我正在遵循使用docker和python应用程序的Docker-getting started指南,但是当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
我在运行Flask
和which flask
which python
/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
答案 0 :(得分:1)
不是问题的直接答案,但这可以为您节省大量时间。
每个docker命令都会向图像添加一个新图层。在构建图像时,docker将尝试找出需要重新构建的图层。您可能会在每次构建时更改应用程序中的文件。这是第一层,因此您每次构建时都必须安装需求。这可以增加额外的等待。
让我们复制requirements.txt
并首先安装要求。然后,在我们更改要求之前,将缓存该层。
# 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
构建dockerfile时,尝试可视化它创建的图层以及这将有助于减少构建时间。
答案 1 :(得分:0)
应该包含pip install
指令,您可能希望将ADD
放在WORKDIR
之前,而且您似乎没有正确的ENTRYPOINT
< / p>
# 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 ?