Let's learn to dockerize Laravel app from scratch (With NGINX, PHP8, PostgreSQL)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll be learning how to dockerize our laravel application so we'll be using nginx php8 postgresql and yarn for the local development environment so the aim of this video is to teach how can we help to improve the local development environment so that the multiple developers who are working on the same project all of them can have this same development environment even though they have different operating systems or the different configurations available so without any delay i want to get started so for this what do you need to have is you need to have docker installed on your machine so currently i am on a mac os and the configuration that we are trying to do it will be useful and any developers who are working with me even if they are on linux or on windows platform they can have the fully running application using this docker application so first for this we need to create the two files that is the first one is docker file and second one is docker compose.yaml so let's get into it let's quickly open the dockercompose.yaml file this is the ml file where we store the information about the different images that will be pulling and the different networks that we create almost it is the one of the most important file okay let's provide the version so for the yaml file the version will be three so the syntax the system will be understanding so let's first create a network so we need to have a network called web app and then the different services are the images that we will be pulling that will be mapped to this particular image particular network so there are the few services that we want so the first service that we want is nginx and then the second one that we want is php and then the third one that we want is postgres so uh now let's add few informations within this so nginx will be pulling the image and zinx and this will be stable and this will be coming from the alpine and the name of the container is app underscore name underscore and the next the reason for me to provide this uh particular name is uh when we are working with the micro services we have lots and lots of applications and let's say all of the applications all of the micro services require nginx in that case if we name everything to be engine x there may be some sort of ambiguity so we do not want to have ambiguity so we are naming this in this pattern so over here we want to have the ports for now let's say we'll be exposing this application to 8000 port on our local environment and the will be mapping the port 80 of the engine next to our external world and then this will link to the php that we'll be creating and then apart from that we will need to mount the volumes as well so over here this should have been something like this would be mapping dot slash from bar www.html and then dot slash docker slash n nginx and app dot conf let's name this site.com the configuration file for the nginx server host and this will be going to nginx d and default.conf and this will be a read write mode also we'd want to map the logs file as well so we will be mapping the logs file to this docker directory logs and the nginx and it will be coming from bar logs and genex and this also needs to be rw so this volume depends on php it will also depend on the postgres that we are creating and then we want to attach this service to this particular network called web app for now so now let's move on to the php side over here so we will be creating a we have already have created a docker file we'll be adding few of few things over there and we'll be building this so we'll be building this and the docker file that we want to use is the main docker file and then we want to provide the name of the container as well container name is app same as above app underscore name underscore php and then we also want to map the volumes uh this should be oh i think this should be going over here so everything will be mapped from bar www.html and then we'd want to expose the port over here and the port will be nine thousand one that will be nine thousand i think there is some mistake so let's just delete this thing and then we will also attach this php to the network called web app so final thing that we need is the postgres so we'll be pulling this and making changes over here as well so we'll be fetching uh post grace and to build.3 dash alpine so this is a older version of the postgres but i suggest you to use the latest version so for my example i'm using this version so the container name will follow the same pattern app underscore name underscore this will be postgres and this should restart only if you stopped and then would want to map the ports as well like this port is 5433 in our application that will be able to accept the connections from this container and this is our local uh this is uh the port from this container that is 5432 and this is 5433 it means in our local machine we can connect in this port so we can connect prospects in this particular port and also we want to map the volumes for this as well so over here so we can simply go over here and we'll be creating a folder called docker inside that will have postgres and then it will store the data from this container which is coming from bar leaf and post this sql and the data so we need to provide few environment variables as well uh postgres underscore user that will be same as db underscore username and the post grace underscore password will be same as db underscore password and post post grace underscore tv so what will this particular thing do is uh when the when we just build our container in that case it automatically creates the database for us so and then we need to add the final thing postgres underscore host underscore auth underscore method and this should be trust and finally we want to attach this to the same web app network so uh with this uh we are almost ready with all of our services so we wanted to have a few things like nginx and then we wanted to have few other things like php and then post case so now we need to make few changes on this docker file because we have not specified the particular version of the php in this so now let's go into that docker file and let's make the few changes so over here so this is our main container so over here uh from we need to get php 8.0 for now so from psp 8.0 dash fpm uh and this is from alpine so we'll be getting uh this php 8.0 fpm alpine image and then we are fetching this image and we want to run few of the commands like we want to install the uh few things like we want to install the node.js we want to install yarn we want to install the composer and everything so for this i'm simply copying the code from my existing code so what are we doing is we are basically installing the postgres sql we are installing the node.js and also and because we want to compile our assets using the container we do not want the local environment to do that and we do want to install the php extension for video and video for the pg sql it is for the first case and we want to install the composer this is how it is done we get it from uh the call using this particular command and the work directory is www.html so the next thing that we want is as you can see on this php image uh we need to you know on on this nginx what you can see is we need to have these server blocks for the application so for that purpose we need to create a directory over here so let's uh create particular directory called dock car and then inside the docker we need to add one particular directory called nginx and then inside nginx will be adding a site.conf that is the server block so site dot conf so over here we'll be pasting a basic server block that nginx provides out of the box so i will go to the top so we will be listening the application on the port 80 and then uh either we check for the index index.php or index.html and the server name is localhost and then error log is stored within this particular file access log is stored over here and the root directory is part w html public and then there are few other configurations so just make sure uh that you have the same line over here because uh this second parameter that we are reading is useful when working with laravel or any sort of php best frameworks and then there are few other informations uh related to this so now i think we are almost ready with everything so all of other things uh like other things that we added for the postgres that will be handled by the container itself so now we want to run the container so how can we do this is we can simply type docker compose up and we do want to build this and we want to do this in the demo mode that is it runs behind the scene in the background so let's press enter so it is okay there is some error on this particular [Music] yaml file i will fix this out so and the line number it is on the particular line number 31 okay line number 31 okay okay it should have been colon but it was something else so let's again build this okay there is problem on the another file as well that is on the line number 45 that should have been dollar and then i hope there are no other errors so it will basically create the containers for us so this is uh something that it takes time because in the initial phase it needs to pull the image on our local machine if we do not have the images for either for the php or nginx or anything and then it needs to follow all these things so as you can see docker file is the main entry point file so it is performing the particular action uh like it is installed running the particular command that we have added over here this is the first file so first of all it pulls the php fpm does alpine image for the php and then it runs this different set of commands for installing the postgres node.json and then it builds the other containers so this is something that will take some time so i will get back to you when everything is installed now that the our docker composer is up so everything is uh up and running so i can check what are the containers that are running at this point so as you can see i can run this command docker space ps so it shows that engine x is running on this particular port uh local was 8000 and it is uh coming from the particular image of from my containers port number 80 and the name of this particular container was laravel underscore engine x that i had given while creating the container so also i have for the post case the name is laravel underscore post case and it is mapped to 5433 on my on my local machine and it's coming from the 5432 of this container and then also for the nginx uh we do have we did have for php we had uh mapped put 9000 of the container to the 9001 so now everything is what is working so now we let's uh go to our dnv file and over there we should make few changes we need uh so this should have been done before running the command so make sure that you follow the same pattern so under this uh the tnb file for the divi connection uh it will be of the type of the pg sql for the push case but laravel provides mysql out of the box when installing the new application make sure that you make changes on this and then the db host needs to be pushed press because uh within uh this docker compose we are using this image and then uh we are using the port 5432 of that we defined inside this container we'll be using the same port and the database name is larabee 300 ts and username and password can be left blank so the one thing that we can verify is as you can see we are listening or we are mapping 5432 of for this container to do to our localhost 5433 that means i can have a connection to the postgres sql without having postgres installed on my local machine so that comes from the docker so for the let's try to add a new connection let's give this a name to be docker postgres and then the port is 5433 and the username is root and value is nothing and for the database uh it is lara view 3 underscore ts and let's test the connection okay it is working fine so now let's see so there is nothing available over here so now what do we want to do is uh we want to run the migrations we want to run the composer and all of the things inside the container so for this what can i do is i need to run this particular command so let's run this again docker ps to show what sort of containers and images that are available so docker exec it and then we need to run this particular command so we want to go inside this particular container and run few of the things larabee underscore php and we want to run this in bin slash assets mode so now we are inside this particular container this is the main container where we had installed php 8.0 where we had installed the composer and also node.js and the one and again as the dependency manager so we let's verify few things so let's see the version of the php so the version of the php is 8.0.9 and then we also had node installed and the node is version 14.7.4 and then uh let's see if we can see what sort of files are available so this is the main file where we had mapped and we had specified the work direction directory so now over here we do want to run the migration first uh okay instead of doing that let's install the composer first so composer is not found okay this is something to be taken this is something we had installed the composer but it's saying that composer is not found so this is some strange thing so we can have this looked afterwards but let's make sure that we can run the migrations first so php artisan migrate status so now migration table is not found we do want to run the migration php artisan migrate over here so now we can see that we can easily create uh this migration and we can run it so if we if i go into this uh database connection and if i reload this i should be able to see the tables over here so accordingly i can run all other commands like if i wanted to run the seeder so in that case i could have done php artisan tv sheet since uh composer ones not found in our container i just moved the script to install the concept to install the composer over here and then i also added npm to the list of the packages that i want to install because we want to run laravel mix and then laravel mix is dependent on the npx which can be installed through thicks uh this package npm so after doing this we need to again run this command docker compose of desktops build nd and the demand mode and i can again get inside the container on docker exact it and then laravel underscore php and bin slash assets so now i am inside it so you can see i do have composer installed so i can install the dependencies of the composer using the command composer install so since i already have installed all of the dependencies it is uh seeing that nothing is is there to install to update a remote so it will basically run the other composer commands so the next thing that we want is we want to have all of the dependencies installed so because we already have npm also installed over here we also already have node installed over here so now we want to run the command to we can run the command yarn to install all of the dependencies and and then we want to run the command to build or to compile the assets since i already have installed all the dependencies i will simply run the command yandev so that all of the dependencies are compiled so uh in the time when everything is being compiled as you can see on this docker compose file we are exposing our port 82 port 8000 which is basically running this nginx container so where we have added this particular configuration file which is reading our application and then it will be exposing to the external world so i will be opening a chrome over here and then in the port localhost 8000 we can have our application accessed so let's just open the google chrome so you can see this is the hello busy welcome to my app this is the old version of my previous video where i was teaching about the laravel mix along with the view 3 composition api in the type script so let's quickly make few changes over there so for now i want to run this application in yarn watch mode so that we can build we can see the changes are reflected on the real time so i can go to the app dot app.js file over here and then uh this is the compiled one instead i want to go simply to the uh resources js and app the ts file over here we do have the welcome component i can simply go to this welcome component so instead of this i will make few changes over over here let's say from docker and then let's simply uh save this and then this is now building my assets and now let's go to my url and let's try to reload it and it seems nothing is being reflected on the real time but we can try reloading the cache and all yeah from docker hello bj welcome to my app so as you can see we are able to have a fully dockerized laravel application where we are able to connect to the database we are able to run the migrations and we are able to have everything done from the container so within this i would like to run this seeder just to verify just to give a proof to you that everything is working fine so i will create a user factory over here and then inside this i will stop this command and i will run the command php artisan db seed which will in return seed 10 datas to my existing database so now i can go to my uh routes slash web.php over there where we are simply returning a welcome plate i will simply comment this out and then i will return all the users user slash all and then i will go into my browser and i will try to reload this again and as you can see we are able to connect to the database from the application from inside the container we are connecting uh nginx php and postgres so there are few things that we must be careful about so within this uh enb file the db host must be the name of the service that we are using and then the port is the same port that it is exposing over there and other things we can add as per our requirement so if we wanted to have radius we could have done that over the docker compose file we could have added another service as the radius and we could have used that as well so the purpose of the video has been fulfilled we are able to have a fully dockerized uh application for the laravel along with composer php nginx postgres and npm or jan as the dependency manager so thank you everyone for being here see you in the next video please don't forget to like share and subscribe
Info
Channel: BPKodes
Views: 1,388
Rating: undefined out of 5
Keywords: laravel, docker, dockerize laravel, laravel vue 3, composition api, tutorials on typescript laravel, laravel with docker
Id: bsABPuKeGxA
Channel Id: undefined
Length: 25min 58sec (1558 seconds)
Published: Sun Aug 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.