Skip to content

Commit 240f4b7

Browse files
committed
feed thru changes into 10, 11
1 parent 7138406 commit 240f4b7

File tree

5 files changed

+116
-22
lines changed

5 files changed

+116
-22
lines changed

chapter_09_docker.asciidoc

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,15 @@ Run 'python manage.py migrate' to apply them.
12181218
----
12191219

12201220

1221+
1222+
NOTE: If you don't see this error,
1223+
it's because your source repo already had the database file in it, unlike mine.
1224+
For the sake of argument, run `rm src/db.sqlite` and re-run the build & run commands,
1225+
and you should be able to repro the error. I promise it's instructive!
1226+
1227+
// TODO this ^ is hacky. figure out a way to make it more likely that the user sees the error.
1228+
1229+
12211230
==== Should we run "migrate" inside the Dockerfile? No.
12221231

12231232
So, should we include `manage.py migrate` in our Dockerfile?
@@ -1240,8 +1249,7 @@ CMD /venv/bin/python manage.py runserver
12401249
<1> We run `migrate` using the `--noinput` argument to suppress any little "are you sure" prompts.
12411250

12421251

1243-
1244-
And if we try our FTs again, they all pass!
1252+
If we try our FTs again, they all pass!
12451253

12461254

12471255
[role="small-code"]
@@ -1262,10 +1270,82 @@ OK
12621270

12631271
=== Mounting files inside the container.
12641272

1273+
But we don't actually want to package up our database _inside_ the image, do we?
1274+
We want the database on the server to have totally separate data from the one on our machine.
1275+
1276+
In most deployments, you'd probably be talking to a separate database server, like postgres.
1277+
1278+
For the purposes of this book, the easiest analogy to a server that's "outside" our container,
1279+
is to access the database from the filesystem outside the container.
1280+
1281+
That also gives us a convenient excuse to talk about mounting files in Docker,
1282+
which is a very useful thing to be able to do (TM).
1283+
1284+
1285+
First let's revert our change:
1286+
1287+
[role="sourcecode"]
1288+
.Dockerfile (ch09l009)
1289+
====
1290+
[source,dockerfile]
1291+
----
1292+
[...]
1293+
WORKDIR /src
1294+
1295+
CMD /venv/bin/python manage.py runserver
1296+
----
1297+
====
1298+
1299+
The extra flag to add is `-v`, and it takes a similar `outside:inside` argument as `-p` did for ports.
1300+
We specify a folder or file _outside_ the container, and the path we want it to appear at _inside_ the container.
1301+
1302+
[role="small-code"]
1303+
[subs="specialcharacters,macros"]
1304+
----
1305+
$ pass:quotes[*./src/manage.py migrate --noinput*]
1306+
Operations to perform:
1307+
Apply all migrations: auth, contenttypes, lists, sessions
1308+
Running migrations:
1309+
Applying contenttypes.0001_initial... OK
1310+
[...]
1311+
Applying sessions.0001_initial... OK
1312+
[...]
1313+
$ pass:quotes[*docker build -t superlists . && docker run -p 8888:8888 -v ./src/db.sqlite3:/src/db.sqlite3 -it superlists*]
1314+
----
1315+
1316+
TIP: if you see an error saying: `django.db.utils.OperationalError`: "unable to open database file",
1317+
try stopping the container, `rm -rf src/db.sqlite3`, then re-run the migrate command
1318+
_outside_ the container, and the rebuild and run your image.
1319+
1320+
1321+
And we check the FTs again.
1322+
1323+
[role="small-code"]
1324+
[subs="specialcharacters,macros"]
1325+
----
1326+
$ pass:quotes[*TEST_SERVER=superlists-staging.ottg.eu:8000 ./manage.py test functional_tests \
1327+
--failfast*]
1328+
Found 3 test(s).
1329+
Creating test database for alias 'default'...
1330+
System check identified no issues (0 silenced).
1331+
...
1332+
---------------------------------------------------------------------
1333+
Ran 3 tests in 26.965s
1334+
1335+
OK
1336+
----
12651337

12661338
AMAZING IT ACTUALLY WORKS
12671339

1268-
commit commit commit.
1340+
That's definitely good enough for now! Let's commit.
1341+
1342+
1343+
[subs="specialcharacters,quotes"]
1344+
----
1345+
$ *git add Dockerfile*
1346+
$ *git commit -m"First cut of a Dockerfile"*
1347+
----
1348+
12691349

12701350
Time for a well-earned tea break I think, and perhaps a
12711351
https://en.wikipedia.org/wiki/Digestive_biscuit[chocolate biscuit].
@@ -1274,18 +1354,22 @@ https://en.wikipedia.org/wiki/Digestive_biscuit[chocolate biscuit].
12741354
Success! Our Hack Deployment Works
12751355
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12761356

