[英]Docker run Flask ImportError: No Module Named Flask
我正在努力完成我的學校作業,即:
目前我處於第 5 階段,嘗試運行 docker 圖像時出現此錯誤:
Traceback (most recent call last):
File "app.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
配置:
it@debian:~/flask-pytest-example$ cat Jenkinsfile
pipeline {
environment {
registry = "tslaceo/flask-pytest"
imageName = 'flask-pytest'
registryCred = 'tslaceo'
gitProject = "https://github.com/tslaceo/flask-pytest-example.git"
}
agent any
options {
timeout(time: 1, unit: 'HOURS')
}
stages {
stage ('preparation') {
steps {
deleteDir()
}
}
stage ('get src from git') {
steps {
git 'https://github.com/tslaceo/flask-pytest-example.git'
}
}
stage ('build docker') {
steps {
script {
dockerImage = docker.build registry + ":$BUILD_NUMBER"
}
}
}
stage ('docker publish') {
steps {
script {
docker.withRegistry( '', registryCred ) {
dockerImage.push()
}
}
}
}
stage ('cleaning') {
steps {
sh "docker rmi $registry:$BUILD_NUMBER"
}
}
}
}
it@debian:~/flask-pytest-example$ cat Dockerfile
FROM python
WORKDIR /flask-pytest-example
RUN python --version
RUN pip freeze > requirements.txt
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . .
CMD ["python", "-u", "app.py"]
it@debian:~/flask-pytest-example$ cat requirements.txt
flask
pytest
it@debian:~/flask-pytest-example$ ls -la
total 52
drwxr-xr-x 5 it it 4096 Apr 27 15:28 .
drwxr-xr-x 19 it it 4096 Apr 27 14:42 ..
-rw-r--r-- 1 it it 178 Apr 27 10:32 app.py
-rw-r--r-- 1 it it 202 Apr 27 15:06 Dockerfile
-rw-r--r-- 1 it it 152 Apr 27 12:39 Dockerfile.save
drwxr-xr-x 8 it it 4096 Apr 27 15:06 .git
-rw-r--r-- 1 it it 38 Apr 27 10:32 .gitignore
drwxr-xr-x 2 it it 4096 Apr 27 10:32 handlers
-rw-r--r-- 1 it it 0 Apr 27 10:32 __init__.py
-rw-r--r-- 1 it it 1147 Apr 27 10:48 Jenkinsfile
-rw-r--r-- 1 it it 1071 Apr 27 10:32 LICENSE
-rw-r--r-- 1 it it 491 Apr 27 10:32 README.md
-rw-r--r-- 1 it it 13 Apr 27 10:32 requirements.txt
drwxr-xr-x 2 it it 4096 Apr 27 10:32 tests
刪除RUN pip freeze...
行; 將其替換為COPY requirements.txt.
獲取使用應用程序的 rest 簽入的該文件的副本。
您顯示的 Dockerfile 中的流程是
requirements.txt
;requirements.txt
中列出的所有包(即什么都沒有)。 requirements.txt
文件應作為應用程序源代碼的一部分簽入,因此如果您將其COPY
而不是重新生成它,它將在其中列出包,並且它將是您在測試中使用的包的確切版本非 Docker 虛擬環境。
我可能會將基於 Jenkins 的構建設置作為此序列中的最后一件事。 更好的方法可能是:
使用普通的 Python 虛擬環境構建您的應用程序; 沒有 Jenkins,沒有 Docker。
python3 -m venv venv. venv/bin/activate pip install -r requirements.txt pytest./app.py curl http://localhost:5000
使用上面顯示的 Dockerfile 將構建、工作、測試的 Python 應用程序包裝在 Docker 容器中。
docker build -t tslaceo/flask-pytest. docker run -p 5000:5000 tslaceo/flask-pytest curl http://localhost:5000
將此設置包裝在 Jenkins 中。
如果您的應用程序出現問題(通常情況下),虛擬環境設置將更容易調試,您可以使用普通編輯器、IDE、調試器等,無需任何特殊設置。 如果您的包裝有問題,那么在本地運行docker build
將再次比嘗試在 CI 環境中重現它更容易調試和調整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.