Laravel Docker Course | Complete Laravel Dockerization

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another aht cloud training series in this course you will learn everything you need to know about docker we will be building a new level docker template that you can use to run all your laravel projects our implementation will include six containers one for php one for websockets database redis queue and node if you're not using any of these services you can just comment that out and still use the same template to run your project i will also provide you with a sample project that we built in our last tutorial which is the chat application before we get started don't forget to subscribe to my channel to stay up to date with my latest training videos to get the most out of this course i expect that you have some basic knowledge in linux you must be comfortable using a terminal and you must have a basic understanding of what's involved in hosting and deploying level projects if you don't know how to build laravel projects i highly recommend you watch my previous video deploy any level application on aws docker is a platform for developing shipping and running your applications it lets you build and run your projects in containers typically when you host a laravel project at the bare minimum you would need three components a server a database and you need to have php installed with docker you can build a container for each component so you would have one for php one for your database and one for your server once you create your docker build for your project you can use it to rebuild your projects on any other computer without spending time configuring your server manually to install docker we're going to go to docker.com on the home page go to developers then click on docs from here you want to click on download and install now you want to select the installation for your operating system if you're using mac then you can select between the intel ship and the mac m1 ship here if you're running windows then you'll need to download the docker desktop as well and also you need to make sure you install wsl and you can find the link for that here you should be able to see all the instructions once you have docker installed it should look something like this this is the user interface that helps you manage your containers we're not going to be using it we will be using the terminal you want to make sure that you have docker up and running so you want to write docker dash v and here if it's working you should see the docker version on the docker interface you want to click on the top here settings you want to make sure that you have used docker compose v2 turned on and if you're running this on windows you want to go to resources wsl integration and you want to make sure that you have this enabled now you'll want to create a new directory for your project i called mine laravel dash docker you can create a new laravel project here or i recommend you use the chat application that we built in our last tutorial for that you'll want to go to my github account that's on github.com slash emad emad dash zamut z-a-a-m-o-u-t the link is in the description then you'll want to go to repositories and you'll want to select the level websocket chat application example from here you can click on code and you can download it as a zip or you can just clone it i'm gonna go ahead and copy this url here i'm gonna go back to our directory and from here we're going to open our terminal and i'm going to run get pull and i'll paste my url here oh yeah before we can run get pull we'll have to run get init all right now we should be able to see we have all our files in here the first docker command i want to teach you is docker ps this will tell you the status of all your docker containers on your computer you can see here for now we don't have anything another command that i want to show you which is the same command is docker compose ps and you can see here it says no configuration file found now docker command works globally it will work when in whatever directory you are it will show you the same thing docker compose is dependent on the directory you're in if it has the docker compose file or not docker compose is used to manage multiple containers for your project now we're going to create our starter docker file so you want to open your project in your code editor and you want to create a new file it's called docker file and you want to also create a new file and it's docker compose so docker dash compose.yaml yml and you also want to create a new directory in here let's call it docker now you'll want to create another file inside your docker folder and call it entry point dot sh if you don't know what all these files are for don't worry we're gonna cover all this once we start building our laravel project now before we go to our next step let's go back to our terminal and let's check what happens when you run docker compose ps and you should see this error top level object must be a mapping and this is a good error it indicates that our docker compose file is created now we're going to build our database container to run mysql server for us in your code editor you'll want to open your docker compose yaml file and the first thing you want to do is you we want to specify the docker compose version as of today the latest version is 3.8 now we will need to create a new volume so volumes is going to be used to store our data every time you start and shut down your docker containers you will lose all your data to make it resistant you will need to create a volume which will store it on your local drive so let's go ahead and write volumes and for the value for the key we gonna write db data this can be any name you like and then we're going to add tilde now let's go ahead and create our database server so we're going to add a new service so we write services and let's go ahead and add some spaces in here and i'm going to add a comment and i'm going to say database server and in here let's call it database this can be any name you like for the image we're going to be using mysql and you want to specify your version now we're going to expose our port so we can connect to our database container so here we write ports we want to expose port 3306 locally and we want to make it 3306 externally so we can connect to it from our computer using the port 3306 now for the username and password and the database we're going to grab this information from our emv file so in here you want to make sure you go to your emv example file and let's open this up on another screen so we have our database connection if you want to connect to your instance for the host you're going to have to use the name that you write here we called it database for the port it's 3306 this is the same port that we set in here you can obviously change this to any number you like but you want to stay consistent with the convention 3306 for mysql now we want docker to create our new database using these variables so we want to set our database name our username and our password as we specify it in here to do that we're going to enter environments and in here we're going to specify a name and a value for the name the first thing we're going to do is set our database so we write mysql underscore database equal and we want to access the value in here so we do that by writing the dollar sign and curly brackets and in here we want to specify the name that we have so let's copy it here i'm gonna copy paste this four times let's go ahead and set our username and that's going to be mysql user for the password same thing mysql password let's go ahead and copy our password from here and for the last one let's say mysql root password and let's set it to the same value as the password we have in here now our last step is we want to specify where we want to store our data and that's going to be inside our volume in here so we do that by writing volume and the first value is going to be d db data which is this name right here and we want to specify the location internally on our server and that's going to be inside var lib as in library and mysql that's the default location now let's go ahead and build this container so we're gonna open our terminal and let's run docker compose up two dashes build now once that's done let's go ahead and scroll all the way to the top and you should see that we have some errors in here and it's saying that the database variables that we have set are not set that's because we created those variables inside our emv example file we will want to have it also inside our emv file that's where it actually reads it off so let's go ahead and copy our example file and create our emv file now let's go back to our terminal and let's do docker compose ps and you can see here our docker container has a status of exit 1 which means it doesn't work so let's go ahead and write docker compose down in case if you have yours running and you'll want to go ahead and write docker compose up build again once that's done running you can't really use this terminal anymore you'll have to keep it open on the left here you will see all the messages relating to your docker container level docker database one let's go ahead and try to connect to this docker container so i have my database client open in here i'm gonna try to connect so the ip address is 127.0.0.1 that's our localhost the user was homestead the password is secret and we set those inside our environment variables we exposed port 3306 let's try to connect and you can see here all the messages relating to our connections and you can see i connected to my database now if you don't want to keep this terminal open all the time what you can do is press ctrl c to exit then write docker compose up dash d so the d mode is basically detached so it will run in the background without you having to keep the terminal open let's go ahead and write docker compose ps to see the status of our containers and you can see here our container named laravel docker database 1 has a status of running in here you will see our ports mapping now the ip address here that you see 0.0.0.0 is also your localhost however this is used by your docker containers so your docker containers communicate through this port but when you try to communicate with it from your computer you will use 127.0.0.1 here on the right you should see the ports mapping so we're mapping port 3306 to port 3306 now anytime you want to stop running this docker container you're right docker compose down once that's done go ahead and write docker compose ps and you should see that there are no running containers now we're going to build our redis container so we're gonna create a new service let's go ahead and add a comment and say redis server and let's go ahead and call it redis for the image we're gonna use the redis albine and we want to run a command which is going to be redis server append only and we're going to require a password so this command will run every time we start our rediscontainer so this is going to start our redis server so in our emv file we have a redis password field and it's set to null you can add whatever password you would like to use null is fine you need to make sure you have it for your emv and emv dot example file now to get that variable we're going to add quotations and a dollar sign and curly brackets and we're going to reference the redis password from our environment variable now our last step is we will need to create our ports so we're going to map ports and for redis the default ports are six three seven nine and we're gonna map that to six three seven nine let's go ahead and run this go back to our terminal and we're going to run docker compose up dash d to run it in detached mode once that's done it should look something like this now you'll want to make sure that your container is actually running so we're going to write docker compose ps and we can see here that we have our database running and we have our redis running and we map port 6379 to 6379. now we're going to build our php service this one is going to be a little bit different than the database and the redis server let's go ahead and create our new service the first thing we want to do is add a comment and let's say php service let's call it php for our php service we're going to build it inside our docker file so we could run custom commands inside your docker file the first thing you want to do is specify your image for php so we're going to write from php and your php version in my case 8.1 as php so the as part is just giving it a name so we could use it as a variable i'm calling it php this could be anything else you like now let's go back to our docker compose file and we want to run whatever is inside this section under the from php 8.1 so inside our docker compose file we can reference this file by using the build command now for the build command we're going to add three variables context and we're going to reference our current directory target is going to be php so the target is basically what you have set in here as php or whatever variable that's the target we're gonna also provide some arguments so we're gonna add args to give some variables and we're gonna specify that the app emv is equal to what we have set inside our environment variable so we're going to use a dollar sign in curly brackets to reference the appian v and we're going to also add an environment variable for this so we're going to write environment for the name we're going to add also the app.emv i'm just going to copy this line and we're going to add another one let's copy this and let's say it's called container roll you don't have to know what this does yet we will get there later on so let the container roll be app we're gonna also have to specify the working directory inside our php server so in here we're gonna write working the ir for directory and it's going to be var www it can be any directory you like but for www is the standard when you host your project on your server for the volume so we could grab our data we're just gonna use the current directory and we're gonna save that to var www now we're going to add our ports and we're going to map port 8000 to 8000 again now this could be any number you like you could map 8000 to 880 but this is pretty standard now our last step for the docker compose file here is we want this server to be built after the database and the redis server are built so to do that we're going to say that this service depends on and we're going to specify the database and we're gonna also specify the redis this way we can have our database and redis created first then this will be created after otherwise you will have a race condition when you try to create all of them at once and you try to run our docker script where we're gonna be building our server my running migrations it's not gonna work now let's go back to our docker file here once we build our server we wanna run some commands so we wanna basically install extensions and things like that so the first thing we want to do is run apt-get update so we could update our server then we will want to install some extensions so i'm going to run so the run command lets you run the commands inside your server once it gets installed so we're gonna install unzip uh we're gonna also install pq dev and we're gonna install also the curl dev command and we're gonna also run and install some docker php extensions we're gonna install bdo and pdo for mysql we're gonna install bc math and basically you wanna add all your extensions that you wanna install here for php remember guys this file this part gets run once our container for php is up and running so we wanna also install redis so we could use it in our program so we're gonna run run and we're gonna use pecl to install radius and the command goes like this install dash o dash if redis and then if you wanna write on the next line you just have to add a slash or backslash and we're gonna run another command and it's going to be we wanna remove all the files that once we install redis gets installed by default for us and that's going to be inside our temp pair folder and we're going to also install the docker extension for redis so we do that by writing docker php extension and we're going to enable redis now after all this is done we're going to copy our project files to our server so first we want to specify our working directory and that's going to be inside our var www folder you could use any folder you like but this is standard once we're inside the folder we're going to copy our code files from here and we're going to place it inside our work directory which is davar.w now our last step for installing we're going to also install composer so to do that i'm going to copy the image from and we're gonna write composer and specify the composer version i'm using 2.3.5 and i'm gonna copy it from this folder and that's pretty default and i want to add this to the same location on our server now once we have all this installed i wanna do things like run our migrations install our composer dependencies so i'm gonna have to specify here an entry point file so we could run our bash script to do that we write entry point and if you guys remember we created this docker folder in the entry point dot sh file we're gonna reference it here so inside here we're gonna write docker slash entry point dot sh and let's go ahead and go to that file so here you could write any command that you want to run that's not relating to installations so it's a bin bash file let's not forget to add our first line here the very first thing i want to do is i want to install our composer dependency i'm going to add two flags the no progress and i'm gonna add the no interaction the reason why i had no interaction is i don't want any input once you install composer sometimes it asks you for yes or no input we don't want that to prevent our script from running now i want to save time as well i don't want this to keep running every single time if composer independencies are already installed so i'm going to wrap this in an if statement and i'm going to say if the file vendor autoload if this does not exist then we want to run our composer install otherwise it's already installed there's no reason to run it two times and let's add our then here let's indent this line and let's close our if statement if someone forgot to create the emv file i wanna just copy the emv.example file to do that i'm gonna write another if statement and i'm gonna say if the file dot emv does not exist then we will want to copy our emv example file so i'm going to do an echo to display a message and i'm going to say creating emv file for emv and let's specify our app emv now the app emv value is actually coming from your docker compose we have it set here if you remember and we have set also a container value so we can access these variables like this here all right once we display that we want to copy our file to do that we're just gonna use the cp command so we're going to copy the emv.example file to emv and we're going to close the if statement actually let's add an else case so if environment variable already exists let's display a message and saying emv file exists let's go ahead and close our if statement now our next step is we want to run php artisan migrate to run our migration files we want to generate our app key and you don't need this but just to show you you can clear your cache you can clear your config cache as well and you can run row clear you can run as many commands as you like here all the commands that you need to setup and after all that i'm going to run php artisan serve on the port and for the port we have to specify this we have to declare this variable so we're going to go back to our docker file and in here we're going to create a new variable on top of the entry point let's say emv we're going to create a new variable and let's call it port and we're going to give this a value of eight thousand let's go back to our entry point file and we're gonna reference it here so we're gonna run our laravel application on port 8000 and we're going to specify the host to be 0.0.0.0 and we will specify our emv file now once that's done let's also execute the default docker php entry point let's go ahead and try running our new container so we're going to go back to the terminal and we're gonna run docker compose down then we're gonna run docker compose up and here looks like we have an error inside our docker compose file it's saying that the volume must be a list so we probably uh have some typo in there so let's go back to our docker compose file and let's see what happens so volumes we missed the space here so spaces and indentation is very important for your docker files let's go ahead and try to run that again docker compose down and docker compose up and i'm gonna also run it in detached mode now our next step is we wanna run php artisan migrate to run our migration files we want to generate our app key and you don't need this but just to show you you can clear your cache you can clear your config cache as well and you can run uh row clear you can run as many commands as you like here all the commands that you need to set up and after all that i'm going to run php artisan server on the port and for the port we have to specify this we have to declare this variable so we're going to go back to our docker file and in here we're going to create a new variable on top of the entry point let's say emv we're going to create a new variable and let's call it port and we're going to give this a value of 8 000. let's go back to our entry point file and we're going to reference it here so we're going to run our laravel application on port 8000 and we're going to specify the host to be 0.0.0.0 and we will specify our emv file now once that's done let's also execute the default docker php entry point let's go ahead and try running our new container so we're gonna go back to the terminal and we're gonna run docker compose down then we're gonna run docker compose up and here looks like we have an error inside our docker compose file it's saying that the volume must be a list so we probably have some typo in there so let's go back to our docker compose file and let's see what happens so volumes we missed the space here spaces and indentation is very important for your docker files let's go ahead and try to run that again docker compose down and docker compose up and i'm gonna also run it in detached mode looks like we got an error and it's saying that it failed to install the extensions here bc match this is a mistake it's supposed to be bc math not match let's go back and fix that so we're gonna go to our docker file and here we're gonna just remove that c so pc math and let's go ahead and run this again and we got another error and here this time it's saying that p r e c l not found this is supposed to be p e c l there's no r that's another typo so let's go ahead and fix that so back to our docker file and here we're going to remove that r let's go ahead and run this again once that's done it should look something like this let's go ahead right now and see what's happening with our docker container so i'm going to write docker compose ps and here you can see our php is running and it's on port 8000 so let's go ahead and give this a try on the browser i'm going to go to 127.0.0.1 on port 8000 and here we have an error it's saying that the app key is required but it was empty you should not be seeing this error but if you do what you want to do is you want to go to your php code again and you want to go to your emv file scroll all the way down and here where you find the pusher variables you want to add the keyword staging for all of these values you shouldn't be seeing this because i have updated my github repository to include these once you make any changes related to websockets you will need to restart your servers so i'm going to run docker compose down and docker can pose up again once that's done let's go ahead and try again we're going to go to our web page and refresh and you should be able to see now the home page it shouldn't be working so if you press connect it's not going to work that's because we don't have our queue and we don't have our websocket server setup before we create our websocket and our queue servers let's go back to our terminal i want to show you some good useful commands so i'm going to clear everything and let's write docker compose ps let's say right now you want to create a new migration file so you wanna execute your php artisan command inside your laravel dock or php container to do that you write docker execute and then you write the container name and for now let's just try php artisan now you run this command inside your php container this is just an example a syntax is like this docker execute the container name and the command that you want to run inside the container now sometimes you want to also connect and ssh into your container you can easily do that so let's write docker compose ps and let's uh ssh or go inside our php container so we're gonna write docker execute if you're on windows you need the minus it and you're gonna write your docker container name and that was php dash one and you wanna write slash bin slash sh if you're on windows you will get this error and you will need to add this win pt first if you're on mac you don't need to do this so let's go ahead and add it when pty if you get this error this only happens if you're on windows so you will need to change this you'll just need to add two backslashes for ben and sh now we're inside our container you can see we have our files in here now we're going to be building our node server node we're going to be using it to run mpm commands and run npm install in order for us to build the node we're gonna do the same thing we did for php so we're gonna go to the docker file and let's go ahead in here and add a new comment and i'm just gonna add equal signs and i'm gonna write a comment here and i'm gonna say node so for node we're gonna do the same thing that we did for php we're gonna say from and we're gonna specify the image and we're gonna be using node 14 alpine you can specify your version and we did here as php so we're gonna do here as node and we wanna do the same thing we did here as well we wanna copy the work directory when we first create our image we want to copy our files then we will want to run mpm install global and cross emv this is required for my project but it might not be required for yours we're gonna also run npm install and now i'm just going to specify our volume which is going to be inside var triple w node modules let's go back to our docker compose file and let's scroll all the way down so we're going to create a new service for node for the name let's call it node and we're gonna do the same thing as we did here for php i'm just gonna copy this line the build let's add it in here for the target we're gonna be using node don't really need arguments for node so we're going to remove this but i am going to be adding our volume so we can access our data and for the volume we're going to specify our current directory and we're gonna map that to user source and we're gonna do the same thing but we're gonna also attach our node modules folder to our user source node modules and our last step is we're going to turn tty to true now let's go back to our terminal let's go ahead and run docker compose down and docker compose up and detach mode let's double check that it's actually working so we're gonna run docker compose ps and you should be able to see here we have our node container it's up and running to create our queue we're gonna create a new service let's scroll up and let's minimize all what we have so far all right i'm going to create this on top of the node server let's add a new comment and let's just say this is for our q server let's call it a queue and for the build we're going to do the same thing that we did for php let's copy this actually let's copy even the environment and let's add it inside our queue for the container role you want to change this from app to queue all right now we're gonna also copy the working directory and we will also copy our volume that's pretty much it for our cue for in docker compose now notice here we're using the same target as php so we're gonna be running the same docker part for php and we have the same entry point so let's go back to our entry point for our entry point we want to install composer if it does not exist we also want to create our emv file if it does not exist but for the queue we don't want to run this this is only for the php application so what we're going to do is we're going to wrap this in an if statement so the first thing i'm going to do is create a new variable i'm going to call it roll enroll is going to be coming from our docker compose file remember how we set this container role inside environment for the queue it has the value of q for the php it has the value of app and let's add that here all right now we're going to make an if statement and say if the role is equal to app then we want to basically run all this and let's go ahead and invent this all right now else if the roll is equal to q then we're gonna run basically our q command here we're gonna write echo and i'm gonna say running the q and let's go ahead and run our queue command so we're gonna write php we're gonna have to point to our php directory so we can run uh artisan commands and we're gonna say qwork i'm gonna set the verbos flag on and i'm gonna say tries equal three to try three times time now i'm gonna set it to 180. so i'm gonna run docker compose down and docker compose up once that runs you wanna run docker compose ps and in my case looks like i got an error code here but i wanted to use this opportunity to show you guys how to look up the logs so right now both of these exited so they failed i'm gonna write docker logs and i'm gonna start off with the php let me copy this and i'm to paste it here and here the log is telling me inside the entry point file line 16 i am missing this symbol so let's go ahead and see what's happening there and if you take a look here i have this symbol and this should not be there so we're gonna have to remove it from here and we're gonna also have to remove this symbol from here i added it by mistake and actually while we're at it let's take this one and let's put it inside our app because i don't want this to run for our queue and another thing i see here it's a mistake we need to have spaces between our if statements now let's go ahead and try running this again once that's done let's go ahead and run docker compose ps and you can see they're all up and running to enable web sockets it's really easy we're going to create a new service let's just literally copy all the php service and let's rename php here to websocket and we want to keep the target the same we want to change the environment container role from here to uh websocket and we want to change so that depends at on database and redis that's correct we want to leave it for the port we're gonna expose port 6001 we're gonna all map that to 6001. now we're going to go to our entry point and let's go all the way to our if statement here so what we want to do is we want to write a new else case let's copy this and else if the role is websocket and let's go ahead and copy the echo here as well and say running the websocket server and to run blur valve websockets it's really easy we're gonna write php artisan websockets serve and that should be pretty much it let's uh try running this so we're gonna run docker compose down and docker compose up let's double check that it's working so we're gonna write docker compose bs and looks like they're all running let's check our website so i'm gonna refresh my page here and i'm gonna connect my user and let's open another browser page and let's connect another user and let's open a last one and let's just connect without a user so for the first one i'm gonna write a message i'm going to say hi and you should be able to see the message come here if the websockets are connected and looks like it's working let's check on the other browser screen you can see the same message and this is a real-time chat application thank you for watching if you guys found this tutorial useful i would greatly appreciate it if you hit that like button if you're not subscribed make sure to subscribe to my channel to stay up to date with my latest training videos if you have any questions don't forget to leave it on the comment down below and we'll see you on the next video
Info
Channel: Emad Zaamout
Views: 53,589
Rating: undefined out of 5
Keywords:
Id: WahJ91Nrgn0
Channel Id: undefined
Length: 43min 30sec (2610 seconds)
Published: Mon May 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.