@@ -47,7 +47,7 @@ Instead, we'll use the popular Gunicorn Python/WSGI server.
47
47
In addition, several options in _settings.py_ are currently unacceptable.
48
48
`DEBUG=True`, is strongly recommended against for production,
49
49
we'll want to set a unique `SECRET_KEY`,
50
- and as we'll see, other things will come up.
50
+ and, as we'll see, other things will come up.
51
51
52
52
Let's go through and see if we can fix things one by one.
53
53
@@ -104,6 +104,12 @@ $ *docker build -t superlists . && docker run \
104
104
-it superlists*
105
105
----
106
106
107
+ // DAVID: Incidentally I got the following error:
108
+ // Bind for 0.0.0.0:8888 failed: port is already allocated.
109
+ // Turned out the previous container was still running,
110
+ // I just used the docker kill process you taught me about earlier.
111
+ // Not sure if it's worth including that here, possibly clutter?
112
+
107
113
108
114
==== The FTs catch a problem with static files
109
115
@@ -192,6 +198,12 @@ $ *docker build -t superlists . && docker run \
192
198
And if you take another manual look at your site, things should look much healthier.
193
199
Let's rerun our FTs to confirm:
194
200
201
+ // DAVID: Incidentally as per your suggestion Harry I have just been skipping
202
+ // the requirements.txt and instead just amending the pip install in the Dockerfile.
203
+ // If we do that, however, we'll need to make sure readers rebuild the image each time
204
+ // we add a requirement - such as at this point.
205
+
206
+
195
207
// JAN: That doesn't work until you install whitenoise to venv on local machine
196
208
197
209
[role="small-code"]
@@ -208,6 +220,12 @@ Ran 3 tests in 10.718s
208
220
OK
209
221
----
210
222
223
+ // DAVID: I got
224
+ // UserWarning: No directory at: /Users/david.seddon/Documents/reviewing/goat-book/src/static/
225
+ // mw_instance = middleware(adapted_handler)
226
+ // I think we need to move the static folder into src too, in the previous chapter.
227
+
228
+
211
229
Phew. Let's commit that:
212
230
213
231
[subs="specialcharacters,quotes"]
@@ -463,7 +481,7 @@ TIP: Better to fail hard than allow a typo in an environment variable name to
463
481
464
482
==== Setting environment variables inside the Dockerfile
465
483
466
- Now let's set that environment variable in our Dockerfile using then `ENV` directive:
484
+ Now let's set that environment variable in our Dockerfile using the `ENV` directive:
467
485
468
486
[role="sourcecode"]
469
487
.Dockerfile (ch10l007)
@@ -560,7 +578,7 @@ image::images/search-results-400-bad-request.png["Duckduckgo search results with
560
578
`ALLOWED_HOSTS` is a security setting
561
579
designed to reject requests that are likely to be forged, broken or malicious
562
580
because they don't appear to be asking for your site
563
- (HTTP request contain the address they were intended for in a header called "Host").
581
+ (HTTP requests contain the address they were intended for in a header called "Host").
564
582
565
583
By default, when DEBUG=True, `ALLOWED_HOSTS` effectively allows _localhost_,
566
584
our own machine, so that's why it was working OK until now.
@@ -641,6 +659,13 @@ CMD gunicorn --bind :8888 superlists.wsgi:application
641
659
----
642
660
====
643
661
662
+ // DAVID: It would be nice to explain the difference between RUN and CMD.
663
+
664
+ // DAVID: Interestingly when I did this I put the RUN directive after the ENV
665
+ // directive, which led to a KeyError: 'DJANGO_SECRET_KEY' which foxed me for a bit.
666
+ // Might be worth calling out that we're running collectstatic in debug mode.
667
+
668
+
644
669
// TODO: gitignore src/static
645
670
646
671
Well, it was fiddly, but that should get us to passing tests!
657
682
658
683
We're nearly ready to ship to production!
659
684
685
+ // DAVID: It might be worth pointing out what Whitenoise is actually doing.
686
+ // From what I understand, we're still using Django to serve static files.
687
+
660
688
Let's quickly adjust our gitignore, since the static folder is in a new place:
661
689
662
690
//0010
@@ -673,7 +701,7 @@ $ *git commit -am "Add collectstatic to dockerfile, and new location to gitignor
673
701
674
702
=== Switching to a nonroot user
675
703
676
- TODO: this is definitely a good idea for security, needs writing up.
704
+ TODO: WIP, this is definitely a good idea for security, needs writing up.
677
705
678
706
Dockerfile should gain some lines a bit like this:
679
707
@@ -707,7 +735,6 @@ $ *rm src/db.sqlite3*
707
735
$ *touch src/db.sqlite3* # otherwise the --mount type=bind will complain
708
736
----
709
737
710
-
711
738
Now if you run the tests, you'll see they fail;
712
739
713
740
[role="small-code"]
@@ -721,6 +748,10 @@ selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
721
748
element: [id="id_list_table"]; [...]
722
749
----
723
750
751
+ // DAVID: Got me thinking, I'm not always clear when I need to rebuild the image.
752
+ // I would have thought I might need to do it here, but I didn't. Might be worth
753
+ // explaining in the previous chapter when we do.
754
+
724
755
And you might spot in the browser that we just see a minimal error page,
725
756
with no debug info (try it manually if you like):
726
757
@@ -756,7 +787,7 @@ I mean, yes, separating the concepts of handlers and loggers and filters,
756
787
and making it all configurable in a nested hierarchy is all well and good
757
788
and covers every possible use case,
758
789
but sometimes you just wanna say "just print stuff to stdout pls",
759
- and you wish that configuring the simplest thing was a little easier].
790
+ and you wish that configuring the simplest thing was a little easier. ].
760
791
761
792
Here's pretty much the simplest possible logging config
762
793
which just prints everything to the console (ie standard out).
@@ -784,7 +815,6 @@ Rebuild and restart our container,
784
815
try the FT again (or submitting a new list item manually)
785
816
and we now should see a clear error message:
786
817
787
-
788
818
----
789
819
Internal Server Error: /lists/new
790
820
Traceback (most recent call last):
0 commit comments