Latest / CyberCode Academy / Course 31 - Dive Into Docker | Episode 6: A Hands-On Guide to Dockerizing Web Applications
Transcript
- 0:00Speaker 1: Have you ever just sat there staring at your monitor
- 0:03Speaker 1: waiting minutes or I mean maybe even tens of minutes
- 0:07Speaker 1: for a tiny single line code change to compile.
- 0:10Speaker 2: Oh yeah, watching that progress bar just crawl.
- 0:13Speaker 1: Right and you're sitting there knowing full well it should
- 0:16Speaker 1: take milliseconds. It just completely breaks your flow. You know,
- 0:19Speaker 1: it disrupts the entire development life cycle and frankly, it
- 0:22Speaker 1: wastes a ton of time.
- 0:23Speaker 2: It's honestly a massive bottleneck in software engineering. Like when
- 0:27Speaker 2: you look at modern development pipelines, whether you're deploying cybersecurity tools,
- 0:32Speaker 2: or training AI models, or just pushing a standard web app, exactly,
- 0:36Speaker 2: efficiency at that compilation layer is just critical. You really
- 0:39Speaker 2: need your environment to be fast, predictable, and totally isolated.
- 0:44Speaker 1: Well, that is exactly our mission for this deep dive today.
- 0:47Speaker 1: We're dissecting the hidden mechanics of Docker.
- 0:50Speaker 2: Yeah, the real underlying engine.
- 0:52Speaker 1: We've pulled together all the foundational concepts to conceptually build
- 0:55Speaker 1: a production grade web application from scratch, literally line by line.
- 1:00Speaker 1: We're going to look at the mechanisms that actually eliminate
- 1:02Speaker 1: those excruciating wait times, and.
- 1:04Speaker 2: We're going to contextualize this using a specific micro services architecture.
- 1:08Speaker 2: So imagine you're building a Python Flask web app.
- 1:12Speaker 1: Pretty common scenario, very common.
- 1:14Speaker 2: Its function is just to serve a web page displaying
- 1:17Speaker 2: a counter. Every time you reload the page, the counter increments,
- 1:21Speaker 2: and the state of that counter is saved in reddis.
- 1:23Speaker 1: Which is functioning as an in memory key value data store.
- 1:26Speaker 1: Right exactly, And to be completely clear here, this isn't
- 1:29Speaker 1: some crappy toy example. The Flask app and the reddest
- 1:32Speaker 1: data store will run in entirely separate, isolated containers.
- 1:37Speaker 2: Yeah, talking over a dedicated Docker network, right.
- 1:40Speaker 1: We are treating this as a real world deployment. You
- 1:42Speaker 1: don't even need prior Flask or reddest knowledge to follow along.
- 1:46Speaker 2: No, not at all. The focus here is entirely on
- 1:48Speaker 2: mastering the Docker file itself. That file is your blueprint.
- 1:52Speaker 2: It dictates exactly how the Docker engine constructs the image
- 1:55Speaker 2: your app will live inside.
- 1:56Speaker 1: Okay, let's unpack this because every blueprint needs a foundation.
- 2:00Speaker 1: Right before we start shoving our Python codebase into a container,
- 2:04Speaker 1: we have to establish that base environment.
- 2:06Speaker 2: You do, and Docker files are passed strictly top to bottom,
- 2:09Speaker 2: so you always start with the from instruction.
- 2:12Speaker 1: Right, which imports a base image, and honestly, the strategy
- 2:15Speaker 1: behind the versioning tags here is where I see a
- 2:18Speaker 1: lot of teams make their very first architectural mistake.
- 2:21Speaker 2: Oh absolutely, So you append a tag to the image
- 2:25Speaker 2: name using a colon. You could lock down your image
- 2:27Speaker 2: to a highly specific tag like Python two point seven
- 2:31Speaker 2: point one three, But.
- 2:32Speaker 1: The consensus in enterprise environments is usually to lean away
- 2:36Speaker 1: from pinning that exact patch version, isn't it like teams
- 2:39Speaker 1: prefer binning just the minor version, So just Python point
- 2:42Speaker 1: two point seven.
- 2:43Speaker 2: Yeah, And the reason for that is automated security patching.
- 2:45Speaker 2: If you lock it to two point seven, the Docker
- 2:47Speaker 2: engine just automatically pulls the latest patch release within that
- 2:51Speaker 2: specific branch every time you run a fresh build, which is.
- 2:54Speaker 1: Huge for security because patch releases are designed to just
- 2:56Speaker 1: contain bug fixes and critical security patches, no breaking it changes.
- 3:00Speaker 2: Right, So if a zero day vulnerability drops in the
- 3:02Speaker 2: Python runtime and version two point seven two zero comes out,
- 3:06Speaker 2: your next automated pipeline bill just pulls.
- 3:08Speaker 1: The secure version without you having to manually go in
- 3:11Speaker 1: and refactor Docker files across like one hundred different repositors exactly.
- 3:15Speaker 2: It's a massive operational benefit but the tag doesn't just
- 3:20Speaker 2: specify the language version, it also specifies the underlying operating
- 3:24Speaker 2: system variants.
- 3:25Speaker 1: Right, because historically images defaulted to Debian distributions which are
- 3:29Speaker 1: stable but just incredibly bloated, super.
- 3:32Speaker 2: Bloated, and then you have the slim variants which strip
- 3:35Speaker 2: out non essential packages. But for our architecture, we really
- 3:38Speaker 2: want to use Python Dot two point seven dash Alpine.
- 3:42Speaker 1: Alpine Linux is so fascinating to me when you look
- 3:44Speaker 1: at the internals, because like, if you start with the
- 3:47Speaker 1: scratch image in Docker, you literally have an empty file system,
- 3:51Speaker 1: no shell, no utilities, nothing, your emptiness, right, and Debian
- 3:54Speaker 1: is on the complete opposite end. But Alpine hits this
- 3:57Speaker 1: sweet spot by replacing the massive vnuc libry with muzzle
- 4:01Speaker 1: Limb and swapping standard utilities for BusyBox.
- 4:04Speaker 2: The footprint reduction there is drastic. You're shrinking the base
- 4:07Speaker 2: image from hundreds of megabytes down to like five megabise. Yeah,
- 4:11Speaker 2: and that minimal size obviously accelerates your deployment speed, but
- 4:15Speaker 2: more importantly, from a cybersecurity perspective, it drastically minimizes your
- 4:19Speaker 2: attack surface.
- 4:21Speaker 1: Because fewer installed libraries just means fewer potential vectors for
- 4:24Speaker 1: exploitation exactly.
- 4:26Speaker 2: So our foundation is set with from Python dot two
- 4:29Speaker 2: point seven dash Alpine. Next, we need to organize the
- 4:32Speaker 2: file system. We use run mcut ear app to make
- 4:35Speaker 2: a dedicated directory.
- 4:37Speaker 1: Right because the ru and instruction executes standard shell commands
- 4:40Speaker 1: inside the image during the build process.
- 4:42Speaker 2: Yeah, and creating this app directory ensures our code is
- 4:45Speaker 2: physically separated from system level binaries. It prevents weird path collisions, so.
- 4:49Speaker 1: Naturally, moving into that directory requires changing paths. In a
- 4:54Speaker 1: normal Linux terminal, you just type cd app. But you're
- 4:56Speaker 1: actually not supposed to use runcd app in a Docker
- 4:59Speaker 1: file right now.
- 5:00Speaker 2: Oh, definitely don't do that, And the reason comes down
- 5:02Speaker 2: to how Docker processes instructions. Every single run command actually
- 5:07Speaker 2: spawns a temporary ephemeral container.
- 5:09Speaker 1: Wait, really a whole new container just for that line.
- 5:12Speaker 2: Yeah, it creates, it executes, the command, commits to the
- 5:15Speaker 2: result to a new layer, and then destroys that temporary container.
- 5:19Speaker 1: Always. See, so if you run cd app you just
- 5:22Speaker 1: change the directory inside an ephemeral container that immediately gets
- 5:25Speaker 1: deleted exactly.
- 5:25Speaker 2: The very next instruction will just drop you right back
- 5:28Speaker 2: into the root directory.
- 5:29Speaker 1: So to fix that and establish persistent state, we use
- 5:32Speaker 1: the work dear instruction.
- 5:33Speaker 2: Right, executing workdare app tells the Docker demon that for
- 5:37Speaker 2: every subsequent instruction and crucially for the actual run time
- 5:41Speaker 2: execution later, the current working directory is permanently app.
- 5:45Speaker 1: Okay, that makes sense. It's a clean context. Now that
- 5:48Speaker 1: we have our room built, so to speak, we have
- 5:51Speaker 1: to actually transfer our application codebase from our local machine
- 5:55Speaker 1: into the container.
- 5:57Speaker 2: Yeah, and that utilizes the copy wy instruction. It just
- 5:59Speaker 2: takes us source path from your machine and a destination
- 6:02Speaker 2: path inside the image.
- 6:03Speaker 1: But this introduces a really strict rule about context enforcement,
- 6:07Speaker 1: doesn't it. When you trigger a build, docer takes the
- 6:09Speaker 1: directory containing the Docker file, packages it into a tarball
- 6:13Speaker 1: and sends it to the demon.
- 6:15Speaker 2: The build context. And that mechanism is exactly why you
- 6:19Speaker 2: cannot copy files that live above your Docker files directory.
- 6:22Speaker 1: Right. The demon literally can't see them. It only knows
- 6:25Speaker 1: what's in that carbol exactly.
- 6:27Speaker 2: It forces security and make sure the build is actually
- 6:30Speaker 2: reproducible anywhere.
- 6:31Speaker 1: Okay, knowing that we execute our first transfer. But instead
- 6:35Speaker 1: of copying the entire codebase, we're only going to bring
- 6:38Speaker 1: over one single file copyy requirements dot txt.
- 6:43Speaker 2: Just using the dot to represent the current working directory,
- 6:46Speaker 2: since work deer already set it to app.
- 6:47Speaker 1: Right. And here's where it gets really interesting. Why are
- 6:50Speaker 1: we fracturing the copy process? Why not just copy everything
- 6:53Speaker 1: at once and then install dependencies.
- 6:55Speaker 2: Yeah, this is the secret, sauce. To understand the genius here,
- 6:59Speaker 2: you have to look at the Union file.
- 7:00Speaker 1: System, the real engine of Docker's efficiency.
- 7:03Speaker 2: Exactly, every single instruction we just typed from Workdeer, copy
- 7:07Speaker 2: run creates a separate, independent layer, and Union FS stacks
- 7:12Speaker 2: these layers.
- 7:13Speaker 1: And during the bill process, these layers are strictly read only,
- 7:16Speaker 1: right yes.
- 7:18Speaker 2: And because they're read only, Docker cashes them cryptographically. It
- 7:22Speaker 2: takes a SHA two five six hash of the instruction
- 7:25Speaker 2: and the files involved.
- 7:26Speaker 1: So if you rebuild, Docker checks the hash of the
- 7:29Speaker 1: new requirements dot txt file against the cast layer. If
- 7:33Speaker 1: it matches, it skips the execution and instantly loads the cash.
- 7:37Speaker 2: But and this is the big butt, there is a
- 7:39Speaker 2: strict cascading cash invalidation protocol. If a hash does not match,
- 7:44Speaker 2: meaning you alter a file, Docker invalidates that layer, and.
- 7:48Speaker 1: It invalidates every single layer below it in the stack too.
- 7:51Speaker 2: Every single one. It forces a complete rebuild from that
- 7:54Speaker 2: point down.
- 7:55Speaker 1: Okay, let's ground this with an analogy. Think of packing
- 7:57Speaker 1: a suitcase. You put your heavy, bulky winter coats the
- 8:00Speaker 1: very bottom and your light T shirts at the top.
- 8:02Speaker 2: I like that.
- 8:03Speaker 1: In our app, the massive third party libraries we download
- 8:06Speaker 1: from the internet, those are the winter codes we rarely
- 8:08Speaker 1: change them. But our source code, the stuff we edit daily,
- 8:11Speaker 1: those are the T shirts. Right.
- 8:13Speaker 2: If you pack the T shirts at the bottom, replacing
- 8:15Speaker 2: one shirt means unpacking all the heavy coats exactly.
- 8:18Speaker 1: So if we ran copy y first to bring in
- 8:21Speaker 1: all our code and then ran pip.
- 8:23Speaker 2: Install, the cash becomes a huge liability. You change one
- 8:26Speaker 2: variable in your Python code, the hash for the copyy
- 8:29Speaker 2: layer changes and the cash is broken.
- 8:32Speaker 1: And then the pip install layer below it gets invalidated too,
- 8:35Speaker 1: so you end up waiting ten minutes to re download
- 8:37Speaker 1: gigabytes of packages over a single typo fix.
- 8:40Speaker 2: It's rage inducing, but by restructuring it you isolate the volatility.
- 8:45Speaker 2: You copy the static requirements dot txt first, you run
- 8:49Speaker 2: the heavy installation, locking those files into a cashed layer, and.
- 8:53Speaker 1: Then we copy the highly volatile source code at the very.
- 8:55Speaker 2: End, YEP, you hit save, Docker checks the requirements layer,
- 8:58Speaker 2: cash hit checks the install layer cache, hit reaches the
- 9:01Speaker 2: final code, copy sees the change, breaks the cash, and
- 9:04Speaker 2: copies a text file in a millisecond.
- 9:06Speaker 1: Oh wait, times eliminated. That's just brilliant architecture, it really is.
- 9:10Speaker 2: But you know, the life cycle of a container doesn't
- 9:12Speaker 2: end on your laptop. Once this image is pushed to
- 9:15Speaker 2: production registry, orchestration tools need to manage it.
- 9:18Speaker 1: Which transitions us to metadata, and we add that using
- 9:21Speaker 1: the label instruction. It expects a key value pair, right
- 9:25Speaker 1: like label version one point zero exactly.
- 9:27Speaker 2: In a clustered environment with hundreds of microservices, metadata is
- 9:32Speaker 2: how you manage the chaos. Load balancers and orchestration tools
- 9:36Speaker 2: actively query those labels to route traffic or trigger rollbacks.
- 9:40Speaker 1: But there's a nuance here about layer bloat. Since every
- 9:43Speaker 1: instruction creates a new layer. Putting five label commands creates
- 9:48Speaker 1: five separate layers.
- 9:49Speaker 2: Right yeah, which slightly increases your parsing time and footprint.
- 9:52Speaker 2: To fix that, you just chain them. You declare label once,
- 9:56Speaker 2: provide the first pair, hit a space, and provide the next,
- 9:59Speaker 2: or use.
- 9:59Speaker 1: It backslash at the end of the line if it
- 10:01Speaker 1: gets visually messy.
- 10:02Speaker 2: Exactly, the backslash tells the shell that the command continues
- 10:06Speaker 2: on the next line. It keeps your file readable, but
- 10:08Speaker 2: compresses everything into one optimized layer.
- 10:11Speaker 1: So our image is optimized, structural, and tagged. The last
- 10:14Speaker 1: thing is telling the container what to actually do when
- 10:17Speaker 1: it turns.
- 10:17Speaker 2: On, which brings up the critical difference between the run
- 10:20Speaker 2: instruction and the CMD instruction. Teams confuse these constantly.
- 10:24Speaker 1: Because they both look like standard shell commands.
- 10:27Speaker 2: Right, but their contexts are entirely different. Rue operates exclusively
- 10:32Speaker 2: in the build phase. It modifies the file system and
- 10:35Speaker 2: commits to the image. Cmd'd does absolutely nothing during the build.
- 10:38Speaker 1: Oh interesting, it's just a default parameter for later exactly.
- 10:42Speaker 2: It embeds an execution instruction into the metadata, which the
- 10:45Speaker 2: demon reads when the container actually boots. For our app,
- 10:49Speaker 2: we use cmd flask run host zero point zero point
- 10:52Speaker 2: zero point zero port five zero point zero point one.
- 10:56Speaker 1: Now that host flag is super important because if you
- 10:59Speaker 1: run flask natively, it binds to local host right twenty
- 11:02Speaker 1: two one seven point zero point zero point one.
- 11:04Speaker 2: Yeah, but inside a container, local host strictly refers to
- 11:07Speaker 2: the container's isolated internal loop back. If you leave a default,
- 11:10Speaker 2: it rejects all external.
- 11:12Speaker 1: Traffic, so binding it to point zero point zero forces
- 11:14Speaker 1: it to listen on all interfaces, so we can actually
- 11:17Speaker 1: reach it from our host machines browser exactly.
- 11:19Speaker 2: And you probably noticed I formatted that CMB instruction as
- 11:22Speaker 2: a Jason array like with brackets in quotes.
- 11:25Speaker 1: Yeah, the exact form instead of just typing it out
- 11:27Speaker 1: as a rowstring.
- 11:28Speaker 2: Right, and that is a huge deal for production if
- 11:30Speaker 2: you use the standard string form. Docker wraps your command
- 11:33Speaker 2: in a shell process like binch my c.
- 11:35Speaker 1: Which creates the notorious PID one problem. Yes.
- 11:39Speaker 2: In Linux, process ID one handles system signals. If your
- 11:43Speaker 2: app is wrapped in a shell, the shell becomes pid one.
- 11:47Speaker 2: So when the orchestrator tries to gracefully shut down the
- 11:50Speaker 2: container with a sink term, the.
- 11:52Speaker 1: Shell intercepts it and never passes it to the Python
- 11:54Speaker 1: act exactly.
- 11:55Speaker 2: The orchestrator eventually gives up and just force fully kills
- 11:58Speaker 2: it with a sig kill, which can corrupt your database
- 12:01Speaker 2: or drop active user requests.
- 12:03Speaker 1: Yikes. So the exec form bypasses the shell entirely, ensuring
- 12:08Speaker 1: your app gets the right signals.
- 12:09Speaker 2: Yep. It's those granular mechanical details that separate a toy
- 12:13Speaker 2: project from an enterprise deployment. Oh and I should mention
- 12:16Speaker 2: the great thing about cmd is that the end user
- 12:18Speaker 2: can easily override it.
- 12:20Speaker 1: All right. If a DevOps engineer runs docor run image bench,
- 12:23Speaker 1: it just ignores our flask command and drops them into
- 12:26Speaker 1: the debugshell. Ultimate flexibility. Well, let's quickly recap this journey.
- 12:30Speaker 1: We bypassed a bloated OS for a tiny Alpine base,
- 12:33Speaker 1: replacing glibbc with muzzle. We use work dear for clean paths.
- 12:37Speaker 1: We exploited the union FS caching by copying our requirements first,
- 12:41Speaker 1: completely eliminating dependency latency. We chained our metadata and use
- 12:46Speaker 1: the exec form of CMD for clean shutdowns.
- 12:48Speaker 2: It's a perfect blueprint. And to really reinforce this, I
- 12:51Speaker 2: have a quick mental exercise for you.
- 12:52Speaker 1: Okay, let's hear.
- 12:54Speaker 2: Imagine you're working on this code based tomorrow. You open
- 12:57Speaker 2: a pison file, modify a single lodging block, save it,
- 13:01Speaker 2: and trigger a build based on the caching mechanisms we
- 13:04Speaker 2: just talked about, which specific instruction in our docro file
- 13:07Speaker 2: is the first one to break the cash and trigger
- 13:09Speaker 2: a rebuild?
- 13:09Speaker 1: Okay, this test the cascading invalidation protocol. It's the second
- 13:13Speaker 1: copyy command. The copy that brings in the general source.
- 13:17Speaker 2: Code spot on. Because the requirements dot txt didn't change
- 13:20Speaker 2: the heavy PIP install layer above, it stays perfectly locked
- 13:23Speaker 2: in the cash.
- 13:24Speaker 1: So what does all this mean for you? It means
- 13:26Speaker 1: these principles apply everywhere, whether you're using no JS, go Ruby.
- 13:31Speaker 1: Structuring your blueprint to isolate static dependencies from vulnerable code
- 13:35Speaker 1: gives you the power to make any build lightning fast.
- 13:38Speaker 2: We have successfully engineered a perfectly optimized, totally isolated container
- 13:43Speaker 2: for our flask cap But there is one pretty big
- 13:48Speaker 2: architectural puzzle left.
- 13:49Speaker 1: Wait what puzzle?
- 13:50Speaker 2: Well, we said at the start that this flask app
- 13:53Speaker 2: relies on a reddest database to save the counterstate, right right,
- 13:56Speaker 2: And we just perfectly packaged flask into an isolated locked
- 14:01Speaker 2: and reddis is going to be in its own entirely
- 14:03Speaker 2: isolated container. So if containers are meant to be completely
- 14:05Speaker 2: walled off, how exactly do these two separate worlds securely
- 14:09Speaker 2: discover each other?
- 14:10Speaker 1: Oh? Wow?
- 14:11Speaker 2: Yeah, how do they talk and transmit data without exposing
- 14:14Speaker 2: their internal ports to the wild west of the public Internet?
- 14:17Speaker 1: Ah? The mysteries of Docker networking and volume mounts. Moving
- 14:21Speaker 1: from one container to a multi container orchestra is a
- 14:23Speaker 1: huge paradigm shift. Definitely something that wanted to explore as
- 14:26Speaker 1: you start scaling up, but for now. The next time
- 14:29Speaker 1: you trigger a build and brace yourself for a ten
- 14:31Speaker 1: minute wait, remember the layers, remember the heavy coats and
- 14:34Speaker 1: the T shirts. You have the tools to control the speed.
- 14:38Speaker 1: Thanks for joining us on this deep dive. We'll catch
- 14:40Speaker 1: you next time.