You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapter_09_docker.asciidoc
+24-217
Original file line number
Diff line number
Diff line change
@@ -10,144 +10,31 @@ This will be the 9th chapter of the final book. The GitHub repo is available at
10
10
If you have comments about how we might improve the content and/or examples in this book, or if you notice missing material within this chapter, please reach out to the author at [email protected].
* We'll build a minimal Dockerfile with everything we need to run our site.
65
23
66
-
Let me know how you got on, if you get stuck on anything,
67
-
if any explanations don't make sense,
68
-
or if any of the instructions don't work for you.
24
+
* We'll learn how to build and run a container on our machine.
69
25
70
-
I hope you enjoy the new version!
71
-
****
26
+
* We'll make a few changes to our source code layout. like using an _src_ folder.
72
27
73
-
=== The Danger Areas of Deployment
74
-
// XUAN: I read the title as meaning _TDD and_ the danger areas of deployment, but the following section seems to be talking about the danger areas of deployment?
75
-
76
-
Deploying a site to a live web server can be a tricky topic.
77
-
Oft-heard is the forlorn cry __"but it works on my machine!"__
78
-
79
-
((("deployment", "danger areas of")))
80
-
Some of the danger areas of deployment include:
81
-
82
-
Networking::
83
-
Once we're off our own machine, networking issues come in:
84
-
making sure that DNS is routing our domain to the correct IP address for our server,
85
-
making sure our server is configured to listen to traffic coming in from the world,
86
-
making sure it's using the right ports,
87
-
and making sure any firewalls in the way are configured to let traffic through.
88
-
89
-
Dependencies::
90
-
We need to make sure that the packages our software relies on
91
-
(Python, Django, and so on) are installed on the server,
92
-
and have the correct versions.
93
-
94
-
The database::
95
-
There can be permissions and path issues,
96
-
and we need to be careful about preserving data between deploys.
97
-
98
-
Static files (CSS, JavaScript, images, etc.)::
99
-
Web servers usually need special configuration for serving these.
100
-
((("static files", "challenges of")))
101
-
102
-
Security and Configuration::
103
-
Once we're on the public internet,
104
-
we need to worry more about security.
105
-
Various settings that are really useful for local development
106
-
(like the Django debug page)
107
-
become dangerous in production
108
-
(because they expose our source code in tracebacks).
109
-
110
-
Reproducibility and Divergence between local dev and prod::
111
-
All of the above add up to differences between your local development environemnt
112
-
and the way code runs in production.
113
-
We want to be able to reproduce the way things work on our machine,
114
-
as closely as possible, in production (and vice-versa)
115
-
to give us as much confidence as possible that
116
-
"it works on my machine" means "it's going to work in production".
117
-
118
-
119
-
One way to approach the problem is to get a server
120
-
and start manually configuring and installing everything,
121
-
hacking about until it works,
122
-
and maybe think about automating things laterfootnote:[
123
-
This was, more or less, the approach I took in earlier editions of the book.
124
-
With a fair bit of testing thrown in of course.].
125
-
126
-
But if there's one thing we've learned
127
-
in the world of agile/lean software development,
128
-
it's that taking smaller steps usually pays off.
129
-
130
-
How can we take smaller, safer steps towards a production deployment?
131
-
Can we _simulate_ the process of moving to a server
132
-
so that we can iron out all the bugs
133
-
before we actually take the plunge?
134
-
Can we then make small changes one at a time,
135
-
solving problems one by one,
136
-
rather than having to bite off everything in one mouthful?
137
-
Can we use our existing test suite to make sure things
138
-
work on the server, as well as locally?
139
-
140
-
Absolutely we can. And from the title of the chapter,
141
-
I'm sure you're already guessing that Docker is going
142
-
to be part of the answer.
28
+
* We'll start flushing out a few issues around networking and the database.
143
29
144
30
145
31
=== Docker, Containers and Virtualization
146
32
147
33
Docker is a commercial product that wraps several free
148
34
and open source technologies from the world of Linux,
149
35
sometimes referred to as "containerization".
150
-
(Feel free to skip this section if you already know all about Docker.)
36
+
37
+
NOTE: Feel free to skip this section if you already know all about Docker.
151
38
152
39
You may have already heard of the idea of "virtualization",
153
40
which allows a single physical computer to pretend to be several machines.
@@ -176,10 +63,13 @@ So you can run multiple operating systems using separate VMs
176
63
on the same physical box, as in <<virtualization-diagram>>.
177
64
178
65
[[virtualization-diagram]]
179
-
.Virtualenvs vs Virtual Machines
180
-
image::images/virtualenv-vs-vm.png["A diagram showing a virtualenv running in an operating system, vs multiple virtual machines running different operating systems on a single real machine"]
66
+
.Physical vs Virtual Machines
67
+
image::images/virtualenv-vs-vm.png["A diagram showing a physical machine, with an operating system and a Python virtualenv running inside it, vs multiple virtual machines running different operating systems on a single real machine"]
181
68
182
69
70
+
// TODO; remove virtualenvs from this diagram, they just confuse things.
71
+
// add another diagram later to contrast venvs with dockers.
72
+
183
73
Containerization works at the operating system level:
184
74
it gives you multiple virtual operating systems that
185
75
all run on a single real OS.footnote:[
@@ -207,11 +97,6 @@ If you're running Linux containers on a Mac or a PC,
207
97
it's because you're actually running them on a Linux VM under the hood.].
208
98
See <<containers-diagram>> for an illustration.
209
99
210
-
[[containers-diagram]]
211
-
.Containers Share a Kernel in the Host Operating System
212
-
image::images/containers-diagram.png["Diagram showing one or more containers running on a single host operating system, showing that each container uses the kernel from the host OS, but is able to have its own filesystem, based on an image, but also possibly mounting directories from the host filesystem"]
213
-
214
-
215
100
The upshot of this is that containers are much "cheaper".
216
101
You can start one up in milliseconds,
217
102
and you can run hundreds on the same machine.
@@ -224,6 +109,9 @@ NOTE: If you're new to all this, I know it's a lot to wrap your head around!
224
109
Hopefully, following along with these chapters and seeing them working in practice
225
110
will help you to better understand the theory.
226
111
112
+
[[containers-diagram]]
113
+
.Containers Share a Kernel in the Host Operating System
114
+
image::images/containers-diagram.png["Diagram showing one or more containers running on a single host operating system, showing that each container uses the kernel from the host OS, but is able to have its own filesystem, based on an image, but also possibly mounting directories from the host filesystem"]
227
115
228
116
229
117
==== Why not just use a virtualenv?
@@ -246,6 +134,8 @@ and they don't need to understand the intricacies of any particular
246
134
language's packaging systems.
247
135
248
136
137
+
138
+
249
139
==== Docker and your CV
250
140
251
141
That's all well and good for the _theoretical_ justification,
@@ -265,97 +155,14 @@ For many working developers, a container image is the final artifact of their wo
265
155
it's what they "deliver",
266
156
and often the rest of the deployment process is something they rarely have to think about.
267
157
158
+
In any case, without further ado, let's get into it!
268
159
269
160
270
161
271
-
=== An Overview of Our Deployment Procedure
272
-
273
-
Over the next three chapters, I'm going to go through _a_ deployment procedure.
274
-
It isn't meant to be the _perfect_ deployment procedure,
275
-
so please don't take it as being best practice,
276
-
or a recommendation--it's meant to be an illustration,
277
-
to show the kinds of issues involved in putting code into production,
278
-
and where testing fits in.
279
-
280
-
281
-
**This chapter: Containerizing our software**
282
-
283
-
* Adapt our FTs so they can run against a container.
284
-
285
-
* Build a minimal Dockerfile with everything we need to run our site.
286
-
287
-
* Learn how to build and run a container on our machine.
288
-
289
-
* Get a first cut of our code up and running inside Docker,
290
-
with passing tests.
291
-
292
-
293
-
**<<chapter_10_production_readiness,Next chapter>>: Moving to a production-ready configuration**
294
-
295
-
* Gradually, incrementally change the container configuration
296
-
to make it production-ready.
297
-
298
-
* Regularly re-run the FTs to check we didn't break anything.
299
-
300
-
* Address issues to do with the database, static files, secrets, and so on.
301
-
302
-
303
-
**<<chapter_11_server_prep,Third chapter>>: Preparing our staging server and deployment tools**
304
-
305
-
* We'll set up a "staging":footnote:[
306
-
Some people prefer the term pre-prod or test environment.
307
-
It's all the same idea.]
308
-
server, using the same infrastructure a we plan to use for production.
309
-
* Set up a real domain name and point it at this server
310
-
* Install Ansible and flush out any networking issues
311
-
312
-
313
-
**<<chapter_12_ansible,Final chapter>>: Preparing our staging server and deployment tools**
314
-
315
-
* Gradually build up an Ansible playbook to deploy our containers on a real server.
316
-
317
-
* Again, use our FTs to check for any problems.
318
-
319
-
* Learn how to SSH in to the server to debug things,
320
-
where to find logs and other useful information.
321
-
322
-
* Confidently deploy to production once we have a working deployment script for staging.
323
-
324
-
325
-
326
-
=== TDD and Docker vs the Danger Areas of Deployment
327
-
328
-
Hopefully you can start to see how the combination of TDD, Docker, Staging,
329
-
and automation are going to help minimise the risk of the various "Danger Areas".
330
-
331
-
* Containers will act as mini-servers
332
-
letting us flush out issues with dependencies, static files, and so on.
333
-
A key advantage is that they'll give us a way of getting faster feedback cycles,
334
-
because we can spin them up locally very quickly, and make changes quickly.
335
-
336
-
* Our containers will package up both our Python and system dependencies,
337
-
including a production-ready web server and static files system.
338
-
as well as many production settings and configuration differences.
339
-
This minimises the difference between what we can test locally,
340
-
and what we will have on our servers.
341
-
342
-
* Our FTs mean that we'll have a fully automated way of checking
343
-
that everything works.
344
-
345
-
* Later, when we deploy our containers to a staging server,
346
-
we can run the FTs against that too.
347
-
It'll be slightly slower and might involve some fiddly compromises,
348
-
but it'll give us one more layer of reassurance.
349
-
350
-
* Finally, by fully automating container creation and deployment,
351
-
and by testing the end results of both these things,
352
-
we maximise reproducibility, thus minimising the risk of deployment to production.
353
-
354
-
355
162
=== As Always, Start with a Test
356
163
357
164
((("environment variables")))
358
-
Let's adapt our functional tests slightly
165
+
Let's adapt our functional tests
359
166
so that they can run against a standalone server,
360
167
instead of the one that `LiveServerTestCase` creates for us.
361
168
We'll do it by checking for an environment variable
@@ -496,7 +303,7 @@ Currently, all our code is source code really, so we move everything into _src_
496
303
(we'll be seeing some new files appearing outside _src_ shortly).footnote:[
497
304
A common thing to find outside of the _src_ folder is a folder called _tests_.
498
305
We won't be doing that while we're relying on the standard Django test framework,
499
-
but it's a good thing to do if you're using pytest, for example.]
306
+
but it can be a good thing to do if you're using pytest, for example.]
0 commit comments