Skip to content

Commit f7ca3aa

Browse files
committed
feat(docker): add production docker lecture
Keep information on how to run locally using Flask dev server and volumes
1 parent 4240662 commit f7ca3aa

File tree

7 files changed

+70
-0
lines changed

7 files changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Run our Flask app with gunicorn in Docker
2+
3+
Throughout the course, we've been working with a Docker image like this one:
4+
5+
```dockerfile
6+
FROM python:3.10
7+
EXPOSE 5000
8+
WORKDIR /app
9+
COPY requirements.txt .
10+
RUN pip install -r requirements.txt
11+
COPY . .
12+
CMD ["flask", "run", "--host", "0.0.0.0"]
13+
```
14+
15+
This is all well and good for local development, but when we deploy our application we want to run it with the best performance possible.
16+
17+
This is why we don't want to run the Flask development server and the Flask debugger. Instead, we'll use gunicorn to run our app.
18+
19+
## Run our Flask app with gunicorn
20+
21+
First let's add `gunicorn` to our `requirements.txt` file:
22+
23+
```text title="requirements.txt"
24+
flask
25+
flask-smorest
26+
python-dotenv
27+
sqlalchemy
28+
flask-sqlalchemy
29+
flask-jwt-extended
30+
passlib
31+
flask-migrate
32+
# highlight-start
33+
gunicorn
34+
# highlight-end
35+
```
36+
37+
Then, let's change our `Dockerfile` to use `gunicorn`:
38+
39+
```dockerfile
40+
FROM python:3.10
41+
WORKDIR /app
42+
COPY ./requirements.txt requirements.txt
43+
# highlight-start
44+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
45+
# highlight-end
46+
COPY . .
47+
# highlight-start
48+
CMD ["gunicorn", "--bind", "0.0.0.0:80", "app:create_app()"]
49+
# highlight-end
50+
```
51+
52+
The `CMD` line change is the important one, as it runs `gunicorn` on port `80`, and we pass in the app factory function.
53+
54+
:::tip
55+
Note I've also changed the `pip install` line. Adding `--no-cache-dir` and `--upgrade` just makes sure we can't accidentally install from a cache directory (which shouldn't exist anyway!), and that we'll upgrade to the latest possible versions allowed by our `requirements.txt` file.
56+
:::
57+
58+
## Run the Docker container locally with the Flask development server and debugger
59+
60+
If you use this `Dockerfile`, it doesn't mean you can't run it locally using the Flask development server. You don't have to lose the automatic restarting capabilities, or the Flask debugger.
61+
62+
To run the Docker container locally, you'll have to do this from now on:
63+
64+
```zsh
65+
docker run -dp 5000:5000 -w /app -v "$(pwd):/app" teclado-site-flask sh -c "flask run"
66+
```
67+
68+
This is similar to how we've ran the Docker container with our local code as a volume (that's what `-w /app -v "$(pwd):/app"` does), but at the end of the command we're telling the container to run `flask run` instead of the `CMD` line of the `Dockerfile`. That's what `sh -c "flask run"` does!
69+
70+
Now you're ready to commit and push this to your repository and re-deploy to Render.com!

0 commit comments

Comments
 (0)