1 00:00:00,480 --> 00:00:03,430 Let's dive in a little deeper to get a better understanding of 2 00:00:03,430 --> 00:00:05,280 what Docker is useful for. 3 00:00:05,280 --> 00:00:08,220 We'll be going into much more detail in the remaining stages. 4 00:00:08,220 --> 00:00:10,070 But for now, let's try to get a broad overview. 5 00:00:11,170 --> 00:00:14,460 Developers using Docker don't have to worry about installing and 6 00:00:14,460 --> 00:00:17,760 configuring complex supporting software like databases. 7 00:00:17,760 --> 00:00:21,300 Or worry about which version of a language an app is built on. 8 00:00:21,300 --> 00:00:23,450 When a developer Dockerizes an app, 9 00:00:23,450 --> 00:00:28,370 all the complexity of building the app is pushed into what Docker calls containers. 10 00:00:28,370 --> 00:00:31,750 You can think of these like shipping containers software. 11 00:00:31,750 --> 00:00:33,820 Containers are easily built, run, and 12 00:00:33,820 --> 00:00:37,260 shared by any developer with access to the Docker file. 13 00:00:37,260 --> 00:00:39,780 Docker's own website describes it as the world's 14 00:00:39,780 --> 00:00:42,040 leading software container platform. 15 00:00:42,040 --> 00:00:45,010 Docker is used by developers to ensure their apps work on 16 00:00:45,010 --> 00:00:46,930 every machine they're deployed to. 17 00:00:46,930 --> 00:00:51,140 And operations staff use Docker to better scale systems in production. 18 00:00:51,140 --> 00:00:55,390 Enterprise companies use Docker to build agile software delivery pipelines, so 19 00:00:55,390 --> 00:00:58,670 they can ship new features quickly, with better security. 20 00:00:58,670 --> 00:01:03,270 Docker lets enterprises deploy to both Linux and Windows Server easily. 21 00:01:03,270 --> 00:01:06,190 Employees joining a new project no longer have to wait 22 00:01:06,190 --> 00:01:08,960 hours while the supporting software installs. 23 00:01:08,960 --> 00:01:12,715 You don't have to carefully explain how to setup various services. 24 00:01:12,715 --> 00:01:17,370 Docker files abstract away the installation of dependencies, allowing you 25 00:01:17,370 --> 00:01:21,780 to easily package the app for running and test or production environments. 26 00:01:21,780 --> 00:01:24,160 If you've worked with virtual machines before, 27 00:01:24,160 --> 00:01:28,420 containers may seem similar to those, and they are somewhat similar. 28 00:01:28,420 --> 00:01:30,946 Both allow you to install a set of apps and 29 00:01:30,946 --> 00:01:36,015 the services they depend on without mixing them what the software on the host OS. 30 00:01:36,015 --> 00:01:39,429 But containers don't emulate a virtual CPU, memory, and 31 00:01:39,429 --> 00:01:41,894 other hardware like virtual machines do. 32 00:01:41,894 --> 00:01:44,585 They run directly on the host computer's hardware, 33 00:01:44,585 --> 00:01:47,480 making them more efficient in many situations. 34 00:01:47,480 --> 00:01:51,580 To run an app in a Docker container, you first need to write a Docker file. 35 00:01:51,580 --> 00:01:55,651 The syntax for these files is simple, and there are thousands of existing examples 36 00:01:55,651 --> 00:01:58,650 for you to pull from when packaging your apps. 37 00:01:58,650 --> 00:02:02,045 Once a Docker file is made, you can use the Docker commandline 38 00:02:02,045 --> 00:02:04,866 interface to build an image from your Docker file. 39 00:02:04,866 --> 00:02:08,623 An image is essentially a binary file that contains the app defined by 40 00:02:08,623 --> 00:02:10,170 the Docker file. 41 00:02:10,170 --> 00:02:14,030 Then you can share the image with others who can run it as a container. 42 00:02:14,030 --> 00:02:16,380 Suppose we have this Python web app. 43 00:02:16,380 --> 00:02:19,580 It's a really simple app consisting of a single file. 44 00:02:19,580 --> 00:02:24,373 All it does is listen for browser requests on port 8080 and 45 00:02:24,373 --> 00:02:26,965 respond with hello from Python. 46 00:02:26,965 --> 00:02:30,383 But even something this simple introduces a dependency, 47 00:02:30,383 --> 00:02:34,160 we have to have Python 3 installed in order to run it. 48 00:02:34,160 --> 00:02:38,350 And it looks like this machine only has Python 2 installed. 49 00:02:38,350 --> 00:02:43,883 So we get an error when we try to launch this app.py program, python app.py. 50 00:02:43,883 --> 00:02:49,050 And you can see here the import error No Module named http.server. 51 00:02:49,050 --> 00:02:51,617 So let's try using Docker to create an image that 52 00:02:51,617 --> 00:02:55,640 bundles the gap together with the correct version of Python. 53 00:02:55,640 --> 00:03:00,560 We'll need a file named Docker file with a capital D and no extension. 54 00:03:00,560 --> 00:03:04,060 And we'll put this together in the same directory that contains our app. 55 00:03:04,060 --> 00:03:09,678 This Docker file will create another new image based on an Ubuntu Linux image. 56 00:03:09,678 --> 00:03:12,316 It'll install Python 3 on top of it. 57 00:03:12,316 --> 00:03:16,490 It'll copy the app,py file, plus another file that we'll use later 58 00:03:16,490 --> 00:03:20,010 from the current directory on the host into the image. 59 00:03:20,010 --> 00:03:24,876 It'll set app.py to run via Python 3 whenever a container that's based on 60 00:03:24,876 --> 00:03:26,112 the image starts. 61 00:03:26,112 --> 00:03:30,926 And it'll ensure that outside apps can connect to the container 62 00:03:30,926 --> 00:03:35,576 on port 8080, so that they can communicate with our app. 63 00:03:35,576 --> 00:03:37,590 Now let's actually build the image. 64 00:03:37,590 --> 00:03:41,892 We'll run the docker command with the build subcommand, and 65 00:03:41,892 --> 00:03:50,130 use the -t flag to tag the image with the name sample-web-app. 66 00:03:50,130 --> 00:03:54,290 Lastly, we'll tell it to look for a Docker file in the current directory. 67 00:03:54,290 --> 00:03:58,360 As before, Docker build will go through the instructions in our Docker file 68 00:03:58,360 --> 00:03:59,510 one by one. 69 00:03:59,510 --> 00:04:03,772 Creating an image based on Ubuntu, installing Python, 70 00:04:03,772 --> 00:04:08,227 copying our app into the image and exposing port 8080. 71 00:04:08,227 --> 00:04:12,876 We can confirm that our image was created successfully with the docker 72 00:04:12,876 --> 00:04:14,070 images command. 73 00:04:15,700 --> 00:04:20,150 We'll see an image with our sample web app tag in the resulting list. 74 00:04:21,260 --> 00:04:23,550 Now let's create a container based on the image and 75 00:04:23,550 --> 00:04:25,800 try connecting to our Python server. 76 00:04:25,800 --> 00:04:31,890 We'll use the Docker run command, we'll publish our exposed port 8080 as 77 00:04:31,890 --> 00:04:39,550 port 8080 on the host with -p 8080:8080. 78 00:04:39,550 --> 00:04:47,459 And select the image to use by providing our tag of sample-web-app. 79 00:04:47,459 --> 00:04:49,028 Our container will start, and 80 00:04:49,028 --> 00:04:53,280 we'll run our Python app just like we specified in the Docker file. 81 00:04:53,280 --> 00:04:56,900 If we switch to our web browser and direct it to localhost 82 00:04:59,620 --> 00:05:06,250 port 8080, it will connect to port 8080 on our host, be passed through to 83 00:05:06,250 --> 00:05:11,770 port 8080 on the container, and our Python app will respond with Hello from Python. 84 00:05:11,770 --> 00:05:14,450 Because the container is still attached to our terminal, 85 00:05:14,450 --> 00:05:18,850 we can shut it down by going back to the terminal and pressing Ctrl+C. 86 00:05:18,850 --> 00:05:23,450 That will stop the main Python process and the container will then shut down. 87 00:05:23,450 --> 00:05:28,078 If we go back to our browser and Reload, we'll see that we no longer get 88 00:05:28,078 --> 00:05:32,192 a response because on the container that was responding before has shutdown. 89 00:05:32,192 --> 00:05:35,720 Docker is useful for building and deploying single apps or 90 00:05:35,720 --> 00:05:40,872 services, but it's even better when you're building complex distributed systems. 91 00:05:40,872 --> 00:05:43,385 There are existing tools such as Docker Compose, 92 00:05:43,385 --> 00:05:47,670 which you can think of is a Docker file for multiple Docker containers. 93 00:05:47,670 --> 00:05:51,730 And Docker Swarm, which allows you to build, deploy, and monitor multiple 94 00:05:51,730 --> 00:05:56,880 Docker containers at once, either as a single service or as a set of services. 95 00:05:56,880 --> 00:05:59,570 Docker also has a rich networking API. 96 00:05:59,570 --> 00:06:02,060 And connecting containers to the outside world or 97 00:06:02,060 --> 00:06:05,690 other Docker containers is straightforward and easy. 98 00:06:05,690 --> 00:06:09,730 Hopefully, now you have a better understanding of exactly what Docker is. 99 00:06:09,730 --> 00:06:14,160 Up next, we'll discuss why you should be using Docker right now for your projects.