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.

Get Unlimited Access Now

Transcript from the "Docker Ignore" Lesson

[00:00:00]
>> 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.

[00:00:22] 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.

[00:01:03] 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?

[00:01:36] 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.

[00:02:00] 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.

[00:02:25] 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.

[00:02:39] 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.