Docker 🐳 Crash Course 2021 For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys this is docker crash course for beginners okay let's see what you will learn in this course you will know all about docker images containers docker files docker hub and more you will also learn about docker commands and if you have never touched docker before we will go through docker setup first and lastly we will have a closer look to what docker can do by dockerizing a simple python hello world program and also by dockerizing and running a django application from a docker container and we will do everything step by step so don't worry so what is docker really practically speaking it's just a way to package software so it can run on any hardware now in order to understand how that process actually works there are three things you must be aware of docker file docker image and docker container a docker file is just a blue print for building a docker image a docker image is a template for running docker containers and a docker container is just a running process for the docker image so let me explain that in simple words let's for the sake of clarity have a real live developers problem and see how we can solve this problem with docker and understand why docker was created in the first place imagine the following situation you are a developer okay this is you and you're developing a new product in your company using flask for example you finish the job and locally on your machine this software or this app is working perfectly then you want to deploy that on the server but then you discover that there are some dependencies or requirements are missing and we need to have a server that is running the same version of flask and that has also installed these dependencies so the app works perfectly on your computer or on your local machine but if someone else like other users for example try to run that software or that app it will crash because there are a lot of dependencies and requirements for that software to work are missing for example you discovered that there are other users that they have other rules that use these dependencies differently and in conclusion everything starts to get messy and then you start to fix these problems on your machine and you see what's going on now it's not web development anymore it's more of settings and configuration setup for other users and after you do that you think that you did the job you launched the app and it's working fine but no very often you will have a new release of this app with a lot of updates you have specified the requirements or these dependencies for example not only you but other developers and again you will have the same problem that this app works on that system but not the other so you will repeat the same process to modify the settings and so on because there is no unique interface on all computers to run the software in a unique way which is the environment you have created it in and the question now is how much time did you coast your company in terms of money time and resources you see what i mean and the whole point of docker is to solve problems like this by reproducing environments you as a developer who created this application can define the environment with something called docker file and i will talk about docker file later and i will show you a practical example with that but as we saw docker file creates docker image and docker image generates docker container which can run on the server perfectly and then every user can see exactly what you have created no matter how different their operating system is or the dependencies installed on their computers are it doesn't matter so every developer now can rebuild the environment which is saved on this docker image and by the way the docker image is unchangeable it's a read-only file okay no one no user actually can alter the docker image or change in the code if the idea of docker is not clear to you yet you can think of docker as a logistics system in cargo shipping and more specifically maritime or c cargo shipping services you have a big ship that can deliver cargos and these cargos are all different in shape size and form some of these cargos are delivering fruits others are delivering laptops and third cargos delivering cotton for example so here we have a question how we can organize and arrange the shipping of different products on one ship because you can't put laptops on vegetables or fruits for example and here they came up with the idea of containers so let's have containers with fixed size and a unique interface which means how to hold the containers how to lift it up how to move it how to open it and close it for example and inside we will save what we want to save veggies fruits timbers cotton whatever and we will deliver these products in these containers very easily docker works exactly the same the docker engineers thought of the logistics system and containers idea as a way of implementing their approach of delivering software so docker takes your software put it in a container and in this container we will put also some necessary dependencies um other files which compose the convenient environment for that software to work properly so docker uses a unique launching mechanism for all containers so you can have a program written in c another program written in python another one in node different operating systems different database systems it doesn't matter everything is isolated in these containers and shipped on one layer or on one ship and delivering these containers or these packed and isolated software to all the users to conclude docker is a mean of packaging delivery and launching software the last thing that i would like to mention about docker image is that all the images that are created are saved on docker registry on your computer on your local machine but there are ready-made images for you to pull and to use they actually live on a place called docker hub so other users and developers can use commands like docker push and docker pull of the required images it's more like github for repos now in my opinion the best approach to learn docker is to use it and to use it you will need to install it and i highly recommend to install the docker desktop application it installs everything that you need for the command line and also gives you the gui where you can inspect your containers once installed you should have access to docker from command line so let's go ahead and install docker alright so let's go ahead and open docker.com and you will have to um to sign in to register to have an account on docker exactly the same like you would do with github so let's sign in so here my docker id is back brace password so you see here immediately that you have repositories like in github okay uh this is my account okay access the world's largest library of container images you get all the images that already created by other users and get pushed in docker hub repository right so hub.docker.com okay and to install the desktop or the docker desktop just get started with desktop you have to download it so you will download docker desktop for windows it's the same thing with with mac so if you're working on windows or mac i really recommend that you install that so you'll click next next next it's really easy and once it's installed you should have the system and it looks something like this okay so you will have the images and the containers all right and also you will have access to the command line interface so if you will open your your shell or your command prompt and let's type docker version to check out your docker version okay so you get client docker engine community version 20.10.2 go version because docker is written in go language okay server right also you can just type docker and this will give you the different commands that you can use so you get options okay d4 debug for example host list you have management commands and you have commands right attach build commit cp create and so on rm to remove stuff restart to restart the container push pull ps to list all the containers image and others okay all right and to check out your installation works correctly go ahead and run the following command you'll type docker run hello world so immediately he sees that unable to find image hello world latest locally so what it does it goes to docker hop and pulls it from docker hub from library hello world full complete okay and there you go hello from docker this message shows that your installation appears to be working correctly so it shows you now that docker takes different steps in order to generate this message the docker client contacted dr damon and the docker daemon pulled the hello world image from docker hub then dr damon created a new container from that image which runs the executable that produces what what we're seeing here and finally the docker daemon streamed that output to the docker client which sent it to your terminal so let's try other thing for example let's say docker run it which is the interactive mode so it can be run in in the background ubuntu bash so we will interact with ubuntu which is linux distro all right so now we are inside ubuntu okay so we'll say ls lsl okay you can run all of ubuntu's commands here so ubuntu is working virtually all right and let's exit okay we can also run docker info and we're going to work with terminal a lot you need to get used to this so the app's name is called docker app docker incorporated the kernel version the operating system docker desktop os type is working on linux the type of architecture okay all right so let me actually close the command prompt for now and let me create a folder and we'll call it main app for example and let me create a python file here so main.pi and let me just have a program which prints a statement let's say hello docker okay it's just a simple program that prints on the screen the message hello docker and let's go to the integrated terminal and it will work the same if you will open from bash or your command prompt doesn't matter it will do exactly the same thing so we want to check out the images remember that the image is just a snapshot from your source code which is immutable or unchangeable so if you'll type docker images these are the current docker images that i have locally on my docker registry and this was the last one that we did now hello world if you will type docker ps to display the list of open containers we have nothing because we don't have any running containers let's try to create a docker image from this hello docker program and there is a command called docker build so type docker followed by command build following that is a parameter in this case minus t or dash t which stands for a tag because we will need to give the image a tag name and let's call it low docker all right and then the path where exists your source code in this case just a dot which means our current directory all right and let's hit enter okay so it started well it started to build but then it failed why it failed because it failed to read docker file recall that in the slides we said that in order to have an image we need to have a docker file which we don't have in our current directory and docker tried to locate that file but it didn't find it actually it could not read it so let's go ahead and create our docker file to do that simply create the file called docker file like that all right enter and really it doesn't matter you can write it you can write it with a small letter docker file not necessary um with d capital it will work fine right okay so let's see what we can have in our docker file the first thing that i want i want to say from and the name of the language and as this file is written in python i will type python i will precise that this is python 3. notice here that it's underlined so if you will click on python you will click open you will have python image which you can easily pull that python image on your computer and work with it okay so this is the python image docker official images okay so this is the first thing next i want to create a directory so for that you will say run following that a sub command called make dir to create that directory and the directory that i want to create is going to be in user source and app and after we have created this directory we want to specify the working directory so it will be exactly the same workdir you see here the work there path which is exactly the same all right then we want to copy from the current directory which is a dot and to the same location okay user source app the last thing i want a command to execute that file normally when we execute a python file we say python main.pi or run the pi or whatever the name of the file dot by we will do the same thing and you see here that this is executable so python and main.pi so when docker will run that it will go through the the ninth line and it will execute python main.pi and hence run the file or run the program okay so let's repeat what we did so we'll say docker build tag flag and we'll give it the name of main and the current directory and we will hit enter and here it didn't have any problems because it found the docker file and notice that here a first step is from docker library python3 okay it executed this okay then we have created a directory user source app then the work directory will be the same then we have copied to the path so usually copy is followed by two arguments from two and that's it naming to docker io library main now if we will try to docker images we will see that in the repository we have added the main image okay 39 seconds ago and it has a size of 885 megabytes alright now let's go ahead and run the container be careful if you will say docker ps to check out the containers we have nothing and to run a docker container you will type docker run and the name of the application which is main and when you will hit enter you will get hello docker so this is displayed from the container let me show you very quickly our gui interface you see here image in use and this is the container you see here that it's displayed in the container hello docker all right the same result whether in the in the command prompt or in the container itself inside docker gui and remember when we said that an image can have different containers so for instance if we will delete that we can create another one we can create hundreds of containers it doesn't matter so let me show you if you want to delete that image that main image how we can do that in the shell well it's pretty obvious in the gui you can just click here and remove but even in that it won't work because it's in use so if you will make remove it won't work okay because the container actually is still running so in order to stop this you need to just delete it all right but we won't delete it from here i will show you how to do it from there okay so let's say again docker images all right we still have our main image and by the way if you make docker ps to see the running containers you won't find anything although we had a container but it worked and stopped it just displayed the message or printed the message hello docker and it stopped in order to display all the containers that even were working and stopped you say docker ps a it's a flag for all display all and you have here main we had hello world and ubuntu alright all these containers were working and stopped now to delete an image from our table you simply say docker image because we want to delete an image followed by sub command called rm for remove and here you can remove in a couple of ways either by using the image id or by the repository's name so if you will type main it's the same as if you will type the image id so we'll take that copy and paste here and hit enter and there it is untagged main latest deleted okay it's deleted now if you will see um docker images again you'll find that main has been removed from the table these are very important commands to know and memorize docker images ps how to remove an image how to remove a container how to run how to build also we'll see docker compose when we will work with our django project okay so without further ado let's create a django project all right compose and django let's go ahead and create a folder i'll call it django django docker project okay it's a long name but just to describe what we're doing and i'm going to open that with vs code all right so the first thing we need to do now is to create a docker file so let's create docker file then we will save from python like we did in the other example and here we will see together new things that we didn't see in other in the other example for instance we will have the environment we need to define the environment and here the environment for python is called python unbuffered and we will set it to one or to true the next thing that i want is the work directory which will be equal to just slash code creating a path or folder called code then we want to copy from requirements.txt which is essentially the dependencies okay that your django project will need and we want to copy from requirements.txt to the code folder and we will run this by pip install reading mode requirements.txt then at last we will copy everything from the current directory to the code folder all right and let's go ahead now and create requirements dot txt so i'm defining here the django version that i want to install as a dependency and less than 4.0 okay perfect so now we have saved requirements and closed it now let's go ahead and create another important file and we will call that file docker dash compose dot yaml this file describes the services that make your app okay so in this example we have two types of services web server and database and the compose file describes which docker images these services use and how they're linking together just for the sake of time i'll just copy that and i will paste it in docker compose so you can see here we have two types of services database and web database is um the image is from postgres so docker will go to docker hub and we'll pull the postgres image and install it environment postgres db equal to postgres postgres user equal to postgres and the password is postgres and if you have worked with django before you know that django works with sqlite database so when we will create our project we will need to change in the settings file we'll need to change from sqlite to postgres in order to be compatible then we have the web build from the current directory command python manage the pi run server 4 x 0 8 000 which is the local host ports this means that the local port should be set to the public one and this depends on db which is set here all right now let's go ahead and create a project usually in django you would do django admin start project followed by the name of the project here we will do it differently in docker so you will need to type docker dash compose run web then django admin followed by start project command and the name of the project so we will say simply django project followed by dot which means i want to compose this project in the current directory [Music] all right so it's building from python you see goes line by line in the docker file when we have set from python 3 we have set the environment 316 working directory code copying the requirements then installing the requirements installing django successfully install django okay then copy everything to code successfully built and this is the the image id all right image for service web was built because it did not already exist to rebuild this image you must use docker compose build or docker compose up dash dash build we will see that later all right okay perfect now if we will dur what's inside our django docker project we will find that we have successfully created the django proj project we still have the dockercompose.yaml the dockerfile requirements.txt and manage.py okay so we have our django project and let's go ahead and change in the settings we will need to change the database and later we'll need to add the name of the application in installed apps after we after we'll create it then we have the database this needs to be changed all right so also for the sake of time i will copy it and i will replace with on the postgres hostdb uh the port is 5432 the engine is backhands.postgresql okay and now we have a compatible system between the settings.pi and the dockercompose.yaml all right let's close that let's close that and let's get back to our terminal now let's go ahead and run our application from docker so we will run the docker compose up command and we will hit enter okay so we have got an error loading cycle pg2 module no module named psychopg2 so we will need to add in our requirements the cycle pg2 module so let's exit docker okay stopping django alright and let's go to requirements and add the psycho pg2 module so psycho pg2 dash binary version 2.8 plus okay let's save that and let's docker compose build in order to update what we have done okay every time you make some changes you need to update that in order to be reflected in the docker image all right so collecting and downloading psycho pg2 and once that done we will actually try to [Music] run again the server from docker so let's say docker compose up to run the project inside docker okay looks fine okay perfect so starting development server at um four zeros eight thousand which is the local host let's go ahead and click on that let's open and we have our server is working perfectly okay so it's listening on port 8000 and this is running from docker this is not running locally from a machine okay so let's close that and let's hit control c just to stop the server so the last thing that i want to do in this course is to create an app for the project and to do that is very similar to what we did to create the project so docker dash compose run web then we want from python manage.pi start app and the name of the app let's say i love docker and here you don't have to make dot just hit enter and it will be created in in the directory [Music] all right perfect so we have i love docker created so let's go ahead and make some modifications first of all i would like to create templates and in templates i will create index dot html okay and i will just paste that it's just simple boilerplate with two tags i love docker i'm also biscus all right um just nothing fancy really and i want to go to views and i want to create um index function and it takes a request all right and what it does it returns render and the request and the file is called index.html okay all right so this is views also in also here i would like to create urls.i and from django.urls i want to import the path and from current directory i want to import the views and the patterns url patterns okay views dot index okay so um the path is when it's empty path or just a slash i want to go to views and just trigger the index function all right now in the django project itself i would like to add that app in installed apps all right so come here we'll add i underscore love underscore docker and also in urls i want to add the path which is empty string it goes to i underscore love docker dot urls all right okay nice and i want also to import include okay now in order to update we will use the command docker compose build to update but as we want to run the application from docker after updating i can use a shortcut for that so i'll say docker compose up dash dash build so it finished building it checked for any changes successfully built and give us this image this image id all right okay perfect so looks okay let's go ahead now and open the browser to check out the local host for our program which is working from docker go ahead to localhost port 8000 and we have i love docker i am viscous so we have successfully created a django project and we have created also an app and this app is running from docker alright guys so this is the end of this crash course i hope it was useful to you i didn't cover everything in docker because docker is very big but i promise in the future projects i will include docker as part of the process so to give you more practical view on how to use docker in real life applications if you will okay so once again thank you very much guys for watching and if you have any questions please let me know in the comments and i will leave the code in the description section below and i will see you in the next crash courses
Info
Channel: Bek Brace
Views: 4,720
Rating: undefined out of 5
Keywords: Docker, Django, Python
Id: -eAkQsASIoc
Channel Id: undefined
Length: 36min 49sec (2209 seconds)
Published: Tue Jan 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.