Latest / CyberCode Academy / Course 31 - Dive Into Docker | Episode 8: Networking, Persistence, and System Optimization
Transcript
- 0:00Speaker 1: Have you ever felt like managing Docker containers is basically
- 0:04Speaker 1: like trying to hurt a bunch of amnesia cats.
- 0:06Speaker 2: Yeah, that's well, it's a slightly chaotic image, but I
- 0:10Speaker 2: mean it really captures the reality of containerization perfectly.
- 0:14Speaker 1: Right because you get them all lined up, they're performing
- 0:16Speaker 1: their specific little tasks and then boom, you turn one off.
- 0:20Speaker 2: And when you turn it back on, it's completely forgotten
- 0:22Speaker 2: its own name exactly.
- 0:23Speaker 1: It has absolutely no idea where it left its data,
- 0:26Speaker 1: and it's you know, completely lost the ability to communicate
- 0:29Speaker 1: with the rest of the clouder.
- 0:30Speaker 2: It's a real problem.
- 0:31Speaker 1: So today we're going to figure out how to train
- 0:35Speaker 1: those cats. We are unpacking the hidden mechanics of container choreography,
- 0:40Speaker 1: really going step by step through how they actually operate
- 0:43Speaker 1: under the hood.
- 0:43Speaker 2: Which is so crucial if you want to move beyond
- 0:46Speaker 2: the basics.
- 0:47Speaker 1: Definitely, and by the end of this deep dive, you
- 0:50Speaker 1: the listener, are going to understand this ecosystem so well,
- 0:54Speaker 1: you'll basically feel like a sissidmin wizard. You're stepping into
- 0:58Speaker 1: the role of the system architect along is today.
- 1:01Speaker 2: We are building an entire digital environment from the ground
- 1:04Speaker 2: up here and any good architect knows that before you
- 1:08Speaker 2: start decorating, you have to pour the foundation.
- 1:10Speaker 1: Right, the absolute basics exactly.
- 1:13Speaker 2: For containers, that foundation is communication. When a container wakes
- 1:17Speaker 2: up in this digital void, it needs a way to reach.
- 1:20Speaker 1: Out, which means we really need to understand how Docker
- 1:24Speaker 1: handles networking. So before we get into the containers themselves,
- 1:27Speaker 1: let's just establish a quick baseline. Good idea. If I
- 1:30Speaker 1: start a web server on my physical laptop for like
- 1:34Speaker 1: a weekend coding project, it's operating on standard networking principles. Right,
- 1:39Speaker 1: you have internal networks local area networks or lands, and
- 1:42Speaker 1: then external networks like the broader Internet.
- 1:44Speaker 2: That is the perfect starting point. I mean, just think
- 1:46Speaker 2: of your home Wi Fi router. Every laptop, phone, smart
- 1:49Speaker 2: TV connected to it is on your.
- 1:51Speaker 1: Land, right, they all share a network.
- 1:52Speaker 2: Yeah, they share a common IP address format, usually something
- 1:55Speaker 2: starting with one two point one six eight, And because
- 1:59Speaker 2: they share that space, they can find each other easily.
- 2:02Speaker 1: Okay, So when I run that local web server on
- 2:04Speaker 1: my laptop, say on port five thousand, I have a
- 2:08Speaker 1: crucial choice to make about how I actually bind it
- 2:11Speaker 1: to the network. You do, I see this a lot
- 2:13Speaker 1: in configurations. You know, choosing between local host and zero
- 2:17Speaker 1: point zero point zero. I know they behave differently, but
- 2:20Speaker 1: what is physically happening there?
- 2:22Speaker 2: Well, it really comes down to network interfaces. If you
- 2:25Speaker 2: bind your server to local host, you are essentially telling
- 2:28Speaker 2: it to only listen to the internal loop back.
- 2:31Speaker 1: Interface, like whispering to yourself.
- 2:33Speaker 2: Precisely, only your specific laptop can hear it. Nobody else
- 2:38Speaker 2: on your Wi Fi can access that server at all.
- 2:41Speaker 2: But if you use zero point zero point zero point
- 2:43Speaker 2: zero base, you're opening the door exactly. You are telling
- 2:46Speaker 2: the server to listen on all available network interfaces. You've
- 2:50Speaker 2: just opened the door to your local land, meaning anyone
- 2:52Speaker 2: on your Wi Fi can, you know, type in your
- 2:54Speaker 2: laptop's IP address and see your project.
- 2:56Speaker 1: But the outside work, like the actual Internet, still can't
- 2:58Speaker 1: see it, right, because my home router is acting as
- 3:01Speaker 1: a bouncer like a firewall, blocking all those random outside.
- 3:05Speaker 2: Requests, right, They can't get in by default, So.
- 3:07Speaker 1: If I want the Internet to see it, I have
- 3:08Speaker 1: to go into the router settings and explicitly configure port forwarding. Right,
- 3:13Speaker 1: Tell the bouncer, Hey, if someone from the outside asks
- 3:15Speaker 1: for court five thousand, send them directly to this specific laptop.
- 3:19Speaker 2: You've got it completely. So keep that bouncer and that
- 3:22Speaker 2: local network in mind, because docer basically creates a miniature,
- 3:26Speaker 2: virtualized version of that exact setup inside your computer. Oh
- 3:29Speaker 2: really Yeah, When you install Docker, it silently creates a
- 3:33Speaker 2: default network called the bridge, and on your computer system,
- 3:36Speaker 2: it manifests as a virtual network device named Docker zero.
- 3:41Speaker 1: So it's essentially a virtual Wi Fi router living inside
- 3:45Speaker 1: my machine.
- 3:46Speaker 2: That's a great way to look at it.
- 3:47Speaker 1: And if I just hit run on a container without
- 3:49Speaker 1: giving it any special network instructions, docer just connects it
- 3:52Speaker 1: to that default bridge automatically.
- 3:54Speaker 2: Yes it does. And just like your home router hands
- 3:57Speaker 2: out those one hundred and ninety two point one six eight,
- 4:00Speaker 2: the Docker bridge hands out its own internal IP addresses.
- 4:03Speaker 1: Interesting, so what do those look like?
- 4:05Speaker 2: Well, if you spin up a redtus database container, it
- 4:07Speaker 2: gets dropped onto the bridge and assigned an IP maybe
- 4:11Speaker 2: one seventy two point one sen point zero point two.
- 4:14Speaker 2: Then if you spin up a Flask web application container,
- 4:17Speaker 2: it might get one seventy two point one same point
- 4:20Speaker 2: zero point three.
- 4:21Speaker 1: And because they share that default bridge, Docker allows the
- 4:24Speaker 1: Flask container to send data to the reddest container using
- 4:28Speaker 1: that specific one seven to two point one scene point
- 4:30Speaker 1: zero point two address. They have a connection, they do. Yeah,
- 4:33Speaker 1: but wait, building an application that relies on hard coded
- 4:36Speaker 1: IP addresses sounds like a terrible trap. Oh, it absolutely is, right,
- 4:39Speaker 1: Like what if my server reboots, doctor starts the containers
- 4:42Speaker 1: in a different order, hands out different ips, and suddenly
- 4:44Speaker 1: my web app is throwing errors because redtus isn't it
- 4:47Speaker 1: dot two anymore?
- 4:48Speaker 2: Exactly.
- 4:48Speaker 1: It feels like going to a crowded party and forcing
- 4:50Speaker 1: everyone to memorize each other's social security numbers just to
- 4:53Speaker 1: have a normal conversation.
- 4:54Speaker 2: That is a remarkably accurate comparison, honestly, and it highlights
- 4:58Speaker 2: a major pitfall for beginners. Ying on default bridge IPD
- 5:01Speaker 2: re addresses creates this incredibly fragile.
- 5:04Speaker 1: Architecture because they change, right.
- 5:06Speaker 2: IP addresses are ephemeral, They change all the time. So
- 5:10Speaker 2: to build something resilient we abandon the default bridge entirely
- 5:15Speaker 2: and create a custom bridge network.
- 5:17Speaker 1: Okay, so I write a command to create my own network,
- 5:20Speaker 1: let's call it first network, and I attach my containers
- 5:23Speaker 1: to that instead. What does that actually change.
- 5:25Speaker 2: Under the hood, Well, it activates Docker's internal DNS its
- 5:28Speaker 2: domain name system. When you attach containers to a custom network,
- 5:32Speaker 2: docer automatically acts as a dynamic phone book.
- 5:35Speaker 1: Oh I see, so instead of memorizing the social security number,
- 5:38Speaker 1: you're just handing everyone a name tag.
- 5:40Speaker 2: Precisely the mechanic. So, if your database container's designated name
- 5:43Speaker 2: is just rettus and your web container is web two,
- 5:46Speaker 2: the web container doesn't need to know the IP address anymore.
- 5:49Speaker 1: Wow. So you just write your code to ping the
- 5:51Speaker 1: host name reddis.
- 5:52Speaker 2: Yeah, Behind the scenes, docer intercepts that request, looks up
- 5:56Speaker 2: whatever hidden temporary IP address reddis currently holds and routes
- 6:00Speaker 2: the traffic seamlessly.
- 6:01Speaker 1: That is incredibly elegant. The IP addresses can shuffle around
- 6:05Speaker 1: all day long, but as long as the name tag
- 6:07Speaker 1: stay the same, the system basically never breaks. Okay, so
- 6:10Speaker 1: our amnesiac caps can finally call each other by name,
- 6:13Speaker 1: But we still haven't solved the actual amnesia problem. Let's
- 6:17Speaker 1: explore data volumes, because right now our containers are still
- 6:21Speaker 1: inherently stateless.
- 6:22Speaker 2: Right they are? We probably need a scenario to visualize this.
- 6:26Speaker 2: Imagine your flask web app features a simple visitor hit counter,
- 6:30Speaker 2: and it is storing that running tally inside your reddest
- 6:33Speaker 2: database container.
- 6:34Speaker 1: Okay, simple enough. So fifty people visit the site, the
- 6:37Speaker 1: database ticks up to fifty. Then, for whatever reason, I
- 6:40Speaker 1: need to stop the reddest container. I shut it down,
- 6:43Speaker 1: I boot it back up when I refresh the web page.
- 6:46Speaker 2: What actually happens, the counter says, one, your fifty previous
- 6:50Speaker 2: visits are entirely.
- 6:51Speaker 1: Gone, completely wiped. Wait, why does doctor do that?
- 6:54Speaker 2: It comes down to the fundamental philosophy of containerization, which
- 6:57Speaker 2: is the separation of the compute layer from the store. Okay,
- 7:00Speaker 2: explain that containers are designed to be entirely ephemeral. When
- 7:04Speaker 2: they boot up, they construct a fresh temporary file system
- 7:07Speaker 2: based on their underlying image, and when they shut down,
- 7:10Speaker 2: that temporary filesystem just evaporates.
- 7:12Speaker 1: That sounds dangerous.
- 7:14Speaker 2: It forces you to build applications that don't rely on
- 7:16Speaker 2: the physical server they happen to be running on at
- 7:19Speaker 2: any given moment.
- 7:20Speaker 1: I understand the theory there, but in practice, I mean,
- 7:23Speaker 1: a database that forgets its data is useless. We definitely
- 7:27Speaker 1: need a way to bypass that evaporation.
- 7:29Speaker 2: And we bypass it using what are called named volumes.
- 7:33Speaker 2: You instruct Docker to provision a specific storage space. Say
- 7:37Speaker 2: we name it web to rettis.
- 7:39Speaker 1: Now, I've seen people try to map container data to
- 7:43Speaker 1: specific hard coded folders on their desktop, like see users
- 7:47Speaker 1: documents data. But named volumes are different from that, aren't.
- 7:50Speaker 2: They Very different? Yeah, and much safer. When you use
- 7:53Speaker 2: a named volume, you aren't telling docer exactly where to
- 7:56Speaker 2: put the files on your hard drive.
- 7:58Speaker 1: So where does it go?
- 7:59Speaker 2: Docer manages the volume entirely within its own protected hidden
- 8:03Speaker 2: directory structure on the host machine. You don't interact with
- 8:06Speaker 2: the files directly at all. You just let Docker handle
- 8:08Speaker 2: all the permissions in the pathways.
- 8:09Speaker 1: So how does the container actually know to use it?
- 8:12Speaker 2: When you boot up the Reddus container, you pass a
- 8:14Speaker 2: volume flag that maps your named volume web to rettus
- 8:18Speaker 2: directly to the specific internal directory where Reddus naturally looks
- 8:22Speaker 2: for data, which is what for rereddis that's usually the
- 8:24Speaker 2: data directory.
- 8:25Speaker 1: Ah, I see the trick. Reddus operates completely normally. It
- 8:30Speaker 1: thinks it's just writing its hit counter data to its
- 8:32Speaker 1: own internal data folder, right, But Docker intercepts that right
- 8:36Speaker 1: action and funnels it down into the protected named volume
- 8:40Speaker 1: on the host machine.
- 8:41Speaker 2: Exactly. The container is just a temporary shell, but the
- 8:44Speaker 2: volume is permanent.
- 8:45Speaker 1: Oh, Wow.
- 8:45Speaker 2: Yeah, you can stop the Reddus container. You can completely
- 8:48Speaker 2: delete it. You can upgrade to a brand new version
- 8:51Speaker 2: of Reddus. When you boot the new container and attach
- 8:54Speaker 2: that same web to Rettus volume to its data folder,
- 8:57Speaker 2: it instantly reads the old files.
- 8:59Speaker 1: So you hit counter picks up right at fifty exactly.
- 9:02Speaker 1: That clarifies the compute versus storage concept beautifully. Yeah, we
- 9:06Speaker 1: treat the containers like disposable paper coffee cups, but we
- 9:09Speaker 1: treat the data like the coffee itself. You can swap
- 9:12Speaker 1: the cup whenever you want, as long as you don't
- 9:14Speaker 1: spill the coffee.
- 9:15Speaker 2: That's a great analogy. It's really the only way to
- 9:17Speaker 2: build a system that can scale and update without risking
- 9:20Speaker 2: catastrophic data loss.
- 9:22Speaker 1: Right, So a container can safely store data on the
- 9:24Speaker 1: host machine. But I'm kind of curious about direct collaboration.
- 9:28Speaker 1: If containers are isolated by default, can they share data
- 9:31Speaker 1: directly with each other without me having to, you know,
- 9:34Speaker 1: manually shuffle files around.
- 9:36Speaker 2: They can, Yes, And there is a very specific architectural
- 9:40Speaker 2: pattern where this becomes absolutely necessary, like serving static assets
- 9:44Speaker 2: in a production environment.
- 9:45Speaker 1: By static assets, you mean things like CSS style sheets,
- 9:48Speaker 1: JavaScript files, images, basically the visual building blocks of a website.
- 9:53Speaker 2: Right, let's stick with our Flask application. Your Flask contoner
- 9:56Speaker 2: holds your Python fode, but it also holds a main
- 9:59Speaker 2: dot Com CSS file tucked away inside in a public folder.
- 10:02Speaker 1: Okay, standard setup.
- 10:03Speaker 2: Now, Flask is great for processing back end logic, but
- 10:06Speaker 2: it's actually quite slow at serving static files to thousands
- 10:09Speaker 2: of users. In a professional setup, you wouldn't really use
- 10:12Speaker 2: Flask for that. You would stand up a dedicated high
- 10:15Speaker 2: speed web server like engins, running in its own separate container.
- 10:19Speaker 1: I'm seeing the conflict here in jincs needs to serve
- 10:22Speaker 1: the CSS file to the users, but the CSS file
- 10:25Speaker 1: is locked inside the Flask container's isolated file system. How
- 10:30Speaker 1: do we build a bridge between them?
- 10:31Speaker 2: We start at the source inside the blueprint for the
- 10:34Speaker 2: Flask container, which is its docker file. We add a
- 10:37Speaker 2: specific instruction volume. We point that instruction right at a
- 10:42Speaker 2: public folder.
- 10:43Speaker 1: So we're basically declaring, hey, this specific directory is open
- 10:46Speaker 1: for sharing exactly. Okay, the folder is marcus shareable. But
- 10:51Speaker 1: the second container still has to access this somehow.
- 10:53Speaker 2: Right, So when you boot up the jynx container, you
- 10:56Speaker 2: use the distinct flag datch dash volumes from followed by
- 11:00Speaker 2: the name of the Flask container.
- 11:02Speaker 1: Wait, let's look at the actual mechanics of that. When
- 11:04Speaker 1: I use that volumes from flag, is docer just copying
- 11:07Speaker 1: the files over to the Jinx container.
- 11:09Speaker 2: Not at all, And honestly that's the brilliance of it.
- 11:11Speaker 2: Docer reaches down into the Linux kernel and uses something
- 11:14Speaker 2: called namespace mounting.
- 11:15Speaker 1: What does that mean? In practice?
- 11:17Speaker 2: It physically links the file pathways. Both the flask container
- 11:21Speaker 2: and the Jinx container are looking at the exact same
- 11:23Speaker 2: physical sectors on the host's hard drive simultaneously.
- 11:26Speaker 1: Oh wow, So it's a live connection.
- 11:28Speaker 2: A completely live connection.
- 11:30Speaker 1: So if my flask application dynamically updates a line of
- 11:33Speaker 1: code in that CSS file, the Jinx container serves the
- 11:36Speaker 1: updated version the very next millisecond because it's looking at
- 11:40Speaker 1: the identical file.
- 11:41Speaker 2: It is perfectly synchronized.
- 11:43Speaker 1: I have to say Devil's advocate here, though, dealing with
- 11:45Speaker 1: namespace mounting and volumes from flags sounds a bit convoluted.
- 11:51Speaker 2: It can seem that way at first.
- 11:52Speaker 1: Wouldn't to be infinitely easier to just configure my build
- 11:55Speaker 1: system to copy the CSS folder into both containers. Just
- 11:59Speaker 1: give Ninjin's its own independent copy of the files and
- 12:02Speaker 1: be done with it.
- 12:02Speaker 2: It definitely sounds easier in the short term, but you
- 12:04Speaker 2: are walking right into the duplicated data trap. Let me
- 12:08Speaker 2: pose the scenario for you. You and a coworker are
- 12:10Speaker 2: collaborating on a report. You write a draft and email
- 12:14Speaker 2: them the word document. You both start making edits on
- 12:17Speaker 2: your separate local copies. What happens at the end.
- 12:19Speaker 1: Of the day, Oh, emerging nightmare. Yeah, I don't know
- 12:22Speaker 1: which version is the absolute truth, and we probably end
- 12:25Speaker 1: up overwriting each other's work exactly.
- 12:27Speaker 2: And duplicating files across containers creates the exact same nightmare,
- 12:32Speaker 2: but at an infrastructure scale. Pikes, if your system gets
- 12:35Speaker 2: out of sync, Innings might be serving an outdated stylesheet
- 12:39Speaker 2: while Flask expects a new one, breaking your entire website's layout.
- 12:43Speaker 2: The volumes from approach is like collaborating on a shared
- 12:46Speaker 2: cloud document.
- 12:47Speaker 1: Ah, there's only one single source of truth, right.
- 12:51Speaker 2: The flask container owns the document and the engine's container
- 12:54Speaker 2: is just granted permission to read it. Your architecture remains
- 12:57Speaker 2: perfectly synchronized and clean.
- 12:59Speaker 1: Single source of truth. That's a golden rule. Yeah, okay,
- 13:02Speaker 1: so our containers are communicating by name, preserving their memories
- 13:05Speaker 1: and sharing data seamlessly. But I'm noticing a sort of
- 13:09Speaker 1: side effect here. What's that the more features and complex
- 13:12Speaker 1: tools we pack into these containers to make them do
- 13:14Speaker 1: all this, the heavier they get. Our images are becoming bloated.
- 13:17Speaker 1: How do we keep them from becoming massive resource ogs?
- 13:21Speaker 2: Well, image optimization is a massive topic, But let's start
- 13:24Speaker 2: with the bouncer at the door, which is the dot
- 13:26Speaker 2: docker ignore file. If you use Git, you already understand
- 13:29Speaker 2: this concept from dot get ignored.
- 13:31Speaker 1: Yeah. It's basically a text file where you list out
- 13:33Speaker 1: all the garbage you don't want Docker.
- 13:35Speaker 2: To look at exactly. When Docker builds an image, its
- 13:38Speaker 2: very first step is to copy the context your entire
- 13:41Speaker 2: project folder into the build engine. A dot docker ignore
- 13:44Speaker 2: file contains a list of patterns to stop that.
- 13:47Speaker 1: So what kinds of things do we block?
- 13:49Speaker 2: You can tell it to completely ignore massive dot get
- 13:53Speaker 2: history folders or those temporary dot swap files that text
- 13:57Speaker 2: editors like Vim leave behind. Sees the list and refuses
- 14:01Speaker 2: to copy them into the image.
- 14:02Speaker 1: Which obviously saves megabytes of space. But there's a security
- 14:06Speaker 1: angle here too, right, a huge one, because if I
- 14:08Speaker 1: have a dot end file sitting at my project folder
- 14:10Speaker 1: that contains my live database, passwords and apikeys. I definitely
- 14:14Speaker 1: do not want those permanently baked into a Docker image
- 14:17Speaker 1: where literally anyone who downloads it can extract them.
- 14:20Speaker 2: Absolutely not. The dot doc or nor file keeps those
- 14:23Speaker 2: secrets safely on your local machine. It's a critical security
- 14:26Speaker 2: practice good too, But keeping out unwanted files is the
- 14:29Speaker 2: easy part. The advanced challenge comes when you're dealing with
- 14:32Speaker 2: system dependencies. This is where we can see images shrink
- 14:35Speaker 2: from over three hundred megabytes down to just over one hundred.
- 14:39Speaker 1: Wait, really, I want to understand how that massive reduction
- 14:42Speaker 1: is possible.
- 14:43Speaker 2: Okay, let's say your application needs to talk to a
- 14:46Speaker 2: post rescual database to keep things lightweight. You are probably
- 14:49Speaker 2: using Alpine Linux as your container's operating system.
- 14:52Speaker 1: Sure Alpine is notoriously tiny just a few megabytes, right?
- 14:55Speaker 2: Yeah? Because it comes with almost nothing installed. So to
- 14:58Speaker 2: give it postpressible capability, you have to tell it to
- 15:00Speaker 2: download and install a package called postgrescal div.
- 15:04Speaker 1: Okay, simple enough, I just add an run instruction in
- 15:07Speaker 1: my Docker file to install it.
- 15:09Speaker 2: Here is the catch, though, That package includes two different things.
- 15:13Speaker 2: The lightweight run time libraries your application actually needs to function,
- 15:16Speaker 2: and the heavy compilers and C level build tools required
- 15:20Speaker 2: to piece those libraries together during the installation process.
- 15:23Speaker 1: Oh, I see, so I need the heavy tools for
- 15:25Speaker 1: the like three seconds it takes to build the software,
- 15:28Speaker 1: but then they just sit there taking up two hundred
- 15:30Speaker 1: megabytes of space forever.
- 15:31Speaker 2: Exactly.
- 15:32Speaker 1: Well, why don't I just add a second run instruction
- 15:35Speaker 1: right after it to delete the build tools?
- 15:37Speaker 2: You can't because of how Docker constructs its file system.
- 15:41Speaker 2: Think of a Docker image as a stack of transparent tracing.
- 15:45Speaker 1: Paper, okay, tracing paper.
- 15:48Speaker 2: Every single command you execute in a Docker file, every RN, copy,
- 15:52Speaker 2: Y or add, creates a permanent new sheet of paper
- 15:56Speaker 2: called a layer. If you use one sheet to draw
- 15:59Speaker 2: the heavy build tool and then place a new sheet
- 16:01Speaker 2: on top and use an eraser to delete them, the
- 16:04Speaker 2: tools aren't actually.
- 16:05Speaker 1: Gone, oh, because they're still on the sheet.
- 16:07Speaker 2: Below it, right, They're still perfectly visible on the bottom sheet,
- 16:11Speaker 2: and they still take up just as much space on
- 16:13Speaker 2: the hard drive.
- 16:13Speaker 1: Oh that is a frustrating quirk. Docker layers are immutable,
- 16:17Speaker 1: So how do you actually get rid of the weight.
- 16:19Speaker 2: You have to do all your drawing and erasing on
- 16:21Speaker 2: the exact same sheet of tracing paper. In technical terms,
- 16:25Speaker 2: you have to chain your commands together into one single
- 16:28Speaker 2: massive RN instruction.
- 16:30Speaker 1: I see so looking at advanced Docker files, this is
- 16:32Speaker 1: where you start seeing those massive multi line Bash scripts.
- 16:36Speaker 1: To use double amper sands the in and symbols, which
- 16:39Speaker 1: basically tells the system run this command and if it succeeds,
- 16:42Speaker 1: immediately run the next one.
- 16:43Speaker 2: You've got it.
- 16:44Speaker 1: And they use backslashes at the end of lines just
- 16:46Speaker 1: to break up the text so humans can actually read it.
- 16:49Speaker 2: That's the mechanism. Within one single step, you tell Docker
- 16:53Speaker 2: install the heavy build dependencies, compile the software, and then
- 16:57Speaker 2: completely delete those build dependencies.
- 16:59Speaker 1: Ah so, because it all happens in one breath before
- 17:03Speaker 1: Docker commits the layer to memory, the final save sheet
- 17:06Speaker 1: of tracing paper only contains the lightweight run time software exactly.
- 17:10Speaker 1: It's incredibly clever, it is, But I have to ask,
- 17:14Speaker 1: writing and debugging a massive chain Bash script sounds like
- 17:17Speaker 1: a total nightmare. I mean, one typo and the whole
- 17:20Speaker 1: build fails. Is that level of complexity really necessary?
- 17:24Speaker 2: It's a classic engineering trade off. If you are learning
- 17:26Speaker 2: or just building a casual weekend project, absolutely not spend
- 17:30Speaker 2: the extra two hundred megabytes and save yourself the headache.
- 17:32Speaker 1: Right, keep it simple.
- 17:33Speaker 2: But if you are deploying to an enterprise cloud environment
- 17:37Speaker 2: where your application might scale up to a thousand instances,
- 17:40Speaker 2: that two hundred megabyte suddenly translates to massive network bottlenecks
- 17:44Speaker 2: and skyrocketing storage costs at scale, the ugly bash script
- 17:49Speaker 2: is worth its weight in goals.
- 17:50Speaker 1: Scale really changes the math on everything. All right, So
- 17:53Speaker 1: our image is mathematically as tiny as possible, But what
- 17:57Speaker 1: happens the exact millisecond that can actually wakes up? What
- 18:01Speaker 1: do you mean I'm thinking about our database? If I
- 18:04Speaker 1: have to manually open a terminal log into the Postgress
- 18:07Speaker 1: container and run SQL queries to create my user and
- 18:10Speaker 1: database every single time I boot my optimized image, well,
- 18:14Speaker 1: that totally defeats the purpose of automation. Is there a
- 18:17Speaker 1: way to trigger setup tasks automatically?
- 18:19Speaker 2: There is, and it's built around an instruction called the
- 18:22Speaker 2: entry point. It is essentially a script that executes immediately
- 18:25Speaker 2: after the container boots up, but before your main application
- 18:27Speaker 2: actually starts running, so.
- 18:28Speaker 1: Like an initialization phase exactly.
- 18:31Speaker 2: The creators of the official Postcress image use an entry
- 18:34Speaker 2: point masterfully when their container boots, the script wakes up,
- 18:39Speaker 2: reads the environment variables you passed in like postgriss user
- 18:42Speaker 2: and postgris password, and quietly runs the setup queries for you.
- 18:47Speaker 1: Oh nice.
- 18:48Speaker 2: Yeah, So by the time the database engine actually signals
- 18:50Speaker 2: that it's ready for connections, your user and your data
- 18:53Speaker 2: tables are already fully constructed.
- 18:55Speaker 1: You can probably use it for dynamic configuration too, Like
- 18:58Speaker 1: say I wanted my hit counter app to greet the
- 19:00Speaker 1: user with a custom message. Depending on what environment it's
- 19:03Speaker 1: running in, the entry point script could dynamically rewrite the
- 19:07Speaker 1: HTML file with the custom string right before starting the
- 19:11Speaker 1: web service.
- 19:11Speaker 2: Absolutely, it allows a single generic image to adapt its
- 19:14Speaker 2: configuration dynamically at boot time, preventing you from having to
- 19:18Speaker 2: rebuild the entire image just to change a simple parameter.
- 19:20Speaker 1: I think I have an analogy for this. The entry
- 19:22Speaker 1: point is basically the matre d at a high end restaurant.
- 19:26Speaker 2: Oh, let's build on that.
- 19:27Speaker 1: So no matter who walks through the front door, the
- 19:29Speaker 1: maitre deau performs a consistent set of initialization tasks. They
- 19:33Speaker 1: check the reservation, they set the table, they pour the water,
- 19:35Speaker 1: they prepare the environment, and the CMD instruction. The actual
- 19:39Speaker 1: application you want the container to run is the specific
- 19:43Speaker 1: customer walking in to eat.
- 19:45Speaker 2: That is a phenomenal way to visualize it because it
- 19:47Speaker 2: perfectly explains a very cryptic piece of shell scripting. You'll
- 19:51Speaker 2: always see at the very bottom of an entry point
- 19:54Speaker 2: script exec at era.
- 19:55Speaker 1: Yes, I was wondering of that line. What does executive
- 19:58Speaker 1: actually do well?
- 19:59Speaker 2: In architecture? If you define an entry point, the CMD
- 20:02Speaker 2: instruction no longer runs on its own. The entire CMD
- 20:06Speaker 2: string is intercepted and passed as an argument right into
- 20:09Speaker 2: the entry point script.
- 20:10Speaker 1: Oh like handing the customer specific food order directly to
- 20:14Speaker 1: the major dough exactly.
- 20:16Speaker 2: The script runs through all its setup tasks, and when
- 20:19Speaker 2: it hits that final exec at line, the matrid is
- 20:21Speaker 2: essentially saying, Okay, the table is set. Now take whatever
- 20:25Speaker 2: specific command the user passed in the CMD and execute it.
- 20:29Speaker 1: Wow. So if you forget to include that line.
- 20:32Speaker 2: Your setup script will run beautifully, but your actual application
- 20:35Speaker 2: will never start.
- 20:36Speaker 1: It's a subtle mechanic, but absolutely vital. All right, we
- 20:40Speaker 1: have covered an incredible amount of ground. We've built networks,
- 20:43Speaker 1: managed storage, optimized our builds, and automated our startups. We
- 20:47Speaker 1: are practically production ready, getting close but there is a
- 20:51Speaker 1: glaring issue on my local machine because we've been building
- 20:55Speaker 1: and rebuilding and testing, my hard drive is completely full.
- 21:00Speaker 2: Yes, a very common pain point. Docker is fundamentally a hoarder.
- 21:05Speaker 2: It is designed to be deeply conservative about deleting anything
- 21:08Speaker 2: operating on the assumption that you might want to roll
- 21:10Speaker 2: back to a previous layer or restart a stopped container
- 21:13Speaker 2: in the future.
- 21:13Speaker 1: Well, I really need my diskspace back. How do I
- 21:15Speaker 1: see exactly where the bloat is?
- 21:17Speaker 2: Just start by running Docker system DFEF, which stands for
- 21:20Speaker 2: disc free. It will output a breakdown of exactly how
- 21:23Speaker 2: much space your images, containers and volumes are consuming.
- 21:26Speaker 1: Okay, and what's usually the culprit.
- 21:28Speaker 2: What you'll usually find is a massive accumulation of dangling images.
- 21:33Speaker 1: Dangling images. When I list my images out in the terminal,
- 21:36Speaker 1: I sometimes see a bunch of them where the name
- 21:39Speaker 1: and the tag just say literally none. Is that what?
- 21:42Speaker 1: Those aren't?
- 21:43Speaker 2: Yes, they are abandoned layers. Maybe a build failed halfway
- 21:46Speaker 2: through and Docker save the partial progress. Or maybe you
- 21:50Speaker 2: successfully rebuilt an image and the new version took over
- 21:52Speaker 2: the latest tag, leaving the previous version's layers orphaned with
- 21:56Speaker 2: no name.
- 21:57Speaker 1: So They're completely useless, completely useless. So how do we
- 22:00Speaker 1: clear them out? Because I really don't want to delete
- 22:03Speaker 1: them one by one.
- 22:04Speaker 2: Doctor provides a built in cleaning mechanism, docer system prune.
- 22:08Speaker 2: By default, running that command safely sweeps away all unused networks,
- 22:12Speaker 2: all dangling none images, and any containers that are currently stopped.
- 22:16Speaker 1: In digital spring cleaning. But I know there's a flag
- 22:19Speaker 1: you can add to that command dash. You haven't honestly
- 22:22Speaker 1: reading what it does terrifies.
- 22:25Speaker 2: Ah, the aggressive dash of flag. Yeah, yes, it stands
- 22:28Speaker 2: for all. If you run Docker system prune, you completely
- 22:31Speaker 2: bypass the safe sweep. It deletes all images on your
- 22:34Speaker 2: entire system that are currently tied to an actively running container.
- 22:37Speaker 1: That sounds like setting off a digital nuke. What if
- 22:41Speaker 1: it deletes an image I spent three hours perfecting and compiling.
- 22:45Speaker 2: This really requires a shift in how you view your
- 22:47Speaker 2: infrastructure in a mature containerized workflow. Your images are not
- 22:52Speaker 2: your source code. Your docro file is your source code.
- 22:55Speaker 2: It is the immutable blueprint. So deleting an image does
- 22:58Speaker 2: not destroy.
- 22:59Speaker 1: Your work oh, because all the instructions to rebuild it
- 23:02Speaker 1: perfectly are preserved in the text file.
- 23:04Speaker 2: Precisely. If you nuke your system with the dash of flag,
- 23:08Speaker 2: the worst possible consequence is that you have to wait
- 23:10Speaker 2: a few minutes for Docker to rebuild the image from
- 23:13Speaker 2: the blueprint or re download it from the cloud. Oh okay,
- 23:16Speaker 2: it's actually a fantastic habit to get into because it
- 23:18Speaker 2: forces you to prove that your environments are truly reproducible
- 23:21Speaker 2: from scratch.
- 23:22Speaker 1: Okay, that lowers my blood pressure significantly. But before we
- 23:25Speaker 1: wrap up, I want to highlight a brilliant pro tip
- 23:27Speaker 1: for mass stopping containers. Because Prune only deletes stopped containers,
- 23:32Speaker 1: you have to shut them all down first, right.
- 23:35Speaker 2: And if you have two dozen micro services running, typing
- 23:38Speaker 2: Docker stop twenty four times is agonizing.
- 23:41Speaker 1: It really is. But there is a trick using the
- 23:44Speaker 1: quiet flag. If you list all your containers with Docker
- 23:47Speaker 1: container dauch aa, you can add a q.
- 23:50Speaker 2: Flag the quiet mode. It strips away all the formatting,
- 23:54Speaker 2: the names, the creation dates, and outputs nothing but a
- 23:57Speaker 2: raw vertical list of the container.
- 24:00Speaker 1: And because it's a raw list, you can feed it
- 24:02Speaker 1: directly into the stop command. You type Docker container stop,
- 24:06Speaker 1: wrap the quietless command in a subshell and hit enter.
- 24:09Speaker 2: It feeds every single id into the stop command, sequentially.
- 24:12Speaker 1: Shutting down your entire architecture in one breath.
- 24:15Speaker 2: It is an incredibly satisfying command to execute.
- 24:18Speaker 1: It really is well. We've navigated an entire architectural life
- 24:22Speaker 1: cycle today. We took isolated containers and gave them custom
- 24:25Speaker 1: networks so they could find each other by name. We
- 24:27Speaker 1: attached named volumes so that they could persist their memory
- 24:29Speaker 1: across lifetimes.
- 24:31Speaker 2: We utilize namespace mounting to share direct sources of truth.
- 24:34Speaker 1: We chain commands to trim the fat off our images.
- 24:37Speaker 1: We hired a matre d to handle dynamic boot setups,
- 24:41Speaker 1: and we finally learned how to sweep the floor.
- 24:43Speaker 2: You've gone from herding cats to conducting a symphony, but.
- 24:46Speaker 1: As with all complex systems, the learning never truly stops.
- 24:51Speaker 1: I have a two part exercise for you to test
- 24:53Speaker 1: your new architectural skills. First, go to Docker hub the
- 24:57Speaker 1: public registry of container images and look up your absolute
- 25:01Speaker 1: favorite database postgress, my sqel, Mango, whichever you prefer.
- 25:06Speaker 2: Dive into its documentation and see if you can locate
- 25:09Speaker 2: the exact internal folder path where it saves its data.
- 25:12Speaker 2: Until you understand its internal structure, you can't properly map
- 25:16Speaker 2: a permanent named volume to.
- 25:18Speaker 1: It exactly find that path. And second, I want you
- 25:20Speaker 1: to ponder a much bigger question. We just put this
- 25:23Speaker 1: entire deep dive unraveling the manual commands required to make
- 25:25Speaker 1: this work. Custom networks, volume, flags, chain, build steps, enter points,
- 25:30Speaker 1: right typing all of that out in the terminal for
- 25:31Speaker 1: ten different interconnected containers. Every single time you want to
- 25:34Speaker 1: spin up your project is prone to human error.
- 25:37Speaker 2: It's unsustainable for a single developer, let alone a whole team.
- 25:40Speaker 1: So how do the professionals automate orchestrating all these containers simultaneously?
- 25:45Speaker 1: How do you define the networks, the volumes, and the
- 25:47Speaker 1: images in one single readable file so you can spin
- 25:51Speaker 1: up an entire complex architecture with just one command.
- 25:54Speaker 2: That is the pivotal question for anyone moving into advanced deployments,
- 25:58Speaker 2: and the solution lies into discovering tools like Docker compose.
- 26:02Speaker 1: Something for you to explore as you continue building. The
- 26:06Speaker 1: next time you boot up a container and you worry
- 26:08Speaker 1: it's going to act like an amnesiac cat, just take
- 26:11Speaker 1: a breath.
- 26:11Speaker 2: Remember that you are the architect. You build the environment,
- 26:15Speaker 2: you set the rules, and you hand out the name
- 26:17Speaker 2: tags
- 26:17Speaker 1: And if you set the table right, they will always
- 26:20Speaker 1: remember exactly where they put the coffee