Check out a free preview of the full Complete Intro to Containers (feat. Docker) course

The "Docker Ignore" Lesson is part of the full, Complete Intro to Containers (feat. Docker) course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to add a dockerignore file, and explains that the files mentioned within the dockerignore document are files that should not be copied from the host operating system into the container, but that are still necessary within a project.


Transcript from the "Docker Ignore" Lesson

>> Again, like here in layers, all this entire projects in here, and there's something that actually will help you, let's actually go over that really quick. So, there is a concept of something called a Docker ignore file, and a Docker ignore file is much like a gitignore file, do you say like, here's a bunch of stuff that I don't want you to copy over.

And actually to prove my point here, let's pull up my command line here again, and if I say Docker, let's just do Docker run here, like this my note app, and if I say, Ls- lsha, don't detach that's not what I wanted. It didn't copy that over, it used to do that by default, so sometimes it'll copy for your Git directory which is not necessarily something that you need inside of your, this isn't a Git project.

But if it was again, project, right, so if I said like, in fact, let's just do it, get an IT, And then I built it again, and then I ran it again, Let's get it back to the one that the expose is on publish. Just FYI, instead of publish, I never actually do that, I do -p which is just the shorthand, lowercase p, right?

Okay, so that's working, now if I do ls- lsha, notice that it's copying over the git directory, do you need that in your production server? No, do you even want a new production server? No, it's actually like a security vulnerability, right? Because you've your git history and a bunch of stuff in there, so it's actually should be considered sensitive information.

So we could go and explicitly name all the folders that we wanna white list into this but I don't think that's a very useful pattern at all, so what we're gonna do instead is we're gonna say touch .dockerignore. And if you've ever written a git ignore file, it's literally the same format, so if I open this here, oops, you can see I have a dockerignore file here.

And if I just put .git, that's one we wanna ignore and the other one that we've talked about ignoring is node_modules, we never wanna copy over node modules. And you can put that slash too if you wanna make it like a directory, but, again, it's literally the same format as a git ignore.

Just if it matches any node modules anywhere in the path, it'll say nope not bringing that over. Questions about doctor ignore? Every project should at least have .git in its dockerignore.

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