‫In this lecture, we're going to talk about bind mounting and persistent data in those mounts. The
‫requirements are really that you just know about container management and how to run containers.
‫Let's jump in. Let's talk about bind mounts. They're actually pretty cool.
‫When I first learned about them, it sort of gave me an aha of how I could use Docker easily for local
‫development.
‫Really, a bind mount is just a mapping of the host files, or directories, into a container file or
‫directory. You can do just that.
‫You can either specify a directory or just a single file. In the background,
‫it's basically just having the two locations point to the same physical locations on disk.
‫Again, this actually skips the UFS like the other volumes do so that it's not going to wipe out your
‫host location when you delete the container.
‫If there are any files in the container that you map the host files to, the host files win. It
‫doesn't actually delete the files in the container that it overwrote because it's not really overwriting
‫anything in the container.
‫It's just there while the bind mount exists.
‫The minute you don't need the bind mount any more and you re-run the container without it, you would
‫actually see the underlying data that was there before. Because bind mounts are usually host specific,
‫they need specific data to be on the hard drive of the host in order to work.
‫You can't specify them in a Dockerfile.
‫You have to use them at runtime when you use the docker container run command. You can see the format
‫here.
‫It's really the -v that we used before, only on the left side of the colon,
‫we're actually putting in a full path rather than just a name.
‫And the way the Docker actually can tell the difference between the named volume, like we did a while ago,
‫and the bind mount, is the bind mount starts with a forward slash.
‫On Windows, you'll notice that it's a two forward slashes and then the drive letter, and then your
‫path.
‫As long as you have the left colon and right sides, you can really map anything you want from the
‫host into the container, and you can also specify things like read-only.
‫Where this really comes to shine is with development and running services inside your container
‫that are accessing files you're using on your host or changing.
‫Let's do that with Nginx.
‫If you go to the root of our repository for this course, and then we go into the Dockerfile sample
‫2 directory. Now we were in here earlier.
‫If you remember, there's two files. This is for Nginx, right?
‫If we look at the Dockerfile real quick, it's pretty simple. It's just specifying a working directory
‫and then copying an index file into that directory.
‫There's no volumes here.
‫A bind mount doesn't require a volume to work, although one could certainly be there.
‫The host always wins, remember? So, when we're doing a docker container run, then we do a
‫-d...let's name it nginx.
‫Let's give it the port 8080 like before.
‫Then we're going to give it the volume this time. And I need to tell it my current working directory
‫is going to be actually mounted into that working directory in the container. Because my index file is
‫here in this folder,
‫I want it to be in the container so I can edit it here, and it is seen live in the container.
‫So it will make sense in a second.
‫Here's a quick little tip. Instead of me having to figure out my whole path and type the whole thing
‫in, you can use pwd here like so.
‫Which is a shell shortcut that says, 'print out the working directory and replace this command with that
‫path.'
‫Then we're just going to use the same working directory from the Dockerfile there, and then we're going to
‫specify the Nginx image.
‫What I'm going to do, real quick, is I'm actually going to open up another terminal window so we can
‫do two things at once.
‫You don't really need to do this.
‫Basically, we're just going to edit the file on our host, and then I'm going to be in the container and
‫see what's happening.
‫So just to make sure it's working,
‫let's go to localhost.
‫OK, so this isn't the default Nginx HTML file.
‫And we used the regular Nginx image.
‫We didn't actually use the custom one from this directory.
‫We just used a stock image. To prove that point, we can actually do that same, exact command again but
‫take out the -v, and I'll run it on port 8080 instead. And I'll call this one nginx2.
‫If I go over to my browser and go to localhost 8080, that's the default nginx index.html
‫file.
‫This one is the custom one that we have living in this directory.
‫So, already we can see that it mapped it correctly.
‫If I open up another tab here and get a command line in the container.
‫Then go to that directory.
‫You'll notice that we can actually see the Dockerfile because we map the whole directory.
‫If over on the left, if we actually made a new file and I can just use that Linux touch command here
‫to create a new file. Then over on the right...oops...just keep forgetting that...ls -al, and you'll
‫see that in your file.
‫Hopefully you understand what's going on here, that both these locations are the same location.
‫And if I echoed something into that testme file,
‫and then...
‫there you go.
‫Our nginx is able to see the changes because it's just a normal file path in the container,
‫and those files are on the host.
‫If I was to delete them in the container, they would be deleted on the host because it is the same
‫file.
‫This is when I usually get a developer to get their mind thinking about all of their development environments
‫that they have that are this complicated setup. Maybe they're using Vagrant, or some other manual method
‫of setting up their perfect environment, and they can start to see how this could make that so much simpler.
‫Because I didn't have to do any complicated setup on my Mac, I just ran a container.
‫I used the -v command with the mount, and now my code, on my host, can be edited on my host.
‫And I really never need to go into the shell of the container.
‫Usually, you can just run the container and look at its logs to see if there's any errors while you're
‫coding.
‫Of course, this is a simple example with Nginx. Later on, we're actually going to have specific
‫examples of different development environments for local development and containers.