簡體   English   中英

Docker 運行 Flask ImportError: No Module Named Flask

[英]Docker run Flask ImportError: No Module Named Flask

我正在努力完成我的學校作業,即:

  1. https://github.com/aaronjolson/flask-pytest-example
  2. 編寫 Jenkinsfile,Dockerfile -> 在您的 dockerhub 存儲庫中生成一個新標簽
  3. 拉docker
  4. 構建 docker
  5. 運行docker
  6. curl localhost:5000 查看結果

目前我處於第 5 階段,嘗試運行 docker 圖像時出現此錯誤:

Traceback (most recent call last):

File "app.py", line 1, in <module>

from flask import Flask

ImportError: No module named flask

配置:

  1. Jenkins 正在研究 Debian 9
  2. 使用 Windows 客戶端的 Docker 客戶端拉取並運行 docker 映像
  3. 詹金斯文件:
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"
            }
        }
    }
}

  1. Dockerfile:
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"]
  1. 要求.txt
it@debian:~/flask-pytest-example$ cat requirements.txt
flask
pytest
  1. ls -la
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 中的流程是

  1. 從干凈、空的 Python 安裝開始;
  2. 列出所有安裝在那里的包(即什么都沒有)並寫入requirements.txt
  3. 安裝requirements.txt中列出的所有包(即什么都沒有)。

requirements.txt文件應作為應用程序源代碼的一部分簽入,因此如果您將其COPY而不是重新生成它,它將在其中列出包,並且它將是您在測試中使用的包的確切版本非 Docker 虛擬環境。


我可能會將基於 Jenkins 的構建設置作為此序列中的最后一件事。 更好的方法可能是:

  1. 使用普通的 Python 虛擬環境構建您的應用程序; 沒有 Jenkins,沒有 Docker。

     python3 -m venv venv. venv/bin/activate pip install -r requirements.txt pytest./app.py curl http://localhost:5000
  2. 使用上面顯示的 Dockerfile 將構建、工作、測試的 Python 應用程序包裝在 Docker 容器中。

     docker build -t tslaceo/flask-pytest. docker run -p 5000:5000 tslaceo/flask-pytest curl http://localhost:5000
  3. 將此設置包裝在 Jenkins 中。

如果您的應用程序出現問題(通常情況下),虛擬環境設置將更容易調試,您可以使用普通編輯器、IDE、調試器,無需任何特殊設置。 如果您的包裝有問題,那么在本地運行docker build將再次比嘗試在 CI 環境中重現它更容易調試和調整。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM