1 00:00:04,476 --> 00:00:09,460 Developers use configuration files to customize the tools they use everyday. 2 00:00:09,460 --> 00:00:10,990 For example, shells look for 3 00:00:10,990 --> 00:00:13,790 config files so they know what prompt to display for you. 4 00:00:13,790 --> 00:00:16,300 What functions to load and so on. 5 00:00:16,300 --> 00:00:20,880 Programming languages have tools that use configuration files to build an app. 6 00:00:20,880 --> 00:00:23,930 Likewise, Docker looks for a dockerfile for 7 00:00:23,930 --> 00:00:26,370 instructions on how it should build an image. 8 00:00:26,370 --> 00:00:29,900 In this stage, we're going to learn about all the instructions you can include in 9 00:00:29,900 --> 00:00:33,570 a dockerfile, so that your image is configured just the way you want it. 10 00:00:35,262 --> 00:00:39,390 A dockerfile is just a plain text file that you store in your application 11 00:00:39,390 --> 00:00:40,425 directory. 12 00:00:40,425 --> 00:00:44,190 Dockerfiles let you install application dependencies within the image, 13 00:00:44,190 --> 00:00:48,240 specify how to run your app, set up networking configuration, and much more. 14 00:00:49,270 --> 00:00:52,100 When you use the docker command-line interface to build an image, 15 00:00:52,100 --> 00:00:53,030 it will look for 16 00:00:53,030 --> 00:00:58,140 your dockerfile, and use the instructions it contains to set up your image. 17 00:00:58,140 --> 00:01:01,940 Lines of a dockerfile are either comments or instructions. 18 00:01:01,940 --> 00:01:05,640 If a line has a pound or hash symbol at the very beginning and 19 00:01:05,640 --> 00:01:10,550 only at the very beginning, it's treated as a comment line and ignored by docker. 20 00:01:10,550 --> 00:01:13,930 All other lines are treated as instructions. 21 00:01:13,930 --> 00:01:17,650 An instruction is several of predefined key words followed by one or 22 00:01:17,650 --> 00:01:19,110 more arguments. 23 00:01:19,110 --> 00:01:22,510 By convention, an instruction key word should be in all caps, 24 00:01:22,510 --> 00:01:25,070 although Docker itself doesn't require that. 25 00:01:25,070 --> 00:01:29,630 You remember the image for our Python web app that we created earlier in the course. 26 00:01:29,630 --> 00:01:34,220 We installed Python, copied a script from the host file system into the images file 27 00:01:34,220 --> 00:01:38,650 system, and then set it up to run that script when the container launched. 28 00:01:38,650 --> 00:01:41,870 Here's the dockerfile that we used to create that image. 29 00:01:41,870 --> 00:01:44,620 If you want a copy, see the teacher's notes. 30 00:01:44,620 --> 00:01:47,190 This file illustrates several important commands, 31 00:01:47,190 --> 00:01:49,480 which we'll go over in the next few videos. 32 00:01:49,480 --> 00:01:53,596 You can see FROM, RUN, ENTRYPOINT, CMD, 33 00:01:53,596 --> 00:01:59,090 WORKDIR, COPY, and EXPOSE instructions here. 34 00:01:59,090 --> 00:02:01,480 To build an image based on this dockerfile, 35 00:02:01,480 --> 00:02:05,690 we need to run the docker build command from our terminal. 36 00:02:05,690 --> 00:02:08,360 We need to supply a repository name and tag for 37 00:02:08,360 --> 00:02:11,790 the image, which we'll do with the -t option. 38 00:02:11,790 --> 00:02:17,080 We'll use a repository name of sample-web-app. 39 00:02:17,080 --> 00:02:21,300 Then we need to type a colon character followed by the tag we want to apply. 40 00:02:21,300 --> 00:02:23,760 We'll use a tag of 1.0. 41 00:02:23,760 --> 00:02:27,950 Lastly we need to specify the directory that contains our dockerfile. 42 00:02:27,950 --> 00:02:32,170 Since this is a Python web app I have it stored in the directory called Python. 43 00:02:33,670 --> 00:02:37,730 If we run this we'll see docker carrying out the instructions in our dockerfile 44 00:02:37,730 --> 00:02:39,200 one by one. 45 00:02:39,200 --> 00:02:43,720 It retrieves a copy of the Ubuntu image which our image is going to be based on. 46 00:02:43,720 --> 00:02:46,020 It runs commands to install Python 3 and 47 00:02:46,020 --> 00:02:49,680 copies our app source code over from the Python directory to the image. 48 00:02:50,980 --> 00:02:54,410 It sets our app up to run when the container starts and 49 00:02:54,410 --> 00:02:59,600 it exposes network port 8080 which our app will be listening for connection on. 50 00:02:59,600 --> 00:03:03,315 Now that we've created an image, we can run it using the docker run command. 51 00:03:05,872 --> 00:03:07,830 This image exposes a port. 52 00:03:07,830 --> 00:03:11,460 But we'll need to publish that port before anyone can connect to it. 53 00:03:11,460 --> 00:03:18,540 We'll publish the exposed port 8080 to port 8080 on the host, 54 00:03:18,540 --> 00:03:23,380 with the -p option, -p 8080:8080. 55 00:03:23,380 --> 00:03:26,840 Then we need to specify the image name and tag we want to run. 56 00:03:26,840 --> 00:03:31,147 We'll use the same name and tag we specified when 57 00:03:31,147 --> 00:03:35,993 running docker build, sample-web-app:1.0. 58 00:03:35,993 --> 00:03:38,890 When we hit enter, the container will run. 59 00:03:38,890 --> 00:03:46,270 If we make a connection to port 8080, on our dot host with localhost:8080, 60 00:03:46,270 --> 00:03:52,600 it will be forwarded to the exposed port 8080 on our container. 61 00:03:52,600 --> 00:03:56,596 We should get a response from our app, which is running within the container. 62 00:03:56,596 --> 00:04:00,300 Our terminal is still attached to the web app running on the container. 63 00:04:00,300 --> 00:04:04,650 So we can just go back to our terminal and press Ctrl + C to shut it down. 64 00:04:04,650 --> 00:04:07,480 Since our web app is the container's main process 65 00:04:07,480 --> 00:04:10,550 the container will shut down as soon as the app does. 66 00:04:10,550 --> 00:04:14,090 Before we move on I want to show you a couple possible variations of 67 00:04:14,090 --> 00:04:16,720 the command to build and tag images. 68 00:04:16,720 --> 00:04:19,510 Often, you'll be building an image when you're already in the same 69 00:04:19,510 --> 00:04:22,330 directory that contains your dockerfile. 70 00:04:22,330 --> 00:04:25,300 If we were to change to the Python directory and 71 00:04:25,300 --> 00:04:28,440 list files, our dockerfile would be there. 72 00:04:28,440 --> 00:04:29,480 If this is the case, 73 00:04:29,480 --> 00:04:33,240 you can just specify the current directory as the one that contains your dockerfile. 74 00:04:34,810 --> 00:04:38,440 So let's build up the docker build command again and 75 00:04:38,440 --> 00:04:41,200 edit it to work within the current directory. 76 00:04:41,200 --> 00:04:44,770 The only thing we need to change is the directory name, 77 00:04:44,770 --> 00:04:48,520 which we just changed to a single dot, which specifies the current directory. 78 00:04:50,460 --> 00:04:53,780 You also don't have to specify a particular tag when you're just 79 00:04:53,780 --> 00:04:55,730 experimenting with different builds. 80 00:04:55,730 --> 00:04:58,800 When you're running docker build and you need a value to pass for 81 00:04:58,800 --> 00:05:02,210 the -t option, you can specify just the repository name. 82 00:05:02,210 --> 00:05:04,900 So in this case, that would be sample-web-app. 83 00:05:04,900 --> 00:05:07,830 And you can leave the tag portion following the colon off. 84 00:05:07,830 --> 00:05:11,920 By default, Docker will use a tag of latest. 85 00:05:11,920 --> 00:05:14,860 The same is true when running a container with docker run. 86 00:05:16,020 --> 00:05:18,970 You'll still need to specify any ports you want to publish, but 87 00:05:18,970 --> 00:05:23,990 you can just specify the repository name and leave the tag off. 88 00:05:23,990 --> 00:05:26,770 Docker will use a default of latest. 89 00:05:26,770 --> 00:05:30,055 In the next few videos we'll take a look at the instructions used in our 90 00:05:30,055 --> 00:05:31,020 dockerfile. 91 00:05:31,020 --> 00:05:31,520 See you there.