Dockerfile for a Go project with go mod replace directive

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh in one of other videos uh we already discussed about go modern place directive uh in this one i want to combine uh go mode replace uh with docker file so basically my goal is to have a separate docker file for each go application and in case of govip frameworks what i have is in my root directory i have multiple go applications and each of them might have let's say for iris or for echo i i have docker file and my goal is to have them in isolation so they don't depend on each other and also they don't depend depend on folder structure so basically i don't have to keep them on my root directory or or in on any other separate directory because why i'm saying this because if we put this docker file on on root directory we can actually access both this one and this package but the thing is since we want to keep it inside in subdirectory we will be limited to the context of this this directory so let me show solution and then we can implement it for another like basically what i have now i have implementation for iris we will we will review it and then we will check how we can implement it for another go application so basically what i have uh in my go mode replace i have this replace directive which says that for this application when you say this import pad you need to go one step up and use a package folder package module in this case this folder contains all the shared code for each of these applications like models in mobile in memory storage and etc um and and that's it basically so when we run this application locally it will run successfully and for this import it will use our local directory that's it so now one step forward what we want to do we want to have docker file and build this application as a docker docker container docker application and let me show make file so my docker command is i build this image in this directory and then i run it and expose this 8080 port so but but since we have this replace directive if i don't do one trick uh this docker file let me show this docker file this docker file will not have access to our another folder which is this package so basically let me show what i mean so if i go to echo where i don't have this implementation um and and basically we can also leave this this step here so what i will do here i will for now remove this render we don't need it for now multitrack yes so let's go one step back uh use echo uh this is uh this is also co-application so this is request story go web frameworks and and this is a directory i will include all the links in description um so what i i do now i will say let's just copy past from make file so i will say build this application for me so while it's building let's just double check our docker file so now we are in echo in this uh in this application uh very i don't have yet implementation for this problem and problem is uh we have package dependency which is in another directory so we don't have access from docker file and our decker file looks like this we we do like standard steps like this golang we set some environment variables we do some updates then we set our work directory then we say copy everything here to the container and then do run basically this is i also can do without mod vendor result will be same and result is being that um at this moment it doesn't have um things that wonder directly around uncommon mod vendor yeah so what happens is um let's let's try to fix this as it uh suggests so we do go mod vendor and maybe i can skip no cache step yeah so this is just our issue so when um so basically what's happening our goal mode vendor command doesn't know where to find this package and the problem is being that uh this directory is not present in our in our um in our container so one way to solve this would be we say like copy [Music] package uh to our local and then like copy it in a way like uh like correct one would be go src and package for example and then like in terms of relative paths everything would be okay but only problem is we can't actually access this because our context as you can notice is limited to the current current directory which is echo folder um another way to solve this uh or try to solve this is would be like we locate our docker file one step above uh and then we have access to all these folders but but in this in the context of this project i want i don't want to do this way because i want to keep each of the stocker files isolated from each other so they they run in their own context they don't depend on each other and they also don't depend on external structure and and the way to solve it so now for just for a second let's switch to the iris folder in this folder i solved in this application i solved this problem and the solution is actually quite simple what i am doing i am before running any docker i say locally i want to clean up my package structure package dependencies and then vendor so basically i don't use normal go mode but i use a vendor mode and then in my docker file so let's check docker file for for this application everything i need to do is since i have everything present in this folder i can just say go install and this will this will know where to find this package and all this because it's in vendor mode so let's do this let's go back to iris we will run it and then we will solve our issue for another application so if i do make docker so while it's installing uh i know this topic is a bit complicated so if you have got any questions please let me know in discussion session um so if needed i can do another follow up for this i will link also another video where i in isolation explain go mode replace i use the same project for this we can also i can also show you one of the other projects like for for this call meme ui i also use uh go mode replace so uh you can also check this as an example um it's very it's a simple live uh example so let's go back it's still building yeah so now it's running so we can see yeah so we can see we have this application running on this port we can actually try to check it here i will do curl and i think it's entities if i remember correctly yeah so this is this is this is working version and let's now apply this working implementation to another application which is uh which is this director here and basically we we need to very very very simple steps first let's align our make files so now we know that for for docker we will need to have we will need to be in vendor mode so i will be copy pasting this part here actually i already did so we have already this implementation so we have this mod sync we have a docker part so we will build docker application with another name it's echo actually i can also remove no cache parts so we can speed up it a bit and now what i need to do i need to change this part a little and do do this go install so yeah that's it so and now let's try uh let's stop this one move to another implementation and do make docker so this will run the same steps for our echo application which will be first as you can see we already have this vendor file a vendor folder with all dependencies one of them being our own package this package implementation here then then we will have a docker step um here [Music] using docker file of course so and we will then we will do go copy it will always have this vendor file folder and during the installation we will have everything present in our container and as you can see here it's already running so we are building and then we are running this application so in terms of uh api-wise these applications are very same so i can just rerun the same curl uh now we are getting it from from another application echo application so this is what i wanted to show um by by combining go mode replace and go mod vendor we can actually run this kind of applications in containers uh without being like without needing to like restructure our docker file or or our like repository structure
Info
Channel: CoderVlogger
Views: 1,299
Rating: undefined out of 5
Keywords: codervlogger, tutorial, code, software engineering
Id: rihxzjUIJc0
Channel Id: undefined
Length: 12min 23sec (743 seconds)
Published: Thu Feb 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.