1277-
Phew. Well, it took a bit of hacking about, but now we can be reassured that
1278-
the basic piping works. Notice that the FT was able to guide us incrementally
1279-
towards a working site.
1357+
Phew. Well, it took a bit of hacking about,
1358+
but now we can be reassured that the basic Docker plumbing works.
1359+
Notice that the FT was able to guide us incrementally towards a working config.
12801360

1281-
But we really can't be using the Django dev server in production, or running on
1282-
port 8888 forever. In the next chapter, we'll make our hacky deployment more
1283-
production-ready.
1361+
But we really can't be using the Django dev server in production,
1362+
or running on port 8888 forever.
1363+
In the next chapter, we'll make our hacky image more production-ready.
12841364

12851365

12861366
.Test-Driving Server Configuration and Deployment
12871367
*******************************************************************************
12881368
1369+
1370+
TODO update this recap.
1371+
1372+
12891373
Tests and small steps some of the uncertainty out of deployment::
12901374
For developers, ops and infra work is always "fun",
12911375
by which I mean a process full of uncertainty and surprises.

chapter_10_production_readiness.asciidoc

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ which is usually a function called `application`.
6464
Django provides one in 'superlists/wsgi.py':
6565

6666
----
67-
CMD ["/venv/bin/gunicorn", "--bind", ":8888", "superlists.wsgi:application"]
67+
CMD ["gunicorn", "--bind", ":8888", "superlists.wsgi:application"]
6868
----
6969

7070
// TODO mention new cmd syntax with ["list"]
@@ -74,7 +74,7 @@ CMD ["/venv/bin/gunicorn", "--bind", ":8888", "superlists.wsgi:application"]
7474
rebuild and rerun our container:
7575

7676
----
77-
podman build -t superlists . && podman run -p 8888:8888 -it superlists
77+
docker build -t superlists . && docker run -p 8888:8888 -v ./src/db.sqlite3:/src/db.sqlite3 -it superlists
7878
----
7979

8080

@@ -231,10 +231,10 @@ Now let's set that environment variable in our Dockerfile using then `ENV` direc
231231
====
232232
[source,dockerfile]
233233
----
234-
RUN /venv/bin/python manage.py migrate
234+
WORKDIR /src
235235
236236
ENV DJANGO_DEBUG_FALSE=1
237-
CMD ["/venv/bin/gunicorn", "--bind", ":8888", "superlists.wsgi:application"]
237+
CMD ["gunicorn", "--bind", ":8888", "superlists.wsgi:application"]
238238
----
239239
====
240240

@@ -255,7 +255,8 @@ For now, we can set it at the command line
255255

256256
[subs="specialcharacters,quotes"]
257257
----
258-
$ *podman build -t superlists . && podman run -p 8888:8888 -e DJANGO_SECRET_KEY=sekrit -it superlists*
258+
$ *docker build -t superlists . && \
259+
docker run -p 8888:8888 -v ./src/db.sqlite3:/src/db.sqlite3 -e DJANGO_SECRET_KEY=sekrit -it superlists*
259260
----
260261

261262

@@ -328,8 +329,10 @@ or on a server, so we'll use the `-e` flag again:
328329

329330
[subs="specialcharacters,quotes"]
330331
----
331-
$ *podman build -t superlists . && \
332-
podman run -p 8888:8888 -e DJANGO_SECRET_KEY=sekrit -e DJANGO_ALLOWED_HOST=localhost -it superlists*
332+
$ *docker build -t superlists . && \
333+
docker run -p 8888:8888 -v ./src/db.sqlite3:/src/db.sqlite3 \
334+
-e DJANGO_SECRET_KEY=sekrit -e DJANGO_ALLOWED_HOST=localhost \
335+
-it superlists*
333336
----
334337

335338

@@ -359,18 +362,25 @@ and instead we need to run `collectstatic`:
359362
====
360363
[source,dockerfile]
361364
----
365+
WORKDIR /src
362366
363-
RUN /venv/bin/python manage.py migrate
364-
RUN /venv/bin/python manage.py collectstatic
367+
RUN python manage.py collectstatic
365368
369+
ENV DJANGO_DEBUG_FALSE=1
370+
CMD ["gunicorn", "--bind", ":8888", "superlists.wsgi:application"]
366371
----
367372
====
368373

369-
that'll get you passing tests. we ahve a container that we're ready to ship to production!
374+
that'll get you passing tests. we have a container that we're ready to ship to production!
370375

371376
find out how in the next exciting installment.
372377

373378

379+
=== TODO: log files
380+
381+
provoke a 500 error somehow and make sure we see tracebacks for it
382+
383+
374384
[role="pagebreak-before less_space"]
375385
.Production-Readiness Config
376386
*******************************************************************************

0 commit comments

Comments
 (0)