Check out a free preview of the full Complete Intro to Containers, v2 course

The "Bind Mounts" Lesson is part of the full, Complete Intro to Containers, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains when you use a bind mount, a file or directory on the host machine is mounted into a container. Unlike a volume, the file or directory with a bind mount is referenced by its absolute path on the host machine. Bind mounts are useful for sharing local state between your operating system and containers.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Bind Mounts" Lesson

[00:00:00]
>> Brian Holt: Let's get into the next part of our course here, which is the features of Docker. We're gonna go through kind of what I would deem to be the more essential parts of like what Docker is doing on top of containers for you. So none of these in and of themselves are directly containers, but they're like, how do we give files to To a container.

[00:00:20]
How do we network with these containers? How do we use these containers for development? Those kind of features. Yeah, I'll just go ahead and say that at this point, you basically know as much as you pretty much ever have to know about containers as of today, right? We'll talk about features that go on top of containers at this point, but containers don't really get much more complicated than what we talk about.

[00:00:41]
You'll look at some Docker files and see that there are a 100 lines and stuff like that. And they have branching logic and a bunch of stuff like that. And you can have Docker file templates if you have to generate ten different containers based on one kind of manifest.

[00:00:54]
That's all stuff that I know exists, but I've never personally had to do and I can imagine unless you are maintaining like open source containers that you will have to do either. But at this point you're armed with all the knowledge that you could definitely go figure it out that they're not so much more complicated than what we've shown you already.

[00:01:13]
Okay, so, bind mounts right now been kind of just self containing our containers, right? We've basically just saying all everything that we care about in this container gets kind of put inside of it. And then when the container goes away, everything goes with it. The baby also goes with the bathwater, proverbially speaking.

[00:01:36]
That's not always what you want, right? Sometimes you want data to stick around. A good example of that would be a database, right? If I tear down my database container, I don't wanna lose all of the data, right? That's kind of makes sense. We're gonna talk about volumes here in a second that handle that.

[00:01:53]
But for now, we're gonna talk about bind mounts, which are a little bit different and people get confused about the difference between bind mounts and volumes. I'm gonna try and explain it several times to you. And if you still don't get it, please stop me because it's important that you understand the difference between a bind mount and a volume.

[00:02:10]
We talked about here that we don't wanna have snowflake servers, right? Where we don't wanna basically go and very handcart our servers. We wanna have cattle servers so we can tear down and pull up. So how do we have a container that is a cattle server that we can basically use and reuse and destroy and recreate, but we wanna use something locally on our computer?

[00:02:29]
Like a good example, this is like I'm doing my development process on my computer, and I don't have to rebuild the container every single time, right? I don't wanna have that high touch kind of relationship with this container. That's what a bind mount is gonna allow you to do.

[00:02:42]
It's gonna allow you to say like, hey, these files exist on my local computer and I wanna use those there, but I wanna run that through a container. So if I'm running like my, let's say my NodeJS server, and I wanna run it in a Linux environment, and I wanna do active development on it while it's running.

[00:03:00]
This is where a bind mount is really cool. It's basically like a portal to your local computer that you can do all your stuff there and it's translated directly into your container. You don't have to rebuild your container every single time. So this allows them to be essentially stateful without being annoying, essentially, right.

[00:03:18]
So let's talk about more about these bind mounts and everything that I've explained so far is a bind mount. Another really good example of this is like let's say you are running your Astro project locally, and you just want to like surf it through nginx really quick to just to make sure that everything is working.

[00:03:39]
So you can actually do that today with this. Let's go actually go and make that work, so let's go into I think I'm in, I'm on my desktop. That's where I put it. Static asset project. So we have this, we've built it before. That's what that dis folder is.

[00:04:03]
You can just do npm run build. I'm not gonna do that again, but go into dist and then I'm just gonna copy and paste. Actually not from the dist directory. So just directly from the project directory here. And I'm gonna copy and paste all of that.
>> Brian Holt: Yeah, that's fine I don't care.

[00:04:29]
>> Brian Holt: So now in theory if I go to local 8080,
>> Brian Holt: You can see this is actually serving from NGINX.
>> Brian Holt: I think it should even say it on my, if I inspect it, network,
>> Brian Holt: Maybe, it might say it. No, that's request headers, yeah, right there, server, nginx.

[00:05:01]
Kind of cool, right? So let's talk about we actually ran here. So docker run, you got that. And then you understand this part here. No, not that part yet. But this part, the port, and we understand that we ran it for from Nginx latest. Let's talk about this part right there.

[00:05:25]
So we're gonna do a bind mount. That's what the -- mount. We told that the type of mount is bind as you might imagine there are others. And then the source, you have to give it a fully qualified directory. That's what that PWD is DS here. So we're seeing from the present working directory, you can also just give it the full absolute path.

[00:05:44]
I just didn't wanna figure that out. And I said from dist, which is a folder that we were just talking about. And then the target going to be inside of that directory that we put all the files in previously in the static asset project, right? So therefore, we just ran Nginx directly from our command line using docker, using a bind mount to just serve everything from dist, kind of cool, right?

[00:06:08]
I think it's really cool to have the ability to just spin up nginx really quickly, do a bunch of stuff with it and then spin it down. Now you might say Brian there's a trillion ways to serve Static assets on my local computer I can do it with Python.

[00:06:22]
I can do with PHP was a -s or stuff like that I can do it with Surf which is something for cell totally. 100% I probably would do like surf or something like that because it's I can just do MPX surf and that works as well. Why I would actually want to do this If I was testing out configurations with nginx, because you can also bind about the configuration file in there, right?

[00:06:47]
And you can try a bunch of different things to try and see if you can get your configuration correctly. Because messing with nginx configuration is scary in my opinion, right? So at the end of the day, yeah, you can do mpx, serve, but this is almost as easy, right?

[00:07:07]
Which I think is pretty cool, okay? Little note there if you're not seeing it, you just have to make sure that you build the project. So you have to do the npm install and run build if you haven't done that already. Little note there if you're having some issues.

[00:07:20]
>> Brian Holt: So, yeah, we'll I'll talk about volume here in a second, but right now we're talking about bind. Talk about that. That's kind of the whole gist of what a bind mount is. I'm not gonna really show you too much more because there's not really a lot more to it.

[00:07:34]
One thing that you do want to be careful of, like here's a really good example. Let's say we're bind mounting our entire dev project into a container so that we can do container development. There's things that I'm gonna show you here in a little bit that just kind of handle that tool chain for you.

[00:07:47]
But let's say you were doing it by yourself. Let's say you ran npm install locally on Mac OS and then you bind mounted that into Linux. You potentially have a mismatch of native dependencies because if you npm install on Mac, it's gonna install your dependencies for Mac. And then if you bind mount that into Linux, Linux and be like, this is for Mac, I don't know what to do with this, right?

[00:08:12]
So if you're gonna do that, make sure that you're running your npm install inside of the container. Which is annoying because now every time that you start your container, you do have to run npm install it again. Or have some black magic where you install your local dependencies for Linux.

[00:08:26]
Just sounds like a terrible idea to me. So anyway, that's something to keep in mind is that you're now kind of like traversing between worlds and you have to make sure that you are ready for whatever operating system you're gonna be in. So, bind mounts, I think are pretty clear.

[00:08:39]
It's for when you wanna share local state from your host operating system to containers, right? I think that tracks. As you might imagine, anything that you save in your bind mount will be saved to your local computer as well, right? So that it can work both ways.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now