Laravel with Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] when i installed laravel 8 for the first time there was one file that instantly caught my attention any guesses let's go ahead and create a new project i'm just opening up my terminal here we'll do composer create dash project and then laravel slash laravel and we'll just call this project blog then let's just cd into our blog directory and business as usual we'll just do php artisan serve and now we have this local web server at 127.0.0.1 or import 8000. so i'm going to open up chrome and i'm just going to paste that in there all right and here we go we have a brand new laravel project that we can start working on so let's open up vs code i'm going to open up my project all right everything looks pretty normal here we have our app folder bootstrap folder config database public resources routes we see our env file we come down we see our git ignore our composer.json file and then we see this new docker-compose.yaml file that was never there before let's click on that all right so raise your hand if you've ever used docker before if your hand is up in the air right now you're probably feeling the same excitement i felt the first time i created a new laravel 8 project if your hand is down let me explain why you should be excited right now so previous versions of laravel required us the developer to create a local environment with all the bells and whistles to get our project up and going that means you probably needed to install mysql maybe redis possibly apache and some specific version of php and whatever other dependencies your project required you're probably working on a team or with some friends so everyone most likely has different versions of these programs installed or maybe you're working on multiple projects that require different versions of these programs you don't want to upgrade or else you're going to break some older projects that require the older versions you're kind of stuck wouldn't it be great if you could specify some system requirements for each individual project and store those requirements right in the project folder then everyone collaborating on your project would be working in an identical environment no more well it worked on my machine meet docker and that's what you see right here this docker compose file docker is a program that allows us to create an environment for our application to run inside of they call these environments containers think of a container as a super ultra lightweight virtual computer you can have one container or hundreds and thousands of containers and each container can have its own programs installed so we could theoretically have one container running php version 7.3 with one application and then an entirely different container running php 8.0 running entirely different application all on the same computer all running at the same time sounds pretty awesome right it's like the laravel creators are trying to give us a little hint with this docker compose file docker isn't unique to laravel anyone can use docker so we could choose to ignore it and go about developing our application the traditional way or we could see what docker's all about so i say let's give this a whirl first things first let's jump back over to google chrome and i'm going to go up here and let's go to docker.com so chances are if this is your first go around with docker you don't have it installed on your system yet so click on get started and then right here under the docker desktop you want to download for your system environment for me i'm using mac if you're using windows go ahead and download for windows so go ahead and pause this video install docker and come back once docker is ready to roll so this is the docker logo it looks like a little whale container ship with containers on top of it you should get the little docker icon up here in the top right hand corner of your screen go ahead and click on that and we're going to open up the dashboard so at this point in time it says no containers are running if we click on images we don't have any images that's because this is a fresh installation of docker and we haven't created any containers and we haven't downloaded any images fortunately for us laravel made it pretty simple to get up and going so you really don't need a whole lot of docker knowledge or background information to get started using it if you'd like me to do a deeper dive video on docker and how it works leave a comment down below and if i get enough feedback on it maybe i'll go ahead and do a specific docker video but for right now this is really all you need to do to get up and going with laravel and docker so i'm going to click this little x right here and it'll stay running right here so it's still running in the background now that docker is installed i can close out the docker.com tab we're back at our laravel application and i'm just going to go back over to terminal so now that we have docker installed we don't really need to use the php artist and serve command on our local machine so i'm going to press ctrl c to stop this local web server so one of the other packages that laravel 8 installed when we ran our create laravel project command is a package called laravel sale now what laravel sale does is allows us to interact with our docker environment without really having to tinker too much with docker itself it gives us a few commands that we can run on our local machine that execute inside of this special docker container environment so the first command we're going to use is a sale command now like i said sale is a package that was installed automatically when we ran our composer create project for laravel eight so we should be able to run dot slash vendor slash bin slash sale and then a space and then just the word up now what that's going to do is if we jump back over to our vs code and we look at this docker compose file so when we run the sail up command it's going to look at this docker compose.yaml file and these are kind of like some build instructions and this is going to tell docker what to build and by default it's going to create four new docker containers for us so we have our laravel test which is going to house the environment for all the source code of our application if we scroll down right now selenium is commented out this is something that you would use for testing and then if you come down the next one is mysql so it's going to create another container with mysql running so for our ports it's going to forward our local port 3306 to the port on this container of 3306. it's going to bind the two ports together so when we run commands on our local host environment in our terminal it's going to execute those commands on our container running mysql so 3306 on our local machine is pointing at port 3306 on our mysql container the next one is redis and this is going to bind the port 6379 on our local machine to 6379 on our container again this is just allowing us to run commands on our local machine and have it point at ports inside of our docker containers we have a memcache container that's commented out by default and lastly we have a mail hog docker container then down here we're defining some networks we're calling this network sale we're using the bridge driver now i'm not doing a deep dive on docker itself i just want you guys to see that this is here for us to use inside or alongside of our laravel projects if you want to see me do a deeper dive comment down below and let me know you want to hear more about docker and we'll do a much more thorough video on what docker is how it works what is docker compose and what are docker files so let's jump back over to our terminal so now that we understand when we run this sail up command it's going to look at that docker compose file and create some containers for us it's also going to download the images for those containers so the first time you run this command it's going to download each image for each one of those containers and then it's going to spin up each individual container using that image so if it takes a little long the first time around don't worry the subsequent times that you run this sale up command it's going to go much quicker so let's go ahead and run this sale up all right at this point in time i think that took about three maybe four minutes to get to this point on my local machine and we should have docker containers up and running so i'm going to click right here on my docker icon and go to dashboard again now the first thing we see is this container apps i called my app blog so now i see this new blog item and it says it's running we didn't have that before and if i click this i get a drop down list of the containers that are running within this blog environment so the first one i see is blog underscore laravel dot test underscore one then i see blog underscore mysql underscore one blog underscore redis underscore one and blog underscore mail hog underscore one so these are separate containers all running to create my laravel environment now let's take a look under images and you'll see we have some new images right now we have sale dash 8.0 mysql redis ubuntu and then mail hog so these weren't here before when we ran that command sale up it went and downloaded all these images and then it used these images to create these containers right here that our blog application is using so i'm going to close out of this and let's jump back over into chrome now since we stopped our php artist and serve command if i refresh this there's nothing here because we're not using php artisan serve anymore we're using docker so i should be able to just type local host and here we go i'm currently viewing my project that's running inside of that docker environment let's jump back over to our terminal we'll do terminal and i'm going to use command t to open up a new terminal window so now that we have larabel installed and it's running in docker let's run some php artisan commands and make sure everything's working so let's just do php artisan migrate just to run the default migrations for the laravel 8 application and there we go we were able to migrate everything so this is actually hitting our mysql container running inside of docker now if we take a look at our vs code and we come over here to our environment file by default it's saying to use the db host of localhost or 127.0.0.1 and since we bound this 3306 port to our docker container running mysql's 3306 port this is what allows us to run commands on our local machine and have it affect the database running inside of a docker container so let's just say for whatever reason we didn't want to run these commands on our local computer we wanted to use laravel sale and run these commands right inside of our container environment we can do that if we come up here and click on the little docker icon and we go to dashboard i'm going to expand this blog and i'm looking for this right here so mysql8 is running in a container called blog underscore mysql underscore one that's the name of the container so i could come back here and change this to say blog underscore mysql underscore 1. i'm going to leave it as port 3306 because i want to hit port 3306 within this container i'm going to save this and let's open up our terminal again so this time if i run php artisan migrate i just change the db hostname so if i run this again it's not going to work i'm going to get an error right now it's trying to hit that mysql port and it's going to time out so we should get an error in a second i'm going to fast forward this all right so we can see operation time down and that's because since we changed the db host in our environment file we're no longer hitting localhost we're hitting this weird name of blog underscore mysql underscore one so let me go ahead and clear this out now let's try and run that php artisan command inside of our container environment to do that we can use that dot slash vendor slash bin slash sale so we're saying use the sale package and i want you to run this command inside of my docker environment so the command is exactly the same except we can drop the php in the beginning so we should be able to say sale artisan migrate and then press enter this time we got nothing to migrate because we already ran our migrations if i press the up arrow and just say migrate fresh and now we were able to run those migrations again so using vendor slash bin slash sale is look kind of like saying run this command inside of my docker environment if you don't use sale you're running the command on your local machine so both of these will work and you can do this both ways but if you're going to do this inside of your docker environment you want to use sale and you need to update your env file to let sale know where is mysql where is that database at in my instance i was able to look and it was blog underscore mysql underscore one if you called your project something different chances are the container for your mysql is going to be named something different so if you want to use sale make sure you update your db host if you're going to use your local environment to run those commands you can use regular old phpr to send migrate and other php artisan commands as well you could even say sale and then npm install so that just went ahead and ran our npm install but it did it inside of our docker environment it didn't do it on our local machine it did it inside of that docker container we could still run it here and set npm install but we're not going to as you get going with laravel 8 and you start leveraging docker containers sometimes having the ability to run these commands inside of the container is a lot nicer than being able to run it in your local environment you might have two or three laravel projects up at once and you want to go into that specific project folder run a specific command that only affects that docker environment and then in that instance you would use this sale package so i want to go ahead and clear this out and let me jump back over into chrome so at this point in time you should be able to get a laravel project up and going using docker and docker containers depending on what you're working on you can use local commands in your local terminal or you can use the laravel cell package to run those commands inside of the docker container chances are you'll be able to get away with running those commands locally but i just wanted to give you a little preview of laravel sale and a little bit about what it can do to help you work on your projects if you're curious about more options with laravel sale let's go ahead and jump over to the laravel documentation if you click on documentation and you come down here to packages and then you click on sale right here on the laravel.com website they actually have a lot of information that you can read to learn more about sale sometimes typing period slash vendor slash bin slash sale can be a little cumbersome so they actually show you right here you can create this alias that will allow you to just type the word sale and then up inside of your terminal so depending on how much you like sale or how often you plan on using this something like this sale up would be a lot easier than typing dot slash vendor slash bin slash sale one last thing i want to show you i'm going to close this out let's jump back over into our terminal and i'm going to come back over to our sale up command we ran earlier now if we want to stop this process from running we can just hit control c and that will stop all of our docker containers so we can see there were four containers done done done and done if we come back up here to a docker and we click on dashboard we can see that block has exited so it's no longer running we don't see the little running word under here anymore i'm going to close this out and if we jump back over into our chrome browser and we refresh our application it's no longer available to us because we stop those containers from running let's jump back over to our terminal and i just want to show you how quickly this can get up and going the next time around now that we've already downloaded those images i'm just going to run that same command we ran earlier with period slash vendor slash bin slash sale up so that's it our container environments up and running if i jump back over into chrome and i refresh my environment's already up and going so like i said the first time you run that it's going to take a little bit longer but now that it has the images we can get up and going very quickly with our laravel project inside of docker so i hope you learned something in this video i'm probably going to be using laravel with docker in future tutorials so i wanted to take a minute and kind of go over what docker is and how laravel is used inside of docker so that you aren't confused down the road if you see me running this sail up command or using docker in future tutorials hopefully you learned something in this video i'm going to be posting more videos about laravel probably docker vue.js i do some stuff on golang as well so go ahead like subscribe hit the little bell icon so you get notifications and i'll see you in the next video
Info
Channel: Scrypster
Views: 33,450
Rating: 4.9464722 out of 5
Keywords: Laravel, Docker, Laravel Sail, Laravel with Docker, Laravel Container, Containerization, Docker Containers, MySQL, MySQL Docker, MySQL Container
Id: PDaGJ397Ing
Channel Id: undefined
Length: 18min 27sec (1107 seconds)
Published: Tue Dec 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.