Skip to content

Commit 83690bf

Browse files
committed
Review chapter 10
1 parent 22bbb84 commit 83690bf

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

chapter_10_production_readiness.asciidoc

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Instead, we'll use the popular Gunicorn Python/WSGI server.
4141
In addition, several options in _settings.py_ are currently unacceptable.
4242
`DEBUG=True`, is strongly recommended against for production,
4343
we'll want to set a unique `SECRET_KEY`,
44-
and as we'll see, other things will come up.
44+
and, as we'll see, other things will come up.
4545

4646
Let's go through and see if we can fix things one by one.
4747

@@ -81,6 +81,8 @@ CMD gunicorn --bind :8888 superlists.wsgi:application
8181
----
8282
====
8383

84+
// DAVID: Does this need a bit more explanation?
85+
8486
As in the previous chapter, we can use the `docker build && docker run`
8587
pattern to try out our changes by rebuilding and rerunning our container:
8688

@@ -92,7 +94,11 @@ $ *docker build -t superlists . && docker run \
9294
-it superlists*
9395
----
9496

95-
97+
// DAVID: Incidentally I got the following error:
98+
// Bind for 0.0.0.0:8888 failed: port is already allocated.
99+
// Turned out the previous container was still running,
100+
// I just used the docker kill process you taught me about earlier.
101+
// Not sure if it's worth including that here, possibly clutter?
96102

97103

98104

@@ -169,6 +175,11 @@ MIDDLEWARE = [
169175
And if you take another manual look at your site, things should look much healthier.
170176
Let's rerun our FTs to confirm:
171177

178+
// DAVID: Incidentally as per your suggestion Harry I have just been skipping
179+
// the requirements.txt and instead just amending the pip install in the Dockerfile.
180+
// If we do that, however, we'll need to make sure readers rebuild the image each time
181+
// we add a requirement - such as at this point.
182+
172183
[role="small-code"]
173184
[subs="specialcharacters,macros"]
174185
----
@@ -182,6 +193,11 @@ Ran 3 tests in 10.718s
182193
OK
183194
----
184195

196+
// DAVID: I got
197+
// UserWarning: No directory at: /Users/david.seddon/Documents/reviewing/goat-book/src/static/
198+
// mw_instance = middleware(adapted_handler)
199+
// I think we need to move the static folder into src too, in the previous chapter.
200+
185201
Phew. Let's commit that
186202

187203
[subs="specialcharacters,quotes"]
@@ -268,7 +284,7 @@ TIP: Better to fail hard than allow a typo in an environment variable name to
268284

269285
==== Setting environment variables inside the Dockerfile
270286

271-
Now let's set that environment variable in our Dockerfile using then `ENV` directive:
287+
Now let's set that environment variable in our Dockerfile using the `ENV` directive:
272288

273289
[role="sourcecode"]
274290
.Dockerfile (ch10l006)
@@ -364,7 +380,7 @@ image::images/search-results-400-bad-request.png["Duckduckgo search results with
364380
`ALLOWED_HOSTS` is a security setting
365381
designed to reject requests that are likely to be forged, broken or malicious
366382
because they don't appear to be asking for your site
367-
(HTTP request contain the address they were intended for in a header called "Host").
383+
(HTTP requests contain the address they were intended for in a header called "Host").
368384

369385
By default, when DEBUG=True, `ALLOWED_HOSTS` effectively allows _localhost_,
370386
our own machine, so that's why it was working OK until now.
@@ -444,6 +460,12 @@ CMD gunicorn --bind :8888 superlists.wsgi:application
444460
----
445461
====
446462

463+
// DAVID: It would be nice to explain the difference between RUN and CMD.
464+
465+
// DAVID: Interestingly when I did this I put the RUN directive after the ENV
466+
// directive, which led to a KeyError: 'DJANGO_SECRET_KEY' which foxed me for a bit.
467+
// Might be worth calling out that we're running collectstatic in debug mode.
468+
447469
Well, it was fiddly, but that should get us to passing tests!
448470

449471

@@ -457,6 +479,9 @@ OK
457479

458480
We have a container that we're ready to ship to production!
459481

482+
// DAVID: It might be worth pointing out what Whitenoise is actually doing.
483+
// From what I understand, we're still using Django to serve static files.
484+
460485
Find out how in the next exciting installment...
461486

462487

@@ -482,7 +507,7 @@ $ *rm src/db.sqlite3 && touch src/db.sqlite3*
482507
TIP: If you use the `-v` option for docker run to mount a nonexistent path,
483508
Docker will create a new folder at that path.
484509
This is why we do the `touch db.sqlite3`
485-
so that we have a placeholder empty file where the datbase should be.
510+
so that we have a placeholder empty file where the database should be.
486511
Otherwise we get a folder instead of an empty file and django gets even more confused;
487512
if you ever see an error saying `chown: permission denied`, that's probably it.
488513
Do a `rm -rf src/db.sqlite3 && touch src/db.sqlite3` to fix it.
@@ -501,6 +526,10 @@ selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
501526
element: [id="id_list_table"]; [...]
502527
----
503528

529+
// DAVID: Got me thinking, I'm not always clear when I need to rebuild the image.
530+
// I would have thought I might need to do it here, but I didn't. Might be worth
531+
// explaining in the previous chapter when we do.
532+
504533
And you might spot in the browser that we just see a minimal error page,
505534
with no debug info (try it manually if you like):
506535

@@ -536,7 +565,7 @@ I mean, yes, separating the concepts of handlers and loggers and filters,
536565
and making it all configurable in a nested hierarchy is all well and good
537566
and covers every possible use case,
538567
but sometimes you just wanna say "just print stuff to stdout pls",
539-
and you wish that configuring the simplest thing was a little easier].
568+
and you wish that configuring the simplest thing was a little easier.].
540569

541570
Here's pretty much the simplest possible logging config
542571
which just prints everything to the console (ie standard out).
@@ -564,7 +593,6 @@ Rebuild and restart our container,
564593
try the FT again (or submitting a new list item manually)
565594
and we now should see a clear error message:
566595

567-
568596
----
569597
Internal Server Error: /lists/new
570598
Traceback (most recent call last):

0 commit comments

Comments
 (0)