Pip install не удается подтвердить SSL-сертификат: ошибка [SSL: CERTIFICATE_VERIFY_FAILED]

Добрый день, ребята.

Я создаю небольшой образ Python Docker для начинающих, использующих Ubuntu в Windows через WSL. В моем каталоге home/ есть каталог flask-app/ со следующими файлами:

Dockerfile

# our base image
FROM alpine:latest

# Install python and pip
RUN apk add --update py2-pip

# upgrade pip
RUN pip install --upgrade pip

# install Python modules needed by the Python app
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt

# copy files required for the app to run
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/

# tell the port number the container should expose
EXPOSE 5000

# run the application
CMD ["python", "/usr/src/app/app.py"]

app.py

from flask import Flask, render_template
import random

app = Flask(__name__)

# list of cat images
images = [
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26388-1381844103-11.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr01/15/9/anigif_enhanced-buzz-31540-1381844535-8.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26390-1381844163-18.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-1376-1381846217-0.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3391-1381844336-26.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-29111-1381845968-0.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3409-1381844582-13.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr02/15/9/anigif_enhanced-buzz-19667-1381844937-10.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26358-1381845043-13.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-18774-1381844645-6.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-25158-1381844793-0.gif",
    "http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/10/anigif_enhanced-buzz-11980-1381846269-1.gif"
]

@app.route('/')
def index():
    url = random.choice(images)
    return render_template('index.html', url=url)

if __name__ == "__main__":
    app.run(host = "0.0.0.0")

requirements.txt

Flask==1.0

templates/index.html

<html>
  <head>
    <style type = "text/css">
      body {
        background: black;
        color: white;
      }
      div.container {
        max-width: 500px;
        margin: 100px auto;
        border: 20px solid white;
        padding: 10px;
        text-align: center;
      }
      h4 {
        text-transform: uppercase;
      }
    </style>
  </head>
  <body>
    <div class = "container">
      <h4>Cat Gif of the day</h4>
      <img src = "{{url}}" />
      <p><small>Courtesy: <a href = "http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs">Buzzfeed</a></small></p>
    </div>
  </body>
</html>

Когда я пытаюсь создать образ Docker с помощью docker build -t <MY_USERNAME>/myfirstapp ., я получаю следующую ошибку:

alain@testlab:~/flask-app$ docker build -t <MY_USERNAME>/myfirstapp .
[+] Building 3.5s (9/11)                                                                                                                                                                          docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                        0.0s
 => => transferring dockerfile: 566B                                                                                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/alpine:3.5                                                                                                                                               0.8s
 => [internal] load .dockerignore                                                                                                                                                                           0.0s
 => => transferring context: 2B                                                                                                                                                                             0.0s
 => [1/7] FROM docker.io/library/alpine:3.5@sha256:66952b313e51c3bd1987d7c4ddf5dba9bc0fb6e524eed2448fa660246b3e76ec                                                                                         0.0s
 => [internal] load build context                                                                                                                                                                           0.0s
 => => transferring context: 2.46kB                                                                                                                                                                         0.0s
 => CACHED [2/7] RUN apk add --update py2-pip                                                                                                                                                               0.0s
 => [3/7] RUN pip install --upgrade pip                                                                                                                                                                     1.3s
 => [4/7] COPY requirements.txt /usr/src/app/                                                                                                                                                               0.1s
 => ERROR [5/7] RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt                                                                                                                             1.2s
------
 > [5/7] RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt:
0.874 Collecting Flask==1.0 (from -r /usr/src/app/requirements.txt (line 1))
0.949   Could not fetch URL https://pypi.python.org/simple/flask/: There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726) - skipping
0.950   Could not find a version that satisfies the requirement Flask==1.0 (from -r /usr/src/app/requirements.txt (line 1)) (from versions: )
0.951 No matching distribution found for Flask==1.0 (from -r /usr/src/app/requirements.txt (line 1))
------
Dockerfile:12
--------------------
  10 |     # install Python modules needed by the Python app
  11 |     COPY requirements.txt /usr/src/app/
  12 | >>> RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
  13 |
  14 |     # copy files required for the app to run
--------------------
ERROR: failed to solve: process "/bin/sh -c pip install --no-cache-dir -r /usr/src/app/requirements.txt" did not complete successfully: exit code: 1

Я пробовал это решение с флагом --trusted-host и другое решение, но пока у меня та же проблема.

Поскольку некоторые версии TSL устарели в Python, я обязательно обновил pip до последней версии с помощью curl https://bootstrap.pypa.io/get-pip.py | python3.

Это моя текущая версия пипа:

alain@testlab:~/flask-app$ pip --version
pip 24.2 from /home/alain/.local/lib/python3.10/site-packages/pip (python 3.10)

Это мой текущий Python и его версия OpenSSL:

alain@testlab:~/flask-app$ python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 3.0.2 15 Mar 2022

Что может произойти?

Заранее огромное спасибо!

🤔 А знаете ли вы, что...
С Python можно создавать веб-скраперы для извлечения данных из веб-сайтов.


53
1

Ответ:

Решено

Официальный python на Dockerhub включает версии, использующие alpine; это сэкономит вам несколько шагов.

Возможно:

Убедитесь, что вы используете последнюю версию Flask.

requirements.txt:

Flask==3.0.3

Dockerfile:

# our base image
FROM docker.io/python:3.12.5-alpine3.19

# Using WORKDIR is more idiomatic and saves repeated use of the folder
WORKDIR /usr/src/app

# install Python modules needed by the Python app
COPY requirements.txt .
RUN pip install --no-cache-dir -r ./requirements.txt

# copy files required for the app to run
COPY app.py .
COPY templates/index.html ./templates/

# tell the port number the container should expose
EXPOSE 5000

# run the application
CMD ["python", "app.py"]
Q=78871861

docker build \
--tag=stackoverflow:${Q} \
--file=${PWD}/Dockerfile \
${PWD}

docker run \
--interactive --tty --rm \
--publish=5000:5000/tcp \
stackoverflow:${Q}
 * Serving Flask app 'app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://10.0.2.100:5000
Press CTRL+C to quit
10.0.2.100 - - [14/Aug/2024 16:13:35] "GET / HTTP/1.1" 200 -
10.0.2.100 - - [14/Aug/2024 16:13:36] "GET / HTTP/1.1" 200 -
10.0.2.100 - - [14/Aug/2024 16:13:37] "GET / HTTP/1.1" 200 -
10.0.2.100 - - [14/Aug/2024 16:13:38] "GET / HTTP/1.1" 200 -

А затем просмотрите http://localhost:5000