Skill Level Intermediate
- Hi, I'm Shayne Boyer and I'm principal PM on the Cloud Advocate Team. - And I'm Lisa Guthrie. I'm a program manager in Microsoft's developer division. - Great. We're going to talk about using Docker and .NET Core. Lisa, I've been listening and hearing a lot about Docker. What is it and why should I be using it with my .NET Core applications? - Yeah, so, I think of Docker as a way of encapsulating everything that you need to run an application such as a .NET Core application. Including the application code itself, as well as any frame works or anything else. Packages that it depends on. And of course the underlying operating system itself. So, a Docker container is a way to put that all together in to one nice neat package. To build it repeatedly in a repeatable fashion. And to put it wherever you need it. Be it on your development work station or in production. - So, it sounds a little bit like a VM. How is this different from a VM? So, a VM is a machine where I would put all my stuff. But I obviously can't pick up my machine and move it around. So, how is this different from shipping my machine around when it comes to packaging up an application? - Containers are running on the same machine can share certain components. So, a container is always going to be significantly in many cases lighter weight than the equivalent VM because for example you don't need to have an entire copy of the underlying operating system. Some of that is provided by the platform itself. - Okay, awesome. So, what I want to do is I've got a Docker file here. And a Docker file I think is what we need in order to describe what we need. You mentioned that we need to have all of our dependencies inside of this Docker image. So, I'm looking at a Docker file here and the first line that I've got here it says, FROM mcr.microsoft.com/dotnet/core/sdk:3.0. What is this telling me here in this first line? - [Lisa Guthrie] So, everything after that from statement is the name or the path to the base image. Which is that underlying image that your eventual container image will be built on. In this case you've specified that you want to use a base image that has the .NET Core sdk version 3.0 on it. - [Shayne Boyer] Okay. - [Lisa Guthrie] But you could use an earlier version of the .NET Core sdk, you could specify an image that's running something else entirely. But that particular image is out there. It's published by Microsoft. It's available for you to use. And this line just brings that in so that you can use it. It's very similar to having a pre-built VM that has everything on it. But much simpler and smaller. - So, this is the base image. We're establishing kind of everything we're going to work upon. - Exactly. - So, the next line we got it says, WORKDIR, so I'm assuming that means a work directory. And we're getting a little bit of help here from our IDE. And we're establishing it looks like sources where we're going to be working in. - Yes, so that's the directory in this eventual container image that will contain stuff. - Okay. It looks like I have a copy command. And if I look at the help, it looks like we're going from a source and then adds them to file. So, I'm assuming we're copying some files from our directory that we're in and then probably to that work directory, right? - Right. So, at this point you have an operating system which is awesome. You have the .NET Core frame work. Or the .NET Core sdk there on the container image. Which is also awesome. But you don't actually have your application. - Oh, okay. - So, what this does is it copies your application in to that container image so that you can run it there. - [Shayne Boyer] Okay. And then the next thing we got an entry point. And it looks like we're just doing what we would normally do with a .NET Core application. We're saying dot net and run. - [Lisa Guthrie] Exactly. - [Shayne Boyer] So, if I'm looking at the application, let's go back to this line, this copy. What exactly are we copying? We're saying we're copying, I'm assuming my current directory is what we're doing. - [Lisa Guthrie] Right. - [Shayne Boyer] And in this case it's just a Console app, its a Hello World application, right? - [Lisa Guthrie] Yes. - [Shayne Boyer] So, how will we go about building this? I'm assuming that there's going to be some sort commands that we have to run. - [Lisa Guthrie] Yes. - [Shayne Boyer] Here. So, I think I'm going to drop out to our terminal and run some commands. Now, I'm assuming Docker's got a command line of some sort that we're going to execute some commands on, right? - [Lisa Guthrie] It does, yes. Since you're running on a Windows machine, you've got Docker desktop for Windows installed. And there is a CLI that is logically enough called Docker. Which you can use. And there's a Docker build command that you can use. - Much like our .NET CLIs, we have our .NET and then a command, right? - Exactly. - I think that the first thing I'm going to do is do a Docker and then probably build. Right. And then I need to specify a tag. Because I want to tag what I'm building. Kind of name it, right? So, we'll just call this console. Maybe 101. And I then I think the next thing we want to do is probably say what do we want to pass in? Where are we working? Plus the dot here to indicate the current directory that we're in. - Right. - [Shayne Boyer] So, let's go ahead and run this. And see what our output is. - [Lisa Guthrie] Yes. - [Shayne Boyer] So, it looks like we're sending our build context to the Docker daemon and that is that service that runs from Docker desktop for Windows as you mentioned. But executing a couple of other steps here. So, let's maybe look at this a little in depth. It says, step one is at from. So, it looks like I've already downloaded that base image. But if it wasn't there, kind of where would I get that from? Where's that coming from? - [Lisa Guthrie] That's coming out from mcr.microsoft.com which is Microsoft Container Registry. It's a location where we publish the official Microsoft images for things like Windows and .NET Core plus Windows and so on. There are lots of container registries out there. There are probably the best know is Docker Hub. Which is where a number of well known container images are published. You can also publish your own container images. So, for example, if your company had a number or certain frame work that was used in multiple applications, you could publish your own base image that is this plus that frame work. And then that would just be there and available for people to use. - Great. It looks like we're setting our work directories as expected and we're copying those files. And now it says we've built and tagged it and gives them some sort of a hash that I'm running it on. And then the end here, tagged as we asked for. Console 101. And then it looks like a colon latest. What is the colon latest here mean for us here? - [Lisa Guthrie] That's a way that you can do versioning. You can see as well in the mcr, the very first mcr tag that's specifying sdk;3.0. So, that's the .NET Core 3.0, version 3.0. You can have the same base image with different versions. And you can specify the version that you want to use. So, as you've done here in the from statement, you're saying I want to use the version 3.0 of that .NET Core sdk. And you can also use latest, which means I just want to use the latest. So, if for example, 3.1 of the .NET Core sdk were released, automatically your application would start building with that. - Oh, okay. Great. I'm going to clear the screen. Give us a little more space. And now that I have this container image that we've produced, I need to do something with it. Let's see if it's out there. So, I'm going to say Docker. I want to run this image. And I need to pass it the name of what we named it. And I believe it was Console 101. And my expectation is that it's going to run this particular container and I should get maybe Hello World should be the output. Now, I'm not running this executable Windows Console app on my actual machine. They're running inside of the container itself. And as you mentioned I can now package this up and kind of move it around. - Right. - Back and forth between other machines. - Exactly. It would be very easy to bring this on to my machine. And even if I weren't running the .NET Core sdk, everything that I need is in that container image. And so it would run just fine on my machine. - [Shayne Boyer] And as expected we got our Hello World. And you know I feel like a super awesome container developer now with .NET Core. Great. So, we've got our Hello World app running in the container. But I feel like I'm jumping back and forth between visual studio, going to the terminal, I'm running some commands. And that internal loop feels like not so comfortable. It'd be great if visual studio had a way to help us out with that. - Yeah, in fact we have some great container tools built right in to visual studio that make it really easy to work with Docker containers. - Great. Let's check out the next video where we can jump in and look at that.