Creating your first Dockerfile, image and container

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this episode we're going to be looking at building a very simple very basic image and container from your own docker file now I've gone ahead and pulled up the docker file reference page here and if you're looking for it just do a quick search for docker file it's typically the first thing that pulls up click on that and it pulls up the docker file reference pretty quickly now there's a lot of really good information inside of this docker file reference that will tell you everything that you could possibly want to know about all the different commands that you can put into your doctor file and unfortunately it will tell you everything that you really need to know about putting things into your docker file which means there is quite frankly an overwhelming amount of information at times when you're just getting started this reference is not really the best place to go so I want to simplify things for you we're going to build the most simple smallest possible image that we can from the two commands that we need in order to do that and that's going to be the from command as well as the CMD command or command you know so let's get started with this real quick I'm going to drop over to the command line and I've got an empty folder at this point and I'm going to touch a docker file and I'm going to use a capital D with this because the docker build command is case sensitive when it looks for docker file with capital D yes you can specify any other file name that you want doesn't even have to be a dot docker file it could just be any random name like Batman or George or whatever you want it to be but it's easier if you go ahead and use the capital D docker file after I've done that I'll open up my editor and we'll start looking at what goes into this particular file now as I've said a few times before most docker images and containers have a base Linux installation inside of them and by most I really mean like 98 99 out of a hundred times the yeah it is possible for you to build a docker image without a base Linux installation but you're talking about C++ and other really low-level compilers in order to get down into almost an operating system level sure you could do it and there are some people that do do it I mean who's building the Linux distributions after all but it's not something that you generally want to do most of the time you want to start with a base Linux installation and for that I prefer to use Alpine Linux most of the time because it's incredibly small it's like five Meg's or less to get an Alpine Linux install up and running inside of docker vs. Ubuntu can be a couple of hundred Meg's and it can grow really fast once you start adding a lot more complicated pieces and parts now when we get to a full nodejs build later on yeah it's going to be a pretty hefty sized image because of everything that we need but to start with we're going to go with alpine Linux and we're going to create a very basic installation with a simple command now as I mentioned previously we're going to use two commands inside of this docker file we're going to use from and we're going to specify alpine linux as the from command this from what it really does is it specifies the base image from which you're building your docker file and your new image now Alpine is an existing image inside of the official docker hub if I look up docker Alpine we will see that there is an alpine linux repository on the official docker hub website we don't have to download it in advance or anything we can just specify from Alpine and we're good to go now aside from the from command there's one other thing that you need and that is the CMD command or command command really this is going to be an array that specifies the command and any parameters that we want to execute if you've done any kind of shell scripting from Mel J S or other languages you're probably familiar with having to specify each individual part each parameter as an individual part of an array and that's kind of what's happening here so to start with let's go ahead and run echo hello world this is going to be the most basic and simple docker image and container that we could possibly imagine I'm going to save that I'm going to head back to my command line and we're going to build this so we're going to say docker build now the docker bilk man needs at least one parameter and that is the folder from which to build so I'm going to say docker bill dot because the docker file is in my current folder now once I run that you'll see a couple of different things happen you'll see step one from Alpine I've already got Alpine Linux installed if you don't have it installed it will go through a download process for that base image but once it's gone through that step one it's going to create a temporary image inside of our docker configuration and then it's going to move on to the next line of our docker file which is the command echo hello world now we've got another docker image created and it's got an output here that's the last part of our docker file so it's removing this previous intermediate container that I had created and it's telling us that we have successfully built this container with this particular ID now if you're used to the pool command with named images that we've seen previously that's great it's good to be able to do that but right now we don't have a named image we just have the image ID and you can use image IDs for any image that you want even if you have a named image you could go ahead and specify the ID and it would still work so what I'm going to do I'm going to copy and paste this I'll just copy the this and I'll say docker run I'll give this name - - name we'll call this desk and we'll paste in the image ID if you think back to the previous episodes you'll remember that the docker run command will turn an image into a container so let's see what happens when we run this ah oops I've already got one called test let me remove that real quick dr RM test and i will rebuild like this ooh echo hello world does not exist because I made a mistake in locating the executable here I was telling you about specifying an array with the command and parameters and I totally messed that up okay I'm gonna run echo hello world let's rebuild this container I'm going to docker RM test again and I will rerun docker build dot now I will docker run - - name paste in the ID that's our theme test sorry about that paste in the ID and there we have it we have hello world as an output we have built our very first docker image and container and how incredibly useless was that I mean it's your typical hello world right not exactly the most useful thing in the world so let's let's add a little more to this let's take what we just did and let's do something a little more with it let's let's take that hello world echo out of the docker container and into a script file that we're going to put into the container and this will start showing us a little more of what docker can actually do so instead of saying command echo hello world like I did here I'm going to edit a script SH and this is a terrible name I know but it doesn't really matter once I've got a script SH file here I'm just going to say hello world from a script file now again not the most useful command ever but I also do need to remember that I need the hash Bank so hash-bang slash bin slash SH that way it knows it's a shell command also once I have that in place I need to make this executable so I will run chmod plus x on the current file that will make the file executable there we see in them all right so now I've got a good script sh file and i want to copy this file into my docker image and container so that it can be executed in order to do that I'm going to go back to my doctor file and I'm going to use a new command I'm going to use copy and we're going to say copy script SH 2 / script Sh now this / script SH this is the destination within the docker image or container itself so once I have the image built it will contain a file called script SH at this location at the slash location inside of the Alpine Linux box now if you want to put this inside of a subdirectory you certainly can and we'll be doing that later on especially with our node images and containers that we're building but for now I'm just going to put this on the root once we have that file copied in I'm going to again use the CMD command and I'm going to add my square brackets and I'm going to say / script SH now I don't really need any parameters at this point so we'll go ahead and save that and we'll rerun that process I showed you earlier I'm going to docker RM test docker build and here in the build output we can see step two is now copying script SH to this location we created an intermediate container at this point we're running the CMD command to say script SH is where we're starting running the command when we actually run the container and then we have successfully built this image ID so now I'm going to do docker run - - name test specify the image ID run that and again we get the hello world output well it's you know a little more interesting because we're actually doing something inside of the docker container we're doing something with the file system but it's not a great example of a docker image or container because it just spits out that hello world and it then it exits not the most useful thing in the world so let's do something a little different than this still let's change this script SH instead of just doing the echo hello world let's run the top command now if you're not familiar with the top command in the linux world in bash world you can run top and it will give you system information about the processes that are currently running in your system typically you hit command C or control C and it will exit out of the top command so I've added top to my script here I'm going to rerun that little process of deleting and rebuilding and the output of this pretty much looks the same we've only modified the script SH we haven't modified anything else I'm going to rerun the run command but I'm going to change the ID so I get the right image to run from and in this case we have the top command running inside of the container which is great we've got something you know that theoretically is more useful it's it's an actual running process inside of the system and there's something very important that you're going to see inside of this top command and that's this first line item right here that's this PID one and if you look PID one is set to the script SH this is important because PID one inside of a docker container is the process that docker uses to determine when it should exit when PID one exits the entire docker container exits so when you have a docker container that you're building an image and a container that you're running from that image you need to know that the very first thing that starts up inside of your container should be PID 1 and the thing that runs the entire container will have that PID and once this process exits you're going to exit the entire container it's a very important thing to note now I said a moment ago that typically you hit command C or control C to exit out of the the top command in order to exit the shell and you would expect that hitting control C or command C would exit the top shell which would then exit the entire container except there's a small problem I'm hitting command C and it's not working hitting control see here and it's not working so there's another thing you need to note about PID 1 in the linux world and that is that the PID one won't exit when you press control-c it's a special Linux process ID that ignores several of those signals the good news is there is a way to get out of this I'm going to open up another tab and I'm going to say docker stop test and it's going to shut down the actual docker image for me it's going to forcibly exit the top command and everything is pretty much good to go once that is done so that we can see that the stop command exited and we're out of the container on this screen if I run docker PS we don't have any containers running at this point in time so that's pretty much it for this episode that is a very basic docker file with the build process using docker build and then running our image using the image ID to create our container I know it's not the most useful thing in the world in terms of seeing an actual application run but this will give you a good idea of how to start writing your own docker files how to get files from your local file system into the docker container so that they can actually do some things stay tuned for some upcoming episodes though where we look at some more advanced features starting with the ability to actually use command C or control C inside of that top command by introducing a script loader or running process to our command right here we're also going to be looking at the difference between entry point and command inside of our doctor file which is something that frankly confuse me through a little while so those will be in a couple of upcoming episodes thanks for watching this episode and happy doctor
Info
Channel: River Lynn Bailey
Views: 387,410
Rating: undefined out of 5
Keywords: WatchMeCode, Docker, Dockerfile
Id: hnxI-K10auY
Channel Id: undefined
Length: 14min 46sec (886 seconds)
Published: Thu Oct 13 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.