Developers use configuration files to customize the tools they use every day. Likewise, Docker looks for a Dockerfile for instructions on how it should build an image. In this stage, we're going to learn about all the instructions you can include in a Dockerfile so that your image is configured just the way you want it.
Here are the contents of the files used in the video:
# Base Image FROM ubuntu:latest # Commands to run to install dependencies RUN apt-get update -y RUN apt-get install -y python3 # When you pass commands to the container, what should interpret them ENTRYPOINT ["python3"] # Command to run when the container starts CMD ["app.py"] # Working directory WORKDIR /app # Copy apps from the local folder to the Docker container COPY app.py app.py COPY alternate.py alternate.py # Make port available EXPOSE 8080
#!/usr/bin/env python from http.server import BaseHTTPRequestHandler, HTTPServer class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(bytes("Hello from Python!", "utf8")) return def run(): httpd = HTTPServer(('0.0.0.0', 8080), testHTTPServer_RequestHandler) httpd.serve_forever() run()
#!/usr/bin/env python from http.server import BaseHTTPRequestHandler, HTTPServer class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(bytes("This is an alternate app!", "utf8")) return def run(): httpd = HTTPServer(('0.0.0.0', 8080), testHTTPServer_RequestHandler) httpd.serve_forever() run()
Developers use configuration files to customize the tools they use everyday. 0:04 For example, shells look for 0:09 config files so they know what prompt to display for you. 0:10 What functions to load and so on. 0:13 Programming languages have tools that use configuration files to build an app. 0:16 Likewise, Docker looks for a dockerfile for 0:20 instructions on how it should build an image. 0:23 In this stage, we're going to learn about all the instructions you can include in 0:26 a dockerfile, so that your image is configured just the way you want it. 0:29 A dockerfile is just a plain text file that you store in your application 0:35 directory. 0:39 Dockerfiles let you install application dependencies within the image, 0:40 specify how to run your app, set up networking configuration, and much more. 0:44 When you use the docker command-line interface to build an image, 0:49 it will look for 0:52 your dockerfile, and use the instructions it contains to set up your image. 0:53 Lines of a dockerfile are either comments or instructions. 0:58 If a line has a pound or hash symbol at the very beginning and 1:01 only at the very beginning, it's treated as a comment line and ignored by docker. 1:05 All other lines are treated as instructions. 1:10 An instruction is several of predefined key words followed by one or 1:13 more arguments. 1:17 By convention, an instruction key word should be in all caps, 1:19 although Docker itself doesn't require that. 1:22 You remember the image for our Python web app that we created earlier in the course. 1:25 We installed Python, copied a script from the host file system into the images file 1:29 system, and then set it up to run that script when the container launched. 1:34 Here's the dockerfile that we used to create that image. 1:38 If you want a copy, see the teacher's notes. 1:41 This file illustrates several important commands, 1:44 which we'll go over in the next few videos. 1:47 You can see FROM, RUN, ENTRYPOINT, CMD, 1:49 WORKDIR, COPY, and EXPOSE instructions here. 1:53 To build an image based on this dockerfile, 1:59 we need to run the docker build command from our terminal. 2:01 We need to supply a repository name and tag for 2:05 the image, which we'll do with the -t option. 2:08 We'll use a repository name of sample-web-app. 2:11 Then we need to type a colon character followed by the tag we want to apply. 2:17 We'll use a tag of 1.0. 2:21 Lastly we need to specify the directory that contains our dockerfile. 2:23 Since this is a Python web app I have it stored in the directory called Python. 2:27 If we run this we'll see docker carrying out the instructions in our dockerfile 2:33 one by one. 2:37 It retrieves a copy of the Ubuntu image which our image is going to be based on. 2:39 It runs commands to install Python 3 and 2:43 copies our app source code over from the Python directory to the image. 2:46 It sets our app up to run when the container starts and 2:50 it exposes network port 8080 which our app will be listening for connection on. 2:54 Now that we've created an image, we can run it using the docker run command. 2:59 This image exposes a port. 3:05 But we'll need to publish that port before anyone can connect to it. 3:07 We'll publish the exposed port 8080 to port 8080 on the host, 3:11 with the -p option, -p 8080:8080. 3:18 Then we need to specify the image name and tag we want to run. 3:23 We'll use the same name and tag we specified when 3:26 running docker build, sample-web-app:1.0. 3:31 When we hit enter, the container will run. 3:35 If we make a connection to port 8080, on our dot host with localhost:8080, 3:38 it will be forwarded to the exposed port 8080 on our container. 3:46 We should get a response from our app, which is running within the container. 3:52 Our terminal is still attached to the web app running on the container. 3:56 So we can just go back to our terminal and press Ctrl + C to shut it down. 4:00 Since our web app is the container's main process 4:04 the container will shut down as soon as the app does. 4:07 Before we move on I want to show you a couple possible variations of 4:10 the command to build and tag images. 4:14 Often, you'll be building an image when you're already in the same 4:16 directory that contains your dockerfile. 4:19 If we were to change to the Python directory and 4:22 list files, our dockerfile would be there. 4:25 If this is the case, 4:28 you can just specify the current directory as the one that contains your dockerfile. 4:29 So let's build up the docker build command again and 4:34 edit it to work within the current directory. 4:38 The only thing we need to change is the directory name, 4:41 which we just changed to a single dot, which specifies the current directory. 4:44 You also don't have to specify a particular tag when you're just 4:50 experimenting with different builds. 4:53 When you're running docker build and you need a value to pass for 4:55 the -t option, you can specify just the repository name. 4:58 So in this case, that would be sample-web-app. 5:02 And you can leave the tag portion following the colon off. 5:04 By default, Docker will use a tag of latest. 5:07 The same is true when running a container with docker run. 5:11 You'll still need to specify any ports you want to publish, but 5:16 you can just specify the repository name and leave the tag off. 5:18 Docker will use a default of latest. 5:23 In the next few videos we'll take a look at the instructions used in our 5:26 dockerfile. 5:30 See you there. 5:31
You need to sign up for Treehouse in order to download course files.Sign up