Skip to content

Commit 21a7aac

Browse files
committed
some fixes from the wip in the docker test pr
1 parent ac8ffc5 commit 21a7aac

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

chapter_09_docker.asciidoc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -984,18 +984,15 @@ $ pass:quotes[*curl -iv localhost:8888*]
984984
* connect to 127.0.0.1 port 8888 [...]
985985
* Trying [::1]:8888...
986986
* connect to ::1 port 8888 [...]
987-
* Failed to connect to localhost port 8888 after 0 ms: Couldn't connect to
988-
server
987+
* Failed to connect to localhost port 8888 after 0 ms: [...]
989988
* Closing connection
990989
[...]
991-
curl: (7) Failed to connect to localhost port 8888 after 0 ms: Couldn't connect
992-
to server
990+
curl: (7) Failed to connect to localhost port 8888 after 0 ms: [...]
993991
----
994992

995993
TIP: The `-iv` flag to curl is useful for debugging.
996994
It prints verbose output, as well as full HTTP headers.
997995

998-
// CSANAD: my `curl` output is slightly different, but it doesn't change much.
999996

1000997

1001998
=== Running code "inside" the container with docker exec
@@ -1118,7 +1115,7 @@ Similarly, if you try our `curl -iv` (outside the container) once again,
11181115
you'll see the error has changed from "Failed to connect",
11191116
to "Empty reply":
11201117

1121-
[role="ignore-errors pause-first"]
1118+
[role="ignore-errors"]
11221119
[subs="specialcharacters,macros"]
11231120
----
11241121
$ pass:quotes[*curl -iv localhost:8888*]
@@ -1216,7 +1213,6 @@ Starting development server at http://0.0.0.0:8888/
12161213

12171214
We can verify it's working with `curl`:
12181215

1219-
[role="pause-first"]
12201216
[subs="specialcharacters,macros"]
12211217
----
12221218
$ pass:quotes[*curl -iv localhost:8888*]
@@ -1372,7 +1368,7 @@ Starting development server at http://0.0.0.0:8888/
13721368

13731369
...and try our FTs again, they all pass!
13741370

