Skip to content

Commit 7fc56ea

Browse files
committed
better recap in 9, tweak warnings in 9+10, add some footnotes
1 parent b34d02c commit 7fc56ea

File tree

2 files changed

+93
-48
lines changed

2 files changed

+93
-48
lines changed

chapter_09_docker.asciidoc

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33

44
.A Note for Early Release Readers
55
****
6-
With Early Release ebooks, you get books in their earliest form—the author's raw and unedited content as they write—so you can take advantage of these technologies long before the official release of these titles.
7-
8-
This will be the 9th chapter of the final book. The GitHub repo is available at https://github.com/hjwp/book-example/tree/chapter_09_docker.
9-
10-
If you have comments about how we might improve the content and/or examples in this book, or if you notice missing material within this chapter, please reach out to the author at [email protected].
6+
With Early Release ebooks, you get books in their earliest form--the
7+
author's raw and unedited content as they write--so
8+
you can take advantage of these technologies
9+
long before the official release of these titles.
10+
11+
This will be the 9th chapter of the final book.
12+
The GitHub repo is available at https://github.com/hjwp/book-example/tree/chapter_09_docker.
13+
14+
If you have comments about how we might improve the content
15+
and/or examples in this book,
16+
or if you notice missing material within this chapter,
17+
please reach out to the author at [email protected].
1118
****
1219

1320
[quote, Malvina Reynolds]']
@@ -28,6 +35,17 @@ and get those tests passing our code running inside Docker.
2835
* We'll start flushing out a few issues around networking and the database.
2936

3037

38+
.Warning, Fresh Content
39+
****
40+
Just to reinforce on the ER note above,
41+
the content for this chapter is all brand new for the third edition,
42+
so I'm particularly keen on feedback and suggestions for it.
43+
44+
So please hit me up at [email protected], or via
45+
https://github.com/hjwp/Book-TDD-Web-Dev-Python/issues[GitHub Issues]
46+
and Pull Requests.
47+
****
48+
3149
=== Docker, Containers and Virtualization
3250

3351
Docker is a commercial product that wraps several free
@@ -1351,7 +1369,11 @@ $ *echo src/db.sqlite3 >> .dockerignore*
13511369

13521370
Now we rebuild, and try mounting our database file.
13531371
The extra flag to add to the Docker run command is `--mount`,
1354-
where we specify `type=bind`, the `source` path on our machine,
1372+
where we specify `type=bind`, the `source` path on our machine,footnote:[
1373+
If you're wondering about the `$PWD` in the listing,
1374+
it's a special environment variable that represents the current directory.
1375+
The initials echo the `pwd` command, which stands for "print working directory".
1376+
Docker requires mount paths to be absolute paths.]
13551377
and the `target` path _inside_ the container:
13561378

13571379
[subs="specialcharacters,quotes"]
@@ -1369,10 +1391,6 @@ TIP: You're likely to come across the old syntax for mounts, which was `-v`.
13691391
This avoids a lot of pain, ask me how I know this.
13701392

13711393

1372-
NOTE: If you see an error from Docker saying "mount path must be absolute",
1373-
replace the source path with `source="$PWD/src/db.sqlite3"`
1374-
1375-
13761394
[role="small-code"]
13771395
[subs="specialcharacters,macros"]
13781396
----
@@ -1409,26 +1427,34 @@ But first, time for a well-earned tea break I think, and perhaps a
14091427
https://en.wikipedia.org/wiki/Digestive_biscuit#Chocolate_digestives[chocolate biscuit].
14101428

14111429

1412-
.Test-Driving Server Configuration and Deployment
1430+
.Docker Recap
14131431
*******************************************************************************
14141432
1415-
Tests and small steps take some of the uncertainty out of deployment::
1433+
Docker lets us reproduce a server environment on our own machine::
14161434
For developers, ops and infra work is always "fun",
14171435
by which I mean a process full of fear, uncertainty and surprises.
1418-
My aim during this chapter was to show that a step-by-step approach
1419-
helps to minimise risk, especially when allied to a functional test suite
1420-
that can help us to catch errors early.
1421-
1422-
Some typical pain points--networking, ports, static files, and the database::
1423-
Moving from the local django development server to a container
1424-
is a chance to rehearse the fiddliness of configuring networking
1425-
in a deployed environment.
1426-
It's also a chance to think about persistence and the database,
1427-
and some configuration issues like static files.
1428-
1429-
1430-
// TODO: add debugging tips docker ps, docker inspect, docker logs.
1431-
// also brief description of network debugging: try curl outside, try curl inside, restart pc / docker /colima
1432-
// also maybe the lsof command to see who's using what port
1436+
And painfully slow too.
1437+
Docker helps to minimise this pain,
1438+
by giving a mini-server on our own machine,
1439+
that we can trying things out with and get feedback quickly,
1440+
as well as enabling working in small steps.
1441+
1442+
`docker build && docker run`::
1443+
We've learned the core tools for working with Docker.
1444+
The Dockerfile specifies our image, `docker build` builds it,
1445+
and `docker run` runs it.
1446+
`build && run` together give us a "start again from scratch" cycle.
1447+
1448+
Debugging network issues::
1449+
We've seen how to use `curl` both outside and inside the container
1450+
with `docker exec`.
1451+
We've also seen the `-p` argument to bind ports inside and outside,
1452+
and the idea of needing to bind to `0.0.0.0`.
1453+
1454+
Mounting files::
1455+
We've also had a brief intro to mounting files from outside
1456+
the container, into the inside.
1457+
It's an insight into the difference between the "stateless"
1458+
system image, and the stateful world outside of Docker.
14331459
14341460
*******************************************************************************

chapter_10_production_readiness.asciidoc

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33

