1 00:00:05,950 --> 00:00:09,890 ‫All right. Let's look at some more Dockerfile examples. 2 00:00:09,940 --> 00:00:16,360 ‫In our repository that you've downloaded from my Github, let's go into Dockerfile sample number 2. 3 00:00:17,320 --> 00:00:19,560 ‫This time, we actually have two files. 4 00:00:19,630 --> 00:00:23,190 ‫We have the Dockerfile. And now we have an index.html. 5 00:00:23,200 --> 00:00:25,510 ‫Super simple. Hello world. HTML. 6 00:00:25,540 --> 00:00:28,100 ‫It's not really important to understand the HTML. 7 00:00:28,150 --> 00:00:32,630 ‫We're just going to copy it into our container image as we're building it. 8 00:00:33,160 --> 00:00:39,070 ‫If we look at the Dockerfile, you'll see on this one that we're actually using Nginx as our FROM 9 00:00:39,070 --> 00:00:39,650 ‫command. 10 00:00:39,670 --> 00:00:45,040 ‫We're not building our own Nginx because ideally, if you can use an official image to get the 11 00:00:45,040 --> 00:00:50,680 ‫job done from Docker Hub, then it will be a lot easier for you to maintain this Dockerfile and 12 00:00:50,830 --> 00:00:52,030 ‫keep it working well. 13 00:00:52,030 --> 00:00:58,570 ‫In simpler scenarios, quite often the official image is work. But as you grow and add complexity to 14 00:00:58,570 --> 00:01:04,330 ‫your environment and your systems, you'll probably find that you need to add additional custom software, 15 00:01:04,330 --> 00:01:09,680 ‫or change the way that it starts, or add scripts in so that it tweaks the configuration. 16 00:01:09,850 --> 00:01:15,820 ‫But when I'm starting a new greenfield project, or if I'm converting some old app, I always start with 17 00:01:15,820 --> 00:01:20,950 ‫the official images from Docker Hub. Then once I hit some roadblocks and I'm not able to use that 18 00:01:20,950 --> 00:01:25,570 ‫image anymore, I might go back to Docker Hub and take another look and see if there's anything custom out 19 00:01:25,570 --> 00:01:30,670 ‫there that's really popular, that I can trust, and look into and see if it's going to solve my problem. 20 00:01:30,700 --> 00:01:32,140 ‫But there's nothing wrong with building your own. 21 00:01:32,140 --> 00:01:34,750 ‫It's just more work and more upkeep over time. 22 00:01:35,140 --> 00:01:37,990 ‫So here is super simple. We've got three stanzas. 23 00:01:37,990 --> 00:01:41,000 ‫You've seen the FROM before. And this time we have the WORKDIR. 24 00:01:41,020 --> 00:01:47,350 ‫Now that's really just basically running a cd space directory change. So you might be tempted to 25 00:01:47,350 --> 00:01:53,410 ‫use the run command and just type run cd to this directory and then do some things. But really the best 26 00:01:53,410 --> 00:01:59,520 ‫practice for Dockerfiles is to always use a separate WORKDIR stanza for whenever you're changing directories. 27 00:01:59,530 --> 00:02:03,730 ‫So if your file gets a little complex and you have to move back and forth in your container to do things 28 00:02:03,730 --> 00:02:08,800 ‫while it's building, you always want to use the WORKDIR because it's a lot easier to describe in the 29 00:02:08,800 --> 00:02:10,020 ‫Dockerfile what you're doing. 30 00:02:10,330 --> 00:02:15,480 ‫In this case, what we're actually doing here is we're changing to the default Nginx directory 31 00:02:15,580 --> 00:02:17,380 ‫for its HTML files. 32 00:02:17,380 --> 00:02:22,750 ‫In the default configuration on Docker Hub, Nginx is acting just as a web server, and it's just serving 33 00:02:22,750 --> 00:02:25,500 ‫static files right off the container disk. 34 00:02:25,750 --> 00:02:32,290 ‫The last stanza here, we have, is the copy. And this is the stanza you'll always be using to copy your 35 00:02:32,290 --> 00:02:38,050 ‫source code from your local machine, or your build servers, into your container images. 36 00:02:38,110 --> 00:02:43,640 ‫In this case, we're just taking our simple index.html and we're overwriting the file in the 37 00:02:43,640 --> 00:02:48,020 ‫Nginx default directory so that it's our custom home page for the web server. 38 00:02:48,280 --> 00:02:53,110 ‫Before we build this, you'll notice that we're missing required stanzas, like the cmd. 39 00:02:53,110 --> 00:02:54,480 ‫So how can we get away with that? 40 00:02:54,610 --> 00:02:59,490 ‫Well, there's already a cmd specified in the FROM image. 41 00:02:59,710 --> 00:03:06,370 ‫And when we use the FROM, we inherit everything from the Dockerfile we're 'froming,' if that's even a word. 42 00:03:06,730 --> 00:03:11,260 ‫This is how you can chain Dockerfiles together so that images depend on other images that depend 43 00:03:11,260 --> 00:03:13,060 ‫on other images. 44 00:03:13,060 --> 00:03:15,670 ‫All right. Let's see how this works. 45 00:03:15,670 --> 00:03:22,320 ‫Before we build this, let's just quickly remind ourselves what a default Nginx server does. 46 00:03:22,330 --> 00:03:32,200 ‫Let's do a quick docker container run -p 8080, and we're not going to need this very long so we're 47 00:03:32,200 --> 00:03:37,450 ‫going to do a --rm so it will delete on quitting. We don't care about the name. We're just 48 00:03:37,450 --> 00:03:43,060 ‫going to run Nginx. If we hop over to our browser and just type in localhost, we see the default 49 00:03:43,120 --> 00:03:45,010 ‫Nginx index page. 50 00:03:45,040 --> 00:03:45,270 ‫OK. 51 00:03:45,280 --> 00:03:51,070 ‫Let's remember that, and then let's just quit this container and we're going to now build our version 52 00:03:51,070 --> 00:03:56,600 ‫of Nginx with our extra source code file. 53 00:03:57,430 --> 00:03:57,880 ‫OK. 54 00:03:57,880 --> 00:03:59,610 ‫This one should actually go really quick. 55 00:03:59,630 --> 00:04:05,230 ‫You notice it only took a few seconds. That's because we already had Nginx in our image cache. And 56 00:04:05,230 --> 00:04:10,480 ‫all it was running was copying a very small file and changing the working directory. 57 00:04:10,480 --> 00:04:15,490 ‫Now let's run the exact command we just ran for Nginx. Only this time, we're going to use this 58 00:04:15,490 --> 00:04:18,590 ‫new image we just built. 59 00:04:19,180 --> 00:04:22,540 ‫Switch over to our browser, and refresh, and great. 60 00:04:22,540 --> 00:04:28,330 ‫Our custom index HTML is running in there and our container works. 61 00:04:28,330 --> 00:04:33,590 ‫If we do a quick image ls, we can see our latest image up there at the top. 62 00:04:33,590 --> 00:04:38,130 ‫If we actually wanted to send this up to Docker Hub, it works the same way as we did earlier. 63 00:04:38,180 --> 00:04:42,450 ‫We just need to re-tag it. So we can do docker image tag. 64 00:04:42,550 --> 00:04:48,440 ‫If you remember the help command, we can look at the format and it's always the one we're tagging 65 00:04:48,470 --> 00:04:57,980 ‫and the tag that we want it to then be called. So docker image tag nginx with html latest. 66 00:04:58,160 --> 00:05:04,750 ‫And then I'll have to call it bretfisher...like that. 67 00:05:04,740 --> 00:05:11,750 ‫Then if I do a docker image ls again, you'll see those top two lines are the same image ID again and 68 00:05:11,750 --> 00:05:13,570 ‫just tagged differently. 69 00:05:13,580 --> 00:05:17,880 ‫Now I could do a docker push with that repository and tag, and I could get it up on Docker Hub.