Skip to content

Commit 9b1f0eb

Browse files
committed
Address some suggestions from david in docker chapter
1 parent da925d8 commit 9b1f0eb

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

chapter_09_docker.asciidoc

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ What do we need to do? Something like this, right?
549549
We create a new file called _Dockerfile_ in the base folder of our repo,
550550
next to the `src/` directory we made earlier:
551551

552+
552553
.Dockerfile (ch09l003)
553554
====
554555
[source,dockerfile]
@@ -586,8 +587,30 @@ CMD python manage.py runserver <4>
586587
<4> Finally the `CMD` instruction tells docker which command you want it to run
587588
by default, when you start a container based on that image.
588589

589-
// deliberately wont work, django not installed
590590

591+
It's probably worth just showing a directory tree,
592+
to make sure everything is in the right place?
593+
All our source code is in a folder called `src`,
594+
next to our `Dockerfile`:
595+
596+
[[tree-with-src-and-dockerfile]]
597+
[subs="specialcharacters,macros"]
598+
----
599+
.
600+
├── Dockerfile
601+
├── db.sqlite3
602+
└── src
603+
├── functional_tests
604+
│   ├── [...]
605+
├── lists
606+
│   ├── [...]
607+
├── manage.py
608+
└── superlists
609+
├── [...]
610+
----
611+
====
612+
613+
// TODO: figure out what to do with the /static folder
591614
592615
593616
==== Docker Build
@@ -634,6 +657,13 @@ superlists latest 7b8e1c9fa68e 13 minutes ago 155MB
634657
635658
636659
660+
NOTE: If you see an error about `failed to solve / compute cache key` and `src: not found`
661+
it may be because you saved the Dockerfile into the wrong place.
662+
Have another look at the directory tree from earlier <<tree-with-src-and-dockerfile>>.
663+
664+
665+
666+
637667
==== Docker Run
638668
639669
Once you've built an image,
@@ -945,7 +975,7 @@ Starting development server at http://127.0.0.1:8888/
945975
946976
==== Debugging Web Server Connectivity With "curl"
947977
948-
A quick check in our browser will show us that nope, that doesn't work either.
978+
A quick run of the FT or check in our browser will show us that nope, that doesn't work either.
949979
Let's try an even lower-level smoke test, the traditional Unix utility `curl`.
950980
It's a command-line tool for making HTTP requests. Try it on your own computer first:
951981
@@ -1044,7 +1074,7 @@ That's definitely some HTML! And the `<title>To-Do lists</title>` looks like it'
10441074
So, we can see Django is serving our site _inside_ the container,
10451075
why can't we see it _outside_??
10461076
1047-
==== Docker Ports Mapping
1077+
==== Docker Port Mapping
10481078
10491079
The pythonspeed guide to Docker's very first section is called
10501080
https://pythonspeed.com/articles/docker-connection-refused/[Connection Refused],
@@ -1074,6 +1104,9 @@ Things clearly aren't working yet.
10741104
.Cannot connect on that port
10751105
image::images/firefox-connection-reset.png["Firefox showing the 'Connection reset' error"]
10761106
1107+
// FT would show this
1108+
// selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=netReset&u=http%3A//localhost%3A8888/&c=UTF-8&d=The%20connection%20to%20the%20server%20was%20reset%20while%20the%20page%20was%20loading.
1109+
10771110
Similarly, if you try our `curl -iv` (outside the container) once again,
10781111
you'll see the error has changed from "Failed to connect",
10791112
to "Empty reply":
@@ -1121,17 +1154,25 @@ Everyone's search results are a little different,
11211154
and mine are perhaps shaped by years of working with Docker and Django,
11221155
but I found the answer in my very first result
11231156
(see <<google-results-screenshot>>),
1124-
which was a https://stackoverflow.com/questions/49476217/docker-cant-access-django-server[stackoverflow post].
1157+
when I searched for "cannot access django runserver inside docker",
1158+
and the result was was a https://stackoverflow.com/questions/49476217/docker-cant-access-django-server[stackoverflow post],
1159+
saying something about needing to specify `0.0.0.0` as the IP address.
11251160
11261161
11271162
[[google-results-screenshot]]
11281163
.Google can still deliver results
11291164
image::images/google-results-with-stackoverflow.png["Google results with a useful stackoverflow post in first position",1000]
11301165
11311166
1132-
So we need to tell Django to bind to _any_ IP address,
1133-
because container networking doesn't always have 127.0.0.1
1134-
as the address of _localhost_:
1167+
We're nearing the edges of my understanding of Docker now,
1168+
but as I understand it, `runserver` binds to `127.0.0.1` by default,
1169+
but that IP address doesn't correspond to the network adapter _inside_
1170+
the container that's actually connected to the outside world,
1171+
via the port mapping we defined earlier.
1172+
1173+
The long and short of it is that
1174+
we need use the long-form `ipaddr:port` version of the `runserver` command,
1175+
using the magic "wilcard" IP address `0.0.0.0`:
11351176
11361177
.Dockerfile (ch09l007)
11371178
====
@@ -1360,10 +1401,12 @@ Running migrations:
13601401
Applying sessions.0001_initial... OK
13611402
$ *docker build -t superlists . && docker run \
13621403
-p 8888:8888 \
1363-
--mount type=bind,source=./src/db.sqlite3,target=/src/db.sqlite3 \
1404+
--mount type=bind,source=./src/db.sqlite3,target=/src/db.sqlite3 \
13641405
-it superlists*
13651406
----
13661407
1408+
// TODO: db.sqlite3 will actually be missing at this point because we did *not* mv it earlier
1409+
13671410
TIP: The old syntax for mounts was `-v`.
13681411
One of the advantages of the new `--mount` syntax is that it will fail hard
13691412
if the path you're trying to mount into the container does not exist
@@ -1419,7 +1462,7 @@ https://en.wikipedia.org/wiki/Digestive_biscuit[chocolate biscuit].
14191462
14201463
Tests and small steps some of the uncertainty out of deployment::
14211464
For developers, ops and infra work is always "fun",
1422-
by which I mean a process full of uncertainty and surprises.
1465+
by which I mean a process full of fear, uncertainty and surprises.
14231466
My aim during this chapter was to show that a step-by-step approach
14241467
helps to minimise risk, especially when allied to a functional test suite
14251468
that can help us to catch errors early.

chapter_10_production_readiness.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ $ *docker build -t superlists . && docker run \
9393
----
9494

9595

96+
97+
98+
9699
==== The FTs catch a problem with static files
97100

98101
As we run the functional tests, you'll see them warning us of a problem, once again.

0 commit comments

Comments
 (0)