Dockerise a Go application | Multi-stage build | Optimise docker image 🐳

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to the channnel in this video we are going to containerize the go application so we will first create a simple server and then we will build the image later we will learn how to reduce the image size using multi-stage build this process is applicable for all type of application if you have the nodejs application python in all kind of application you can follow this by changing the base image and command you can build it so with that note let's build it to follow with the tutorial install the do to your system for that just go to the docker install uh install Docker engine and as per your OS like either it is Linux Mac OS or Windows just install it so you'll get all the instruction here like what are the system requirement and other things so we are inside the go container directory so let's just start with the initializing the go module to initialize it I'm just going to use go mod in it and go app simple okay and uh for a dependency we are going to use a go. EnV package to read the environment variable to install it use goget and the github.com /zo /o. EnV so the reason to have a dependency so that we can mimic a little bit of the real scenarios so once the dependency is added uh we can create the main.go and add our code we are going to create a very simple server which will have one route and which will return hello and whatever message we will pass to it using the environment variable package main Funk Main so we need the HTTP do handle Funk and our route's going to be slash and we are going to create a simple um function which will receive one response writer and the request using response writer we are going to send the response and request is like if we are sending any data to it using the post body or the query and all those things so in this case we don't need anything to return the response we are going to Simply write in the response writer first thing going to be the right header with the HTTP status code we are going to use the status okay and a message w. WR and message is going to be hello and whatever EnV we will pass it get En EnV os. get En EnV and we'll simply use MSG we can create a simple EnV file and which will have a message equals to suppose V okay uh let's keep it in small case only and then in the function we can simply use the init functions like before our application start it should be able to load the environment variable so to load the environment variable we can use go. EnV do load by default it read the EnV file so we don't have to give a file name and it will return an error so just read it if error is not equals to nil then we'll just simply Panic print a statement EnV load it simple and let's just start our server now so to start the server print like a simple message like server is starting at 3,000 okay and we'll listen to 3,000 using HTTP do listen and serve the port number is 3,000 and the we don't have any router so we'll just keep it as nil that's it so let's just test it go run main. go okay our EnV is loaded server is started so let's just test using curl command curl Local Host 3,000 okay so everything is working so now let's just dockerize it to dockerize it we have to create a Docker file first so that we can build this particular application and then using that Docker image we will run it inside a container so to build the image what we need is like we need a file called Docker file okay to build any application you need first a base image like on top of what you are building it as it is our go application so we are going to use a golang image so for that simply use from golang and Alpine uh Alpine is like a very light version of golang okay and then we can say like working directory so for working directory we are going to use like slash app it is like a default directory for the operations whatever come after it like if you're copying anything or the code which you're going to run it will run inside the SL app now let's just simply copy all the content like using copy Dot and Dot it will copy everything from here okay like this EnV go mod go Su mean. go and then it will paste it inside this/ app directory using run command we can install or download all the dependencies whichever is present inside the go mod so we are using this Zoho co. EnV so for that we just have to do run go mod download and after that uh we can build this application so to build it Go build and output going to be Go app so for output file name use daso and the file name so we are going to use GO app and where we want to save it we want to save it in the existing directory so use just dot after building this executable will be saved inside this/ app directory application is going to serve on 3,000 expose major use to document only like for which particular Port this application will run and at last we have to use the command like when this particular image will run so which default command it has to execute so we are going to use the go app SL app Go app our Docker file is completed to build this Docker file start the docker dtop first and then using Docker build command Docker build and then to tag this particular image like giving some name to this image what we can do we can use DT which will add a tag to it so give a tag like Go app version one then the simply dot so dot it will tell like in this particular directory it has to check for the docker file and if there is no Docker file name is mentioned then by default it takes Docker file so then just build it so now our Docker file is built use Docker images command to see the built image so we can see our built image is Go app and its tag is one and this is the image ID and if you can see its size is 292 MB so in later part of it we'll see how to optimize it and make it light using Docker run command we can run this particular image so Docker run then the port number we have to give as we have exposed it on 3,000 so we have to map this Docker containers port to the machines Port dasp 3000 colon 3000 the first first part signifies the machines port and the later part signifies the talker Port so if you want you can simply make it like uh instead of 3,000 you can make it like 44 okay then image name with its tag so we it is Go app one okay and Sur is listening at 3,000 but this particular 3,000 is of inside the docker container and we M this 3,000 to 44 so now to test it uh simply use curl command curl Local Host and44 so we can see like hello world it is working to stop the container use contrl C so right now it just stopped but it is still kind of occupying the memory in the Dockers to remove it completely you use Docker PS minus a okay it will tell all the stoed containers and then using this container ID use Docker RM and then the container so now this is finally removed completely let's now optimize this particular image so if you remember our current image size is like 292 MB so now let's see using the multibuild how we can reduce its size so in the multistage we are going to use this particular thing okay uh this complete build as a first stage and then whatever we have built it that particular build we will going to use into a second stage so it will be like whatever things it has downloaded and all those extra things it will not copy there it will only copy the built part whatever this go app which we are going to build so first we have to name this build so just use as as the um as the alas okay uh and then give the build name so let's see like suppose stage one okay and then come here we don't need the command now and uh expose also do we don't need now instead of using the goang Alpine image which comes with lot of things like the complete go compiler and all those things so we don't need it because our application is already in the binary format so now we just have to run it so to run it we can use Alpine image so use from Alpine and then the working directory going to be suppose slash the next step is copying cop in the binary so we have to tell first like from where we want to copy and using from flag we will tell okay stage one from stage one it has to copy and the source going to be slapp SL Go app okay and we want to keep it inside this/ goab only okay then simply for documentation use Expose and 3,000 as the port number and lastly command command is going to be SL Go app so this is going to be our multi-state Docker file now let's just build it again the same thing Docker build then the tag name so this time we can give it like Go app uh tag is two and then the current context so here I just want to add something like suppose if you have multiple Docker files like one is for this particular thing the simple stage and one is for multi-stage you can create another Docker file using same name Docker file dot suppose multi okay so this is going to be the another Docker file and then just copy paste the complete thing and now instead of using the default Docker file we can give the name also so for that use DF as a file and then we'll simply tell Docker file do multi and then use the dot to find uh to tell it like where to find this Docker file so we are in this same directory so you simply use Dot and then just build it image is built so let's just see it size now so use Docker images and now if you can see we have this go app tag two and its size now reduced to 14.5 MB earlier it was 292 MB so this is the advantage of using multistage and this is very handy when it comes to uh reducing the size of your containers or the image size in the production now if you'll simply run it so use Docker run before that just let's see if our yeah we have no stopped container Docker run- p44 colon 3000 and then we can simply give it as like Go app colon 2 no such fileo directory okay so we have to add one more thing uh we only copied the go app but not the EnV so what we can do is like uh can simply add one more copy and use do EnV in the tnv in this way okay let's build it again give the tag as three let's run it okay so now it is working so to test it simply call Local Host 44 yeah it is working I hope you like this lecture so before we wrap up just uh remove all the containers which is kind of running behind the scene or like which is in the stop stage so for that use Docker ps- a it will tell all the containers uh which are in this exited State and then you simply Docker RM and give the con ID and another one also yeah so all your dangling containers are now CED thank you for watching thank you for watching I'll see you in the next lecture
Info
Channel: practicego
Views: 927
Rating: undefined out of 5
Keywords: golang, golang tutorial, learn golang, golang for beginners, golang api, gorilla mux, mongodb, web development, backend development, rest api, api development, golang web app, golang crud operations, gorilla mux tutorial, deploy golang app, coding, programming, software development, tech, tutorial, beginner, fullstack, freecodecamp, goroutine, interview, interviewquestion, 2024, docker, containerise, optimise
Id: E29wKowfNyY
Channel Id: undefined
Length: 13min 12sec (792 seconds)
Published: Fri Feb 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.