Docker Compose - Deploy Golang Application in Docker | Full Stack Golang Project | Grow Your Skill

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back I am Nish in this session we will deploy our CH application into doger container using doger compose we will have two Services running one for our child outter application our API and uh second service would be for our post database that would be running inside a separate container so in order to implement this two I refactor our code base a little so I'll show it here so first of all I moved all of our code base into a separate directory that is called app and uh we needed to implement two things first we need to use the uh environment files Cu uh remember like in our data in our database so for creating our DSN so initially we were using a hardcode values our database running inside our local machine so that is why we hardcoded it while we deploy this application in a container so we will need to export these values this cred database credentials uh in in the environment so that is why I created this uh EnV file and uh moved all those secrets in into this file and uh simply used uh like this go. package that is coming from Joo from this package and this package is simply just loading the environment file and likewise in the while opening database connection so I substituted the appropriate values accordingly so there is nothing complex and uh second thing that uh that would be needed is that whenever our application would be started in the container there there won't be any tables so we need to migrate our database tables into the database and for that uh I use this goang migrate package you can have a look at that and this is also very very simp simple so there is nothing complex so all I am doing is that whenever our main function gets booted started so first I am migrating the database so this function would be called and uh this will populate the this will create the tables appropriately and for that so I created this uh migration directory and put uh the created these two files that is for the up and that one for the down so inside the up files so we just simply need to uh write our SQL statements so this is very very simple so just simply we need to uh write our native SQL queries uh this is the code refactoring and uh right now so let us proceed for the writing of Docker compost and the appropriate Docker files okay so let's do that first let start from the app so we will have uh two files here so we will create a Docker file and uh also I will create a startup file so I'll create another file also so my idea here is that uh so there is a type of so it would be startup.sh so in this startup.sh files so we will write all the commands uh for the building uh building uh building our application and other stuff and also we will need a Docker compose that would be a EML file that it so all the files necessary files have been created and let us also close which are not needed and also one more file would be we would require so I'll simply I'll just duplicate it and I'll rename it to env. ro because in the Productions so we will uh need to change the host a little okay so first uh start working from the our Docker file so we will start from the base image and the base image would be goang and uh we will see set our working directory so that would be let's say app and uh next uh I will simply copy all the files from the source to the destination next uh let me explain here a little working directory with the working directory directory we are just setting a working directory and with this uh copy directory so we are simply copying all the files from our local uh Local Host machine to the destination in the working directory so this is what we are we are doing and next we will run a command so that our all the uh our application dependency would be uh installed go mod tid so that way so it will uh install all the necessary dependencies next we will have a entry point and here we will say bin sh cuz we need to execute the shallow script up Dosh so these are the things would be required in our Docker file now let's uh work on our startup.sh file so this would be a shell executable shell script so bin as such while when we copied our source file into the host into the Container so we will have these two files EnV and en.pr we are concerned about the env. prod so what I'm going to do is that first I will remove EnV and then I will MV mv. Tov so that way so we will uh just replace the EnV with the EnV dopr and next we will need to compile our application so we will say go build- o and our output file would be named as server and next we will make it exe executable plus X do server and next we will simply execute it so that way so when the we will uh do a Docker compose app so uh this this app would be started we will save it now it's time to work on the docker compose file so we will start with the version and uh version three that is the latest and uh we will start our services so services so we will have uh two Services running let's say chai app that would be one service and second service would be our DB and also we will need uh volumes as well few volumes so we will adjust accordingly so for the chai app so we will build from the context build from the because uh we are already having a Docker file so we will build our application uh from this Docker file so we will say build this is the app I mean here we are saying that build the build the image from from the docker file located at the app directory and we will name our image as uh let's say chai app and uh we will name our container okay so we want need uh to specify the command CU we are running our application from the start.sh file next we will need to expose the port so we will say ports and we will our application is running on Port 3000 so we will map accordingly the port running outside and the on the host machine and the port running inside the container next we will provide the the location of of the environment file so we will say do f app Dov and next we will also need to provide One Directory because we want to our this container to be up when and only when our database is up and running we will use depends on directory and we will very simply say DB and now it's time to work on the our DB service so we will say our image let's say post gra and uh our container name would be let's say post gra container okay so our container name and uh we will also say that we want that uh uh if there is any problem so it will be started automatically restart always and here we will set some environment variables we will provide the you post the database user password and the database name fine now we will set some volumes so that whenever our container gets restarted so our data should be preserved so we will say volumes post space data path is VAR lib poost G and data so we will map our on our host machine the volume would be post data labeled as post data and inside our container the the appropriate this directory would be mapped okay and uh next we will uh open the ports we will map the ports actually so 5 4 32 inside the host machine and 5432 host is by default lense on the port 5432 so that that is why so we I provided uh accordingly and next we will uh map the volumes so here where is simply we will say post gra data and now it's time to build this this uh our application and uh let me go one step further and uh also make some changes in the in the env. Brad file because instead of Deb host as Local Host CU we are our host would be our uh this service so we need to update the uh accordingly so instead of the DB host as Local Host so we will very simply say DB and uh so let's yeah so let's uh try to test it and uh in our Docker so Docker desktop so there is no images now let's try to build the image doer compose build so this command will build the image it may take some time so it will pull up all the uh files from from the docker Hub and uh looks like the image has been built so we can check in the docker desktop now let us try to run the container so we'll say Docker compose up hit enter so it will pull the images appr poess container we are creating directly inside the doer compose so it will pull up the post appropriate image it may take some time we are not using any separate docka file for creating our post container so that is why it is pulling the appropriate image from the docker Hub so looks like the posters container has been started looks like there is some bug so migration driver error okay we will fix it no no worries so looks like the this this is coming from the Local Host looks like our this env. prod file that is not uh uh taking effect so we will we will fix that and uh very shortly and we can check in the containers running so our posters container is running running on 5432 and uh we can check the appropriate error what error is so this is trying to connect on the Local Host let me also check the port uh let me try to stop the post database running on my local machine sud sudo system CTL I'll provide my sudo password I will cross check here with using the status yes it has been stoed and now let's try to rerun this container till it is trying to connect on the Local Host no worries we will fix it out why it is not uh taking effect let us check the doer files so our entry point is uh been as such let me try to write a log message here and let us try to build the image again calling all the dependencies and now again composer up so yes so we are getting this message our entry point is working fine so we are getting it triggered but again so this is uh trying to connect at the Local Host let me figure it out what went wrong looks like we are unable to rename our env. prod file inside the container so with this situation we can do a couple of things we can update our ENB file and put the database credentials of the container like uh we we will simply need to change the DB this will work or uh we can also do one more thing that uh we can uh export all the this information for the security reasons we can export all this this information inside the container environment itself so let us also see how we can do that so we will don't we won't uh use this en file I mean this values from the EnV file and rather we will simply put all this inside the DB uh inside our uh container environment that way so we will again uh try to build the image again compose build so ideally for the As far as the security is concern so this is the ideal ideal approach and let us try to Dr compose up okay this time so it has uh connected uh properly and now it's time to uh check the our uh application so let's see that okay our application is running so click on the article list I will create a new post first post put some description save post success successfully created and uh again click on the edit updated so post got updated successfully and we can also click on the delete so everything every crowd operation is working perfectly fine inside the container let's also try to create another post let's say second post description save so this the post also got created successfully and uh we can also check inside the container as well so let's say open in terminal post press psql to list out all the databases so we have our database we will change our database J HT MX demo and we will list out the tables whatever tables are there so we have the tables like post table is there so this way so it is uh working perfectly fine and we have successfully deployed our application right inside the uh container do container using doer compose with the two services and uh still my the post is not running on my machine so we can check the status here so it is off and uh this is literally working very very fine that's all for this session and uh we have also completed this uh ch router series using postr we completed our crowd operations we deployed our application uh in a doger container and uh if you have anything to say about this session this or this whole series so please do share your views and uh I'll see you next so till then take care and happy coding
Info
Channel: Grow Your Skill
Views: 291
Rating: undefined out of 5
Keywords: Grow Your Skill, Golang programming, beginner tutorial, Go project, web development, full stack project, full stack project with htmx and golang, golang htmx project, golang tutorial, golang, Chi Router golang project, go project, golang and htmx, full stack golang, golang Chi Router, golang for beginners, golang tutorial for beginners, Chi Router, go tutorial, htmx example, go htmx, htmx react, htmx and javascript, docker tutorial, docker compose, golang docker
Id: iZawhZ5Q13A
Channel Id: undefined
Length: 16min 57sec (1017 seconds)
Published: Wed Feb 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.