Revision b411e911-a60e-41dd-ae16-9926a2461d51 - Stack Overflow

You can use the following example to run your `flask` application with `uwsgi` and `docker`.

I will provide a minimal example and you can use it to expand to your needs.

The `uwsgi` conf were extracted from [docs][1].

`uwsgi.ini`
```
[uwsgi]
shared-socket = 0.0.0.0:443
https = =0,ssl/server.crt,ssl/server.key
master = true
module = app:app
uid = uwsgi
gid = uwsgi
```

`app.py`
```python
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
 return "Hello, World!"

if __name__ == "__main__":
 app.run() 
```

`requirements.txt`
```txt
Flask
uWSGI==2.0.26
```

`Dockerfile`
```Dockerfile
FROM python:3.10-slim

RUN apt-get update && apt-get install -y \
 build-essential \
 gcc \
 libssl-dev \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

RUN groupadd -r uwsgi && useradd -r -g uwsgi -m uwsgi

WORKDIR /app

COPY requirements.txt /app/

RUN pip install --no-cache-dir -r requirements.txt

COPY . /app/

COPY ssl/ /app/ssl/

RUN chown -R uwsgi:uwsgi /app

EXPOSE 443

USER uwsgi

CMD ["uwsgi", "--ini", "uwsgi.ini"]

```

Create an `ssl` directory and generate a self-signed cert.

```bash
mkdir ssl
openssl req -x509 \
 -newkey rsa:2048 \
 -keyout ssl/server.key \
 -out ssl/server.crt \
 -days 365 -nodes -subj "/CN=localhost"
```

Now you should have this folder structure:

```txt
.
├── app.py
├── Dockerfile
├── requirements.txt
├── ssl
│   ├── server.crt
│   └── server.key
└── uwsgi.ini
```

Now build and run:

```bash
docker build -t flask-uwsgi-example .
docker run --rm --name flask -p 443:443 flask-uwsgi-example
```

And test with curl:

```bash
$ curl -k https://localhost:443
Hello, World!
```


 [1]: https://uwsgi-docs.readthedocs.io/en/latest/HTTPS.html

AltStyle によって変換されたページ (->オリジナル) /