Learn Docker in 12 Minutes 🐳

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
docker is a tool for running applications in an isolated environment it gives you advantages similar to running your application inside a virtual machine some of those advantages are your app always runs in exactly the same environment so you don't get inconsistencies in how it behaves if it works on your computer it works on every computer it works on the live server it always acts the same if you're working on multiple projects it lets you sandbox each one and keep them separate good for security and eliminate a potential conflict between different projects and lastly it makes it easier to get going with somebody else's projects you don't have to install all of the tools and dependencies that the project needs you just spin up the virtual machine put your call inside and it works doctor gives you these advantages but without the overhead and hassle of running and managing a virtual machine instead we have containers the code and the environment are all wrapped up inside a container but a container is not a full virtual machine when you're going to virtual machine each machine gets its own full operating system including the kernel the kernel is like the core of the operating system the bit that controls the low-level stuff and this is quite resource heavy on the host machine the computer running the virtual machines containers however they all use the host machines kernel a core bit of the operating system is shared but everything on top of that is still separated everything that makes a Linux distribution unique because all Linux distributions are boon to Debian etc are all built on the same kernel and darker uses special features of the UNIX file system to create these isolated environments so containers are compromised the separation and sandbox is not quite as extreme but it's enough and as a result a container can start up in seconds as opposed to minutes they use fewer resources to take in a blessed disk space and using less memory so a container is a running instance of an image an image is a template for creating the environment you wanted to snapshot of the system at a particular time so it's got the operating system the software the application code all bundled up in a file images are defined using a docker file a doc file is just a text file with a list of steps to perform to create that image for example you to configure the operating system install the software you need copy the project files into the right places etc so you write a darker file then you build that and you get an image which you can then run to get containers so let's try out this whole process first you're going to want to install docker for mac or for windows links are in the description this is just some software to allow docker containers to run on your computer and unless you've got a specific reason to use it ignore docker toolbox that's the older way to run docker on a mac or pc i've created a new folder just on my desktop for this demonstration and i'm going to write a super simple hello world application in PHP it's literally just going to echo hello world and i'm going to save that in a folder called SRC for source as index dot PHP right now you can't execute that file you need a web server our goal is to use docker to create one so let's make a new file and we're going to call this docker file we're going to save it next to the source folder not inside docker file capital d one word and in here we're going to use cold to configure our environment so for this we want an operating system with PHP and Apache installed Apache is the web server software the cool thing though is we don't have to start from scratch we start in our docker file with the name of an existing image an image that has already been built and then we build on top of that conveniently you can find lots of existing images on at the docker hub so if you go to hub docker com sign up the search doesn't seem to work if you logged out you can search for images so we can search for a PHP image now the hub includes images from the whole community so it's up to you to decide if the image is suitable and well-maintained the best ones to look out for are the official ones luckily for us an official PHP image already exists at the top you'll find all the variations of the image these are called tax so we just want to like the latest version of PHP and we want to patch it as well so this line right here this has a few versions of PHP with Apache going left to right they get less specific so this will give you specifically 7.0 point 10 all the way to the end where this will always just give you the latest version of PHP that one's usually a bad idea though because that means PHP could just unexpectedly be upgraded and a might break your old code but one of these are the ones is fine for us now if you scroll down you even get instructions telling you how to use the image if you find the Apache section it tells you what to put in the dockerfile so we first want to define the base image using the from keyword and we want then name of the image PHP then a colon and the name of the tag so we'll use as suggested 7.0 - Apache and then we want to copy our files inside the image using the copy keyword so we want to copy the contents of source into slash var slash www slash HTML there just telling us here that this is where Apache will look on its own file system and to find the file so we should put our files there and you can see now why I called that folder source just so it matches these instructions we want one more thing in our doctor file we want to use the expose keyword to expose port 80 this just means when you run the image and you get a container that container will listen on port 80 by default it will just ignore all incoming requests if you're wondering what operating system this PHP image is based on you can usually find the daka file that it's defined by in this case it's linked next to the tag names and we see it's based on Debian similarly that Debian image will have a doorknocker file and they'll stack on top of each other like I said earlier and this layering of images is a huge advantage of using docker the PHP stock you file is a little bit more complicated than ours but let's just focus on ours for now so when we build our docket file docker is going to download PHP from the docker hub it's going to copy our files from source to this location inside of the image it's going to tell running containers to listen on port 80 and then it's going to output a new image our new customized version which will be able to run so to build it I'm going to go to a terminal first I'm going to move to the folder that it's in so we can see we've got docker file right there and I'm going to type docker a build dash T to give it a name I'm just going to call it hello world and then at the end you want to tell it the location of the docker file and since it's in the current directory we just want to put a dot to say that woops helps if I save the docket file first the first time you do this it'll have to download all of the layers that make up that PHP image shouldn't take too long wanted to got the image it's going to copy our files inside at the end its outputs our new image and it's going to be called hello world so we can run this by typing docker run hello world there's one other thing we need in the middle of this we need to use the dash P tag to forward a port port 80 from the host to port 80 in the container so that means when a request gets the host the host is your computer when a request gets there doctor is going to forward that to the container and when it gets to the container that X pause line that we've got in the docker file that will let the container accept the request and allow Apache to handle it so we can run that we'll get some output from the container from PHP and then we can go to localhost and we'll see hello world so we've done it we've got our application running inside a docker container now if you go back to index dot PHP and you change this when you refresh localhost it won't change the the docker container won't reflect the new version of the file and this is because when we built the image it made a copy of that file to see the change you'd need to rebuild the image and spin up a new container from the updated image during development this is obviously a massive pain and this is where volumes come in so there are two types of volumes want to persist and share data between containers we only have one container I'm not going to talk about this today but the second type lets you share folders between the host and the container you can mount a local directory on your computer as a volume inside the container then the container when it's running we'll be able to see the files that were working on hit control-c to stop this container to mount a volume we're going to add another option to the docker run command we're going to add dash V and we want to tell it to mount the the folder users slash Jake slash desktop it needs to be the full path not a relative one slash docker slash SRC so we want that folder that local folder to be mounted to put a caller inside the container and slash var slash www HTML so the image it copies this folder to this location inside the container but during development we don't just want to copy we want to see that folder we want a live view of that folder so we can mount it at that directory so this time when you run it you'll see changes that we make are reflected straight away as soon as we refresh the docker container can't see that change in the file because it's looking at the file itself so this is really useful during development but before you deploy this and try to run the image somewhere else you will need to rebuild the image to get an updated copy of the files put inside volumes just give a running container the ability to see files on the host machines filesystem they do not change the image so when you're done you can press ctrl C to stop the container again so one last thing I want to mention you can see we can easily stop a container manually by pressing ctrl C but containers will stop by themselves when the main process exits in this case that would only be if PHP died for some unexpected reason but you can equally make containers with short running tasks you might have a container which runs tests or a container which runs composer install the process running in these containers will end when the task is complete and when that main process ends the container will stop so for this reason you should endeavor to have one process per container because the life of that container is tied directly to a single process so you don't want 5 other things going on in the background that will all be brought down when without warning when the main process turn it and the ten it just stops but since containers are really lightweight you can run loads and loads of containers on your computer all at the same time and it's no problem at all so we found a suitable image as it uses a base image on the docker hub we wrote a dock or file to augment that image and then we built that to output our new customized image which we could then run to get a container which would run our application we mounted a volume using the dash V tag and we ended up with a docker container running granters it's a very simple application but it is that easy three line docker file gets this up and running in a future video we'll look at more complex situations and we'll look at orchestration options and deployment options so you can get your container to run a website on the Internet please let me know how you found this video feel free to ask any questions I'll try to answer as many as possible either in the comments or in a future video thanks for watching
Info
Channel: Jake Wright
Views: 1,949,560
Rating: 4.9570875 out of 5
Keywords: jake, wright, computer, science, web, internet, docker, container, image, php, docker hub, howto, how to, tutorial, from scratch, beginners, explained, what is, build, learn, mac, windows, dockerfile, apache, process, virtual machine, dev environment, vagrant
Id: YFl2mCHdv24
Channel Id: undefined
Length: 12min 0sec (720 seconds)
Published: Tue Sep 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.