Docker for NET Developers: Dockerfile Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends and welcome my name is Steve and in this docker for dotnet developers video will look at getting started with docker file basics we're going to explore how we can add a docker file to an existing asp.net co-op solution and use that dog file to build a docker image and eventually run a container from that image to create a docker file we need to add a basic text file to this solution by convention that's going to be called docker file and it's not going to have an extension now adding a file without an extension in visual studio is a bit of a pain so I'm going to open up Windows Explorer to do that piece of the work okay so I've got Windows Explorer open here and we're in the root of the project that we're working on inside Visual Studio I'm going to right click I'm going to choose new and text document we're going to give this the name docker file we're gonna not put an extension on this file windows is gonna warn us about that extension change we're going to say yes that's fine and we now have an empty dock file in our solution we're done now in Windows Explorer so we'll close that down in visual studio you will see that the docker file we just created now appears underneath our project and we can double click that docker file to begin editing i'm going to copy in some commands that i created earlier and then we'll explore how each of those commands is used to build a final docker file that can be used to create a docker image now i would like to stress that the docker image that will get a create from this docker file is not going to be production ready for example it's not going to have an optimal file size the first command is the front command and the front command will allow us to specify an image that we want to use as the base for this new image we're creating docker files and docker images are a layered architecture and each image can layer in new dependencies that we may need later on in this case we're using the microsoft asp net core build image and this image layers in things such as the sdk and ms build that we'll need later on when we want to run the dotnet commands this image is available on the docker hub which is a public registry of images in this case we've also specified a : 2.0 and this : is the marker between the image name and the image tag in this case the tag is 2.0 which specifies we want a particular 2.0 version of this image and in this case that corresponds directly to the 2.0 SDK version inside this base image the next command is the working directory command and this allows us to set the directory that we're working in as we build this docker image in this case we've set it to slash app the nips command is the copy command and this allows us to copy files from our local machine into the docker image that we're building the copy command takes two arguments a source and a destination the source will be relative to the build context where the docker commands are being run and in this case we'll be running those docker commands from the directory where our docker file was created the dots here for the source specifies that we want to copy everything from that build context area into the docker image that will include all files and all folders the destination here is relative to the working directory and the working directory will be that slash app folder that we specified above so in this case by specifying the dot we're saying that we want to copy directly into that app folder next we have run and this allows us to run commands inside the docker image as we're building it in this case I'm using the.net build command with the configuration of release this is going to build our source code inside the interim image finally we have entry point an entry point will allow us to specify the command that is run when a container is started from this docker image in this case we're using dotnet run with the no launch profile flag this command will fire up our application with kestrel listening for web requests as long as the donut XE process is running the container will remain running to the no launch profile flag in this case is needed because we haven't specified very explicitly what files we wanted to be into this docker image so all of the files under our project directory will have got copied and that will include things like the launch Settings JSON file by default asp.net core 2-0 will now look at that launch settings file and use the port and environment from that file if it finds it in this case we don't want to do that once this image is running I want it to be running on the default asp.net core port or 80 so by adding the no launch profile flag I can avoid that launch settings file and its values being used and that's it that's all we need in order to create a docker file that is suitable for building a docker image to run an air speed net core application I will stress once more that this is not going to be a production ready image but it's enough to get us going for demo purposes here the next step is to use the dock commands to build a docker image from this docker file so we'll open up the command prompt and explore how we can do that inside this command window you'll see that we're in the path where the docker file that we created earlier is located and that's at the root of that project for our asp.net core application what we're going to do now is use that docker file to create a docker image for our application so I'm going to do that using the docker build command now when we create a docker image we want to be able to reference that image later to be able to start a container from it and so in order to do that we can give it a name and optionally a tag we do both of those using the - - tag option inside this docker build command and then we can pass in the name that we want to give our image in this case I'm going to call it docker demo I'm not going to give it an explicit tag because we don't need to version this particular demo image the final element of this command is the path to the build context the build context specifies all of the files that we want to potentially make up our build in this case we're already running the docker command from the path that will act as our build context and so we can pass a dot if I hit enter that will start the build process for our docker image okay so docker build has completed there you can see that successfully built statement at the bottom and you could also see the successfully tagged statement as well that has given it the name of docker demo but default because we didn't provide a specific tag for this it's been given the tag of latest so the next step is we actually want to run this docker image as a container to do that I'm going to use docker run and when I run a docker image we can pass in arguments and options that we want to use when running that particular instance of the image of the container in this case I'm going to pass in a particular command that allows me to publish ports from the container so what I'm doing here is I'm publishing the port 80 of the container to 6001 on my host and that's needed so that we can actually communicate with the application that's running inside that container which is listening on port 80 if we didn't do that we wouldn't have a way to pass the address from our localhost through into the container finally I need to provide the name of the docker image that I want to run with this docker run command and I'm going to pass in docker demo which is the name we tagged our image with when we created it just a few moments ago now I can hit enter and we'll see that we've start our container so there is our container running and listening on port 80 internally our application is started and we should be able to communicate with this now using a Chrome window which I'll open up inside the Chrome application I'm going to paste in the address to communicate with our application I'm using localhost 6,001 so that's the port that we passed through we'll publish through to the port 80 of the container and then I need to put in the root of slash API slash values which is the default route for the default controller of the API template so if I hit enter we should see that we get some values back as we do here so those values are being generated inside the application that's running inside the container that we started up so there you have it we've explored how we can add a docker file to an existing asp.net core application using a few basic commands in that file build up a docker image and then run and container from that image thank you very much for watching and see you again soon you
Info
Channel: Code With Steve
Views: 28,729
Rating: undefined out of 5
Keywords: docker, dockerfile, asp.net core, asp.net, containers, docker build, docker run
Id: grbc8mevLRc
Channel Id: undefined
Length: 9min 37sec (577 seconds)
Published: Sat Dec 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.