1375-
[role="small-code pause-first"]
1371+
[role="small-code"]
13761372
[subs="specialcharacters,macros"]
13771373
----
13781374
$ pass:quotes[*TEST_SERVER=localhost:8888 ./src/manage.py test src/functional_tests \

chapter_10_production_readiness.asciidoc

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ We'll need to first install Gunicorn into our container,
8080
and then use it instead of `runserver`:
8181

8282

83-
// todo test this
84-
[role="skipme"]
8583
[subs="specialcharacters,quotes"]
8684
----
87-
$ *pip install gunicorn*
85+
$ *python -m pip install gunicorn*
8886
Collecting gunicorn
8987
[...]
90-
Successfully installed gunicorn-21.2.0
88+
Successfully installed gunicorn-22.[...]
9189
----
9290

9391
[[what-is-wsgi]]
@@ -173,8 +171,7 @@ As we run the functional tests, you'll see them warning us of a problem, once ag
173171
The test for adding list items passes happily,
174172
but the test for layout + styling fails. Good job, tests!
175173

176-
// todo: replace pause-first with systematic pause after every docker spinup
177-
[role="small-code pause-first"]
174+
[role="small-code"]
178175
[subs="specialcharacters,macros"]
179176
----
180177
$ pass:quotes[*TEST_SERVER=localhost:8888 python src/manage.py test functional_tests \
@@ -216,7 +213,7 @@ files from Python.
216213

217214
First we install Whitenoise into our local environment:
218215

219-
[role="skipme"]
216+
220217
[subs="specialcharacters,quotes"]
221218
----
222219
*pip install whitenoise*
@@ -315,8 +312,9 @@ I'm sure you'll come across many others.]
315312

316313
The `pip freeze` command will show us everything that's installed in our virtualenv at the moment:
317314

318-
// JAN: This is true only if one installs the libraries locally. So far when following the book we installed them only inside Docker image
319315

316+
// version numbers change too much
317+
[role="skipme"]
320318
[subs="specialcharacters,quotes"]
321319
----
322320
$ *pip freeze*
@@ -351,7 +349,7 @@ Django, Gunicorn and Whitenoise:
351349
[subs="specialcharacters,quotes"]
352350
----
353351
$ *pip freeze | grep -i django*
354-
Django==4.2.11
352+
Django==4.2.[...]
355353
356354
$ *pip freeze | grep -i django >> requirements.txt*
357355
$ *pip freeze | grep -i gunicorn >> requirements.txt*
@@ -361,7 +359,7 @@ $ *pip freeze | grep -i whitenoise >> requirements.txt*
361359
That should give us a requirements.txt file that looks like this:
362360

363361

364-
[role="sourcecode"]
362+
[role="sourcecode skipme"]
365363
.requirements.txt (ch10l004)
366364
====
367365
[source,python]
@@ -410,6 +408,7 @@ TIP: Itamar Turner-Traurig has a great guide to
410408

411409
Now let's see how we use that requirements file in our Dockerfile:
412410

411+
[role="sourcecode"]
413412
.Dockerfile (ch10l005)
414413
====
415414
[source,dockerfile]
@@ -510,7 +509,7 @@ Let them be an inspiration (but not a template) for your own choices!
510509
Note that this if statement replaces the DEBUG and SECRET_KEY lines that are included by default in the settings.py file:
511510

512511
[role="sourcecode"]
513-
.superlists/settings.py (ch10l006)
512+
.src/superlists/settings.py (ch10l006)
514513
====
515514
[source,python]
516515
----
@@ -578,6 +577,7 @@ And try it out...
578577

579578

580579

580+
[role="ignore-errors"]
581581
[subs="specialcharacters,macros"]
582582
----
583583
$ pass:specialcharacters,quotes[*docker build -t superlists . && docker run \
@@ -589,7 +589,7 @@ $ pass:specialcharacters,quotes[*docker build -t superlists . && docker run \
589589
File "/src/superlists/settings.py", line 22, in <module>
590590
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
591591
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
592-
File "<frozen os>", line 685, in __getitem__
592+
[...]
593593
KeyError: 'DJANGO_SECRET_KEY'
594594
----
595595

@@ -673,7 +673,7 @@ Let's use another environment variable for that:
673673

674674

675675
[role="sourcecode"]
676-
.superlists/settings.py (ch10l008)
676+
.src/superlists/settings.py (ch10l008)
677677
====
678678
[source,python]
679679
----
@@ -744,7 +744,7 @@ ENV DJANGO_DEBUG_FALSE=1
744744
CMD gunicorn --bind :8888 superlists.wsgi:application
745745
----
746746
====
747-
// CSANAD: I think it would be important to use a non-privileged user in Docker
747+
748748

749749
// DAVID: It would be nice to explain the difference between RUN and CMD.
750750

@@ -760,9 +760,11 @@ Well, it was fiddly, but that should get us to passing tests after we build & ru
760760
[subs="specialcharacters,quotes"]
761761
----
762762
$ *docker build -t superlists . && docker run \
763-
-p 8888:8888 \
764-
--mount type=bind,source=./src/db.sqlite3,target=/src/db.sqlite3 \
765-
-it superlists*
763+
-p 8888:8888 \
764+
--mount type=bind,source=./src/db.sqlite3,target=/src/db.sqlite3 \
765+
-e DJANGO_SECRET_KEY=sekrit \
766+
-e DJANGO_ALLOWED_HOST=localhost \
767+
-it superlists*
766768
----
767769

768770
and...
@@ -811,6 +813,7 @@ TODO: apologies, WIP, this is definitely a good idea for security, needs writing
811813

812814
Dockerfile should gain some lines a bit like this:
813815

816+
[role="sourcecode skipme"]
814817
.Dockerfile (ch10l0XX)
815818
====
816819
[source,dockerfile]
@@ -868,6 +871,7 @@ image::images/server_error_500.png["A minimal error page saying just Server erro
868871

869872
But if you look in your docker terminal, you'll see there is no traceback:
870873

874+
[role="skipme"]
871875
----
872876
[2024-02-28 10:41:53 +0000] [7] [INFO] Starting gunicorn 21.2.0
873877
[2024-02-28 10:41:53 +0000] [7] [INFO] Listening at: http://0.0.0.0:8888 (7)
@@ -918,10 +922,23 @@ LOGGING = {
918922
----
919923
====
920924

921-
Rebuild and restart our container,
922-
try the FT again (or submitting a new list item manually)
925+
Rebuild and restart our container...
926+
927+
[subs="specialcharacters,quotes"]
928+
----
929+
$ *docker build -t superlists . && docker run \
930+
-p 8888:8888 \
931+
--mount type=bind,source=./src/db.sqlite3,target=/src/db.sqlite3 \
932+
-e DJANGO_SECRET_KEY=sekrit \
933+
-e DJANGO_ALLOWED_HOST=localhost \
934+
-it superlists*
935+
----
936+
937+
Then try the FT again (or submitting a new list item manually)
923938
and we now should see a clear error message:
924939

940+
// TODO: test get from docker logs
941+
[role="skipme"]
925942
----
926943
Internal Server Error: /lists/new
927944
Traceback (most recent call last):
@@ -946,7 +963,7 @@ when considering production-readiness,
946963
we've worked in small steps and used our tests all the way along,
947964
and we're now ready to deploy our container to a real server!
948965

949-
Find out how, in our next exciting installment...
966+
Find out how, in our next exciting instalment...
950967

951968

952969
[role="pagebreak-before less_space"]

0 commit comments

Comments
 (0)