Intro to Docker using a Raspberry Pi 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there my name's Gary Sims and this is Gary explains today I want to look at docker which means we're talking about containers and I want to do that using a Raspberry Pi 4 because it now of course comes in models with 2 gigabytes and 4 gigabytes and I thought using a 4 gigabyte version of the Raspberry Pi 4 would be a good way to try out the capabilities of this board and also to look at docker okay so we're talking about containers now containers are different to virtual machines with a virtual machine for example you would have here it shows here a hypervisor this might be something at the OS level it might be a program let's say like VirtualBox and inside of it you can run entire operating sin where the whole machine is emulated - the graphics cards and the sound cards of the network cards on the CPU and the motherboard and when you run a guest operating system a B you could run Windows you could run Linux you could run FreeBSD and the everything that's running in there it actually thinks it's a real machine it just happens to be done in software in cooperation with the underlying hardware however with a container you actually don't virtualize the operating system what you do is you virtualize you abstract the user environment so that means things like the command line and the libraries and the files that are part of the operating they can all be put into different self-contained packages which we call containers so it's not a virtual machine now because they're just really replicating only the user end of the whole thing then they actually are much more lightweight so in app a you could have maybe Java running in this one you could have could have no js' in this one you might have you know something else to do with network monitoring and they all run in there kind of independent packages but underneath using the docker engine they interact with the underlying operating system to get access to different things like files and so on and because we talked about containers of course you have to have an obligatory picture of a container and so I dug one off one of the royalty-free websites and they have a lot of containers but of course the idea is that with a container everything was in the container once you shut the door it's all self-contained a new shipping out on a lorry or on aura or on a ship on a cargo ship but actually what stays in the container is always the same until finally open with the container and sort of unload and it's the same with the idea with a container on you know on Linux for example everything you need is inside the container and you can have it on Windows you can have it on Linux you have it on arm you have it on x86 but actually everything you need air is inside the container itself ok so to start off with we to install docker now this command here is the normal way to do it that's how you would normally install docker and that automatically takes your operating system and run so that's good for basically all kind of versions of Linux however docker is not yet officially supported on Debian Bastyr which is the version that is using with raspbian that ships with the Raspberry Pi 4 so there's a workaround here and what you do is you to run the first part of the command so it gets the dock or shell file then you need to edit this dock with a file and you need to where it says here 9 and I've given you the few lines of code they need to change it to 10 because that's what buster is but you still tell it it's using stretch and then it'll go ahead and install the packages and they're all compatible it all works so if you're doing Entourage with PI 4 you need to do this hack if you don't go arise with PI 3 or an essay on a Linux machine on Intel then you don't need to worry about this you just need to run this normal docker command like that ok so what we do now is we're gonna go over to our raspberry pi and we can start running some commands in the first commandment gonna run very simply is docker - - version so here we are over on my Raspberry Pi machine and so when they do that docker - - version and there we can see that we're running docker 18 point oh nine point zero and there's the build number okay so the next thing we want to do is actually run a container now they ship it with a very easy container which is called hello world so what you do you say docker that's always all the commands start with docker when you're interacting with the with the docker environment run well that's pretty obvious you want to run something and then the default container they give you is called hello world just like it would be if you were writing a program in C or golang Russ - whatever you always write hello world first and so when we run this actually it fires up this container and it says look hello you've actually made to run this container weld down it will run it for yourself and it says why don't you try something more ambitious and it gives you the command here run this so we're gonna do what it says we're gonna run now not just hello world we're gonna run the ubuntu container so we're gonna take exactly that command few things to notice we're saying run again yes the I am the T basically makes this interactive it means you can talk to it via the command line which of course what we want to do we're gonna run Ubuntu and it's making sure that we run the bash shell in Ubuntu and we're gonna change this here at the end in a minute to show something else you can run so let's run ubuntu bash and now this is why I did this I want to say it doesn't find Ubuntu cuz I haven't got the Ubuntu container so it will automatically download it for us that's absolutely brilliant so this is now going to go off to a repository of containers and I said download Ubuntu and we'll talk more about that repository of containers in a moment so let's just wait for this download to finish okay so the download is finished and we can see now that we are here in this as a root connected to this machine we got the hash sign there so we are actually now running inside of a Ubuntu system now although this is running on raspbian on the PI actually this looks like a Ubuntu system in fact if we do more slash excetera slash OS release it says oh look you're running Ubuntu 18 point 4.0 - but interestingly of course if we do an LS CPU it still tells us that we're running on the cortex a 72 because we are running on the Raspberry Pi if we run for example top we will see that we have kind of you know the four gigabytes of memory here but this is why containers are so great look at this hardly any processes running just the processes that we need not all those processes that are running as part of running a complicated operating system actually just this user land stuff that we need to be running here and finally if we do a you name - a actually what we'll notice is this is not the Ubuntu kernel this is actually the same kernel as we have on our Raspberry Pi and if we go with its other window that I've got here also logged into the same machine if I do you name - I we can see a saw you can do exactly the same thing we can see here 4.19 denigan our new linux arm exactly this any difference here is that is the hostname this is this virtualized this container system here and this is where it says PI because that's actually the name of my Raspberry Pi okay so to exit out of a container all before we do that I just wanted to do one thing I wanted to go to use a slash source that's just a directory here and I want to just create a file ok and we're gonna call it none dot txt ok now this will be important later on but ok so to exit out of the container what you do is you just exit out normally you can press control D or type exit and out we come and we're now back to our normal or Asbury pie underneath now if we now run the same command but we change the bash for top what this shows is we can actually run a container and run a program directly so in this case I want to run the top pros I'm not going to interact with it using the command line it's going to go straight into running that top program so if we fire that up it will start up the container and there is it's just running top and it tells us about the CPU usage in the memory usage and so what this shows is the containers are very good at running specific commands and we're gonna get into this how you can make it run your own programs that you want to do to run your own environments their own apps you're developing inside a container and now when we press Q to quit out of that it actually exits out of the container completely so the container there disappears now we were just talking about the different images let us just say one thing if you do docker images it will list all the different images are available so there's Ubuntu that we downloaded there is hello world and there's some other ones here that we're gonna use and I've pre downloaded them to make our video quicker so we're going to be looking at GCC in a minute we're gonna be looking at node in a minute and so on so let's now just say that you can go over to the docker website and there is a hub of all the different images that are available it's called hobknocker calm and if you go over to browse popular images you can see all the different images not just Ubuntu look the first one is our Java MySQL then you've got you know this the official build here for you know network monitoring busybox Ubuntu there Alpine Linux node which we may use in a minute and then this just goes on and on so there are lots in fact I think I saw a thing at the top of the early go look at that two and a half million images are available now there's one thing to notice when you do pull down image is you've got to be careful who you're putting it down from because you don't know what people have put inside that image so it can be a potential scooter here we have course verified publishers we're obviously we're trusting the oracle of shipping good images there's also official images that come from docker themselves so that's important if you just pull down an image from somewhere you don't know what that person has included and what they're kind of doing in the background just something to be aware of okay so now back onto our Raspberry Pi now you notice that when we ran that command with Ubuntu we went into user source and I and I did I created that file if we go into this now here you will see there is no file that file doesn't not exist in this directory so when you fire up a container actually what happens is is it creates a unique container which it runs for that moment until you exit it and then when you run up a new one you spin up a whole brand-new container however the good thing is the old files are still there so if you do want to go into an old one what we can do is we can say docker container LS which is you know Linux UNIX for listing things - eh all of them and here we can see these are the different ones that we ran here was the Ubuntu that you burn to the Ubuntu and these have IDs connected to them again there's also the hello world there that we ran earlier on so what you can actually do is you can either you can restart one of these old ones so what we're going to do now is we're gonna say docker and we're gonna say start - I now let's this is the one here look we've got that was the first one just ran a second ago that didn't have to fire this is the one where I was running top and then this is the one where we ran Bosch first of all so we're gonna say please run that one and what that will do is it take exactly the same files and restart that container and now if we're going to use a source we can see that files there so there's two things remember first of all is that the container files can be still around if you want them to be but generally when you start to contain it and then you run it it's unique for that moment all the stuff that goes on there is unique and then it goes away of course if you want to keep log files or you want to analyze crashes then you need to keep these around so you can find out what went wrong if they all get deleted then then that can't that might not be helpful when you're trying to do debugging however let if we exit out of here if you want to get rid of all the old ones what you can do is you can say docker container prune okay and that will get rid of all those old images so if we now do docker container LS - hey there's nothing there all gone and you can run that same container here ubuntu in this case again but if you include - - RM then what that does is as we're running this file here once we X out of it the files are automatically deleted so if we do again to list the files there are none there now so including that - - RM will actually make sure the files associated with that container are always to be to win the container exits ok so now I'd like to talk about a thing called a docker file a docker file allows you to create a container that has got some predefined stuff going on inside of it so it's not just vanilla Ubuntu it's not just vanilla the Java container you can actually put your own stuff in that container and get your own stuff to run in the environment that you have chosen the docker file defines what goes on in the environment inside your container and allows you to build your own app into your container and so here is an example of a docker file and you can see it's fairly easy to understand the front part here says that this is based on the GCC which is the you know the c compiler from gu the is based on GCC 4.9 so you can always make sure you get the same version of the compiler as new versions come out and wanting to create this what you going to do is you're going to copy everything that's in our current directory into a directory called my app in the container then when the container is running the working directory will be that my app directory and in prepara in preparing the container it needs to run the compiler for a program called hello WC and then when you run the compiler the container it will run dot hello so what we need to do now is if we go I've got a directory here called hello world GCC and inside of it there are two files one is the docker file that I've just showed you and the other is the hello world program so there are two files in here and when we build the containing it would take that hello WC file and copy into quintet into the container and then actually compile it to create that program and the way you do that is you use this command docker build because obviously we're not running that we're building a container T for tag I'm going to tag this as HelloWorld GCC and I want you to do everything that's in this current directory which is the directory with the docker file in it so if we now go ahead and build our own file there you go it's done that and even as you see here it's run GCC some of this is cache because I've done this before but it goes through that and actually run it and if we now as we did earlier on I went over it quickly but if you look at docker images we can see here hello world GCC because I have that built now and now we can run it so now we can just like we ran the Ubuntu we can run our own version of this now and we can say docker run hello world GCC and we'll do it runs that program hello world hello from within a docker container so now what we see is actually we can write our own program in C in Ruston golang in Python in whatever you want to write it in you can put it into a predefined environment you can build it and you can run it and then you always know that whenever you run your program that container will always have the same environment which means it's much safer rather than you go from one machine to another to another and you don't know the configuration you don't know what they've got installed or the right package installed is the right file system isn't enough you know all the things that you want to check out the right libraries installing is everything there now it's all there because it's this predefined environment that you have control over okay so have a look at another example this time I'm going to be using nodejs I'm saying from node same thing again copy and working directory into this folder called my app a new command here exposed 80s that's saying that this container needs to use port 80 and of course port 80 is what we do for HTTP so when you connect to a website inherently it's clicked into port 80 of that server and then we're saying when you run the container please run node and run index dot j sx j s well let's have a look it is basically a very very simple and stupid in one sense web server that whatever request it get is gonna say hello no js' server here and the date and time we leave you asked for a graphic phylum you ask for an icon it's still going to send the same thing so this is basically completely hopeless when it comes to actually doing anything real well but it shows us that we can run a web server and run that web server inside or the container so let's do that so here we are back on the Raspberry Pi it now I'm gonna go into a directory called node web server and inside we've got our docker file which it just showed you and there is also this index.js file that i just showed you and again we can now build that so that we actually get a proper container using these files and of course we use the build command build and then here - T for the tag simple web server and then everything here it's gone ahead and built that and the same thing again we can run it now there's a couple of things to notice about how we're gonna run this okay first of all notice this here I'm using the - - RM okay but notice this here - P 4080 so what this is saying is port 80 in our program in our webserver gets mapped to port four thousand on my Raspberry Pi and that means that there is now a unique mapping but this container at port 80 and the Raspberry Pi could obviously I might have something else running on my Raspberry Pi at port 80 and also some ports are privileged so this port here means if I now connect to port 4000 on the Raspberry Pi I'm actually kicked into port 80 on the containers it maps from the real world Raspberry Pi through to the containers so if we run this now it's going to come up and say I'm listening on port 80 but of course it's actually listening on port 4000s let's get a web browser out can go in here now the address of my Raspberry Pi is 1.44 which I've got here and I'm connecting to port 4000 so if I now connect to port 4000 on my Raspberry Pi what do I get I get a response from my web server written in nodejs there you go and the date and time so I can refresh this as many times I like and I'm out actually connecting to a web server that's actually running inside the container on my Raspberry Pi but here's the interesting thing because we're running this in container I can run a second one I can run two web servers now so I can now actually run exactly the same command the simple node web server but notice the difference here now I'm going to say map port 4001 to port 80 so I'll have one web server connecting on port 4000 and another web server connection on port 4001 and both web servers think they're working on port 80 because of course that's the standard port number for a web server so here on the second window for that raspberry pi what we can say now is that docker command that I showed you for running the second web server so we do that and we're gonna run it and so notice now it says I'm listening on port 80 because it's a container it thinks it's in its own little contained world or contained environment but actually they're both listening on port 80 but they've been remapped so let's get the web browser again so here is the URL but rather than 4-thousand be gonna say 4001 and now we actually see that we have the second web browser a pointer so here we have one from the first web browser and here we have it from the second web web server so these two web servers are now running independent in their own containers and we can expose them through to the real world you using docker and access them on different ports okay so there you have it an introduction to using docker on a Raspberry Pi for now this really is just an introduction there's loads more stuff to do with services swarms and stacks then there's darker orchestration using things like kubernetes now if you are interested in more dhaka tutorials please let me know in the comments below and we'll see whether it is worthwhile doing a follow up video but otherwise go ahead and try that on your Raspberry Pi 4 you can even try to arise with Pi 3 in it really is a good way of experimenting with containers ok that's it my name is Gary Simms is Gary explained really hope you liked this video please do like please also subscribe and well that's it I'll see you the next one
Info
Channel: Gary Explains
Views: 183,618
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Docker, Containers, Raspberry Pi 4, Pi 4, Arm, docker run, docker build, docker containers, docker images, docker run hello-world, what are containers, what is docker, introduction to docker, 4GB, Pi 4 4GB, Pi 3, Pi 4 2GB, docker run ubuntu
Id: nBwJm0onzeo
Channel Id: undefined
Length: 20min 48sec (1248 seconds)
Published: Thu Jul 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.