44
.A Note for Early Release Readers
55
****
6-
With Early Release ebooks, you get books in their earliest form—the author's raw and unedited content as they write—so you can take advantage of these technologies long before the official release of these titles.
7-
8-
This will be the 10th chapter of the final book. The GitHub repo is available at https://github.com/hjwp/book-example.
9-
10-
If you have comments about how we might improve the content and/or examples in this book, or if you notice missing material within this chapter, please reach out to the author at [email protected].
6+
With Early Release ebooks, you get books in their earliest form--the
7+
author's raw and unedited content as they write--so
8+
you can take advantage of these technologies
9+
long before the official release of these titles.
10+
11+
This will be the 10th chapter of the final book.
12+
The GitHub repo is available at https://github.com/hjwp/book-example/tree/chapter_10_production_readiness
13+
14+
If you have comments about how we might improve the content
15+
and/or examples in this book,
16+
or if you notice missing material within this chapter,
17+
please reach out to the author at [email protected].
1118
****
1219

1320
Our container is working fine but it's not production-ready.
@@ -20,14 +27,12 @@ trying to move from working state to working state,
2027
and using the FTs to detect any regressions.
2128

2229

23-
.Warning, chapter under construction
30+
.Warning, Fresh Content
2431
****
25-
As part of my work on the third edition of the book,
26-
I'm making big changes to the deployment chapters.
27-
This chapter is still very fresh, but the content is all there,
28-
so you should be able to follow along.
32+
Just to reinforce on the ER note above,
33+
the content for this chapter is all brand new for the third edition,
34+
so I'm particularly keen on feedback and suggestions for it.
2935
30-
But as always I really, really need feedback.
3136
So please hit me up at [email protected], or via
3237
https://github.com/hjwp/Book-TDD-Web-Dev-Python/issues[GitHub Issues]
3338
and Pull Requests.
@@ -55,7 +60,7 @@ In addition, several options in _settings.py_ are currently unacceptable.
5560
we'll want to set a unique `SECRET_KEY`,
5661
and, as we'll see, other things will come up.
5762

58-
NOTE: DEBUG=True is considered a security risk,
63+
WARNING: DEBUG=True is considered a security risk,
5964
because the django debug page will display sensitive information like
6065
the values of variables, and most of the settings in settings.py.
6166

@@ -66,6 +71,20 @@ Let's go through and see if we can fix things one by one.
6671
// https://docs.djangoproject.com/en/5.0/howto/deployment/
6772
// somewhere later in the chapter for curious readers?
6873

74+
75+
////
76+
HARRY
77+
manage.py check --deploy gives
78+
WARNINGS:
79+
?: (security.W004) You have not set a value for the SECURE_HSTS_SECONDS setting. If your entire site is served only over SSL, you may want to consider setting a value and enabling HTTP Strict Transport Security. Be sure to read the documentation first; enabling HSTS carelessly can cause serious, irreversible problems.
80+
?: (security.W008) Your SECURE_SSL_REDIRECT setting is not set to True. Unless your site should be available over both SSL and non-SSL connections, you may want to either set this setting True or configure a load balancer or reverse-proxy server to redirect all connections to HTTPS.
81+
?: (security.W009) Your SECRET_KEY has less than 50 characters, less than 5 unique characters, or it's prefixed with 'django-insecure-' indicating that it was generated automatically by Django. Please generate a long and random value, otherwise many of Django's security-critical features will be vulnerable to attack.
82+
?: (security.W012) SESSION_COOKIE_SECURE is not set to True. Using a secure-only session cookie makes it more difficult for network traffic sniffers to hijack user sessions.
83+
?: (security.W016) You have 'django.middleware.csrf.CsrfViewMiddleware' in your MIDDLEWARE, but you have not set CSRF_COOKIE_SECURE to True. Using a secure-only CSRF cookie makes it more difficult for network traffic sniffers to steal the CSRF token.
84+
?: (security.W018) You should not have DEBUG set to True in deployment.
85+
?: (security.W020) ALLOWED_HOSTS must not be empty in deployment.
86+
////
87+
6988
=== Switching to Gunicorn
7089

7190
((("production-ready deployment", "using Gunicorn", secondary-sortas="Gunicorn")))
@@ -137,12 +156,11 @@ $ *docker build -t superlists . && docker run \
137156
-it superlists*
138157
----
139158

140-
// DAVID: Incidentally I got the following error:
141-
// Bind for 0.0.0.0:8888 failed: port is already allocated.
142-
// Turned out the previous container was still running,
143-
// I just used the docker kill process you taught me about earlier.
144-
// Not sure if it's worth including that here, possibly clutter?
145-
159+
TIP: If you see an error saying
160+
`Bind for 0.0.0.0:8888 failed: port is already allocated.`,
161+
it'll be because you still have a container running from the previous chapter.
162+
Do you remember how to use `docker ps`, and `docker stop`?
163+
Otherwise, skip back to <<how-to-stop-a-docker-container>>.
146164

147165
==== The FTs catch a problem with static files
148166

@@ -197,7 +215,10 @@ First we install Whitenoise into our local environment:
197215
*pip install whitenoise*
198216
----
199217

200-
Then we tell Django to enable it, in _settings.py_:
218+
Then we tell Django to enable it, in _settings.py_footnote:[
219+
Find out more about Django Middleware
220+
in https://docs.djangoproject.com/en/5.1/topics/http/middleware/[the docs].
221+
]:
201222

202223
[role="sourcecode"]
203224
.src/superlists/settings.py (ch10l002)
@@ -212,8 +233,6 @@ MIDDLEWARE = [
212233
213234
----
214235
====
215-
// CSANAD: I would add a few thoughts on the significance of the order of
216-
// middlewares.
217236

218237
And then we need to add it to our pip installs in the Dockerfile:
219238

0 commit comments

Comments
 (0)