@@ -1218,6 +1218,15 @@ Run 'python manage.py migrate' to apply them.
1218
1218
----
1219
1219
1220
1220
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
+
1221
1230
==== Should we run "migrate" inside the Dockerfile? No.
1222
1231
1223
1232
So, should we include `manage.py migrate` in our Dockerfile?
@@ -1240,8 +1249,7 @@ CMD /venv/bin/python manage.py runserver
1240
1249
<1> We run `migrate` using the `--noinput` argument to suppress any little "are you sure" prompts.
1241
1250
1242
1251
1243
-
1244
- And if we try our FTs again, they all pass!
1252
+ If we try our FTs again, they all pass!
1245
1253
1246
1254
1247
1255
[role="small-code"]
@@ -1262,10 +1270,82 @@ OK
1262
1270
1263
1271
=== Mounting files inside the container.
1264
1272
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
+ ----
1265
1337
1266
1338
AMAZING IT ACTUALLY WORKS
1267
1339
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
+
1269
1349
1270
1350
Time for a well-earned tea break I think, and perhaps a
1271
1351
https://en.wikipedia.org/wiki/Digestive_biscuit[chocolate biscuit].
@@ -1274,18 +1354,22 @@ https://en.wikipedia.org/wiki/Digestive_biscuit[chocolate biscuit].
1274
1354
Success! Our Hack Deployment Works
1275
1355
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1276
1356
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 .
1280
1360
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.
1284
1364
1285
1365
1286
1366
.Test-Driving Server Configuration and Deployment
1287
1367
*******************************************************************************
1288
1368
1369
+
1370
+ TODO update this recap.
1371
+
1372
+
1289
1373
Tests and small steps some of the uncertainty out of deployment::
1290
1374
For developers, ops and infra work is always "fun",
1291
1375
by which I mean a process full of uncertainty and surprises.
0 commit comments