Create Docker Container with Go App • #golang #docker #containers #api

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's learn how to containerize your go applications all right so before you can get started with this video make sure you have go installed and Docker desktop installed because you're going to need both of those those tool sets in order to pull this off all right so I have vs code uh up on screen just a blank project nothing going on here and we're going to create a very basic Hello World app so I'm going to open my terminal and then I'm going to type in go mod init containerized Go app and hit enter okay so we created a new a new module and then let's go ahead and touch main.go to create that file I'm going to open that file let's close our terminal so we don't need this quite yet and we'll set up calling it kind of the standard things that you normally would in a GoPro so package Main and then I'll do Funk Main and then we're just going to log println hello world like this all right so I'm going to say that I'm going to I save that I'm gonna open a terminal and run go run main.go which should just Echo out hello world which it does so we have our application kind of set up and ready to go now let's create a Docker file which is going to enable that functionality that lets us build the container image we want so I'm going to add a new file here we're going to name this Docker file all one word and then now typically in a Docker file you're going to have um really four main points you have from which tells you the base image you're going to be creating this off of you have the worker which is going to set the working directory for copying your files into you have copy commands and then you have your entry point which kind of tells uh the container image like when it starts when when the container is created based off of an image what command should it start in order to start the container so to speak we're also going to add in a run command which lets us pipe in simple it lets us pipe in a bash script more or less in order to do stuff and we're going to use that in order to build our go code inside the container so technically if this is being created inside of some kind of devops environment you're not even going to need go installed on the the runner the host machine that's going to do all the processing because we're going to do the processing within the base container image which has go installed in it if any of that was confusing I'm going to explain that again as I go along so the first line let's actually close our terminal on here the first line is going to be from and we're going to create a a container image based off of going 1.20 and this is when when I run this provided I don't have this this image pulled down on my computer it's going to reach out to Docker Hub pull down the golang container image and use that as the base image to start off by to build this container so we're going to set there we're going to set the worker to forward slash app and that means that any commands that occur after this specific line is going to be done within a directory inside of the container image called slash app so we're going to copy go.mod directly into that directory so just put a period there and then we're going to copy also main.go into that directory as well now we can use the Run command to build our container image so I'm going to say go build we're going to do Dash o because we want to create a file called bin which is going to contain which is going to be the executable binary that's going to run our code and then dot is basically telling me telling go to build the code that is in the current directory and again since we set the worker to forward slash app this is going to run inside of that app as well where these two files are going to exist and then last bit that we're going to do is entry point which is going to contain a again it's going to contain the command that the container image is going or the container is going to run once it starts up and that's going to be forward slash app forward slash bin since that is where this binary is going to live when line 8 runs and it builds the code into a file called bin since our worker is set to slash app the entry point is going to be slash app slash bin and this essentially is our Docker file that we can use to build the code inside of here so now if I open up my terminal again and we can we can run the necessary commands in order to build the cell say Docker build period and then I'm going to add a tag here of go containerized latest this last bit is optional but we're going to use this in a future video so I'm going to go ahead and take care of that now we'll hit enter also it makes it a little bit easier when we're running it so we can actually call the container image the the cons I guess the friendly container name which in this case is the go containerized latest as opposed to the hash that Docker will typically assign to an image when it creates it without being Tagged so we're going to wait a minute here and just let the process of building this image finish thank you all right so now that the image is finished finished building let's go ahead and list out the images I have on my computer so I can do this with Docker image LS and then let's grep our go containerized hit enter so we have and I'll expand this out just a little more space to work with we have a single image of go containerize the tag is latest here's our hash and it was built 39 seconds ago there's the size of the image itself okay so our image exists on our computer let's minimize this again and get back to our standard working space and let's go ahead and run the container and I can do this let's see I'm going to clear this out it will say go I'm sorry Docker run containerized go containerize latest and it just prints out hello world not a very impressive example but since I'm using Docker one instead of go run we're not using the local code that has been written inside of a vs code directory we're actually creating a container based off of that go containerized image and we're we're running that instead of the code directly okay so now that you understand kind of the basics on how to create an image let's get a little bit more of a real world example and let's build an API using the framework that I like building apis and go with which is Jin so I'm going to type in go get you github.com and let's go ahead and hit enter and this is going to install the Gin package locally inside of our project so this way we can actually build something off of it okay so now that this is running let's go ahead and close the terminal or now that this is installed let's go ahead and close terminal let's go back to main.go and let's get rid of this uh stuff here now we're going to import two packages the first one is going to be net forward slash HTP and the second one is going to be github.com forward slash genganic forward slash gin okay it's gonna have red squiggly because we haven't actually read any code yet don't save it otherwise go is going to remove those but let's create our router R is equal to gen.default and then let's go ahead and add a get um I guess a get Handler off of forward slash ping which is just going to respond with pong this is pretty standard this this comes directly from the source code I believe uh for um forging so if say r.get and then we'll say forward slash ping C gin dot context and this is going to say C dot Json status okay gen.h and then let's pass in message is going to be equal to pong so what we're going to what's going on here essentially is when we execute a get request against our our go API for to forward slash ping to that route it's just going to respond with a simple Json message of pong and then finally r dot run is going to run the uh run the API so I'm gonna go ahead and save this and let's test this out before we even get into the docker portion of it I'll say go run main.go foreign stuff from um from gin which is basically saying since the port environment environment variable Port is undefined it's running on port 8080 by default so let's go ahead and create a a test to to make sure that we're getting this back now I use the vs code Russ client plugin just because I like creating my requests uh directly and we don't want to do that I'd like creating my requests directly in text and storing them along with repository so I'm going to create a file named test.htp and then we'll say get HTTP code localhost Port is 8080 and forward slash ping is the name of that route I'm going to save this let's get a little more room here and then you can see with that plugin installed I get this link here that I can click on that lets me send the request and if I do that we get a Json message of pong so our API is running locally on our machine using go and working now let's see what we have to do in order to get this inside of Docker I'm going to cancel or I'm gonna kill the the API to stop it from wanting I'm gonna head back to our Docker file now most of this can actually stay the same the one difference is because we've added external dependencies uh into our go package we actually have to run a second command before we build it and that is go get and what go get is going to do is it's going to read the contents of go.mod and it's going to crawl the web wherever these modules exist or the packages exist and bring them down and install them now when we ran go get we ran it in the context of our local computer and not within the the process of building our container image so that's why we have to do this here um so yeah let's go ahead and do this one more time I'm going to actually just use the up command to get my go build uh container latest because we're not changing anything actually no we will change something we'll see forward slash API just to get a different container image uh so go containerized API and latest and I'm going to hit enter and this is going to go ahead and build the the container again I'm going to fast forward this because it's going to take a little bit longer but if you pay attention to the screen you'll you'll see that go get command get run and it's actually running right now so uh just pay attention here while I fast forward foreign is built we are going to run it again very similarly we did earlier but we're going to do some things a little bit different now in order to kind of differentiate um the fact that this code is running inside the container image as opposed to on my my development works works machine my development machine we're going to specify an environment variable of port and we're going to set that to 9000 and that's going to tell jyn to run the the API on Port 9000 instead of running on 8080 which is the default and then we also have to tell the tell Docker to pass Port 9000 on our host machine to 9000 on our container image so let's go ahead and do just that so Docker run we're gonna do hyphen e for the environment variable Port all caps is the environment verb we want and we're going to set that equal to 9000 like so and then we're also going to do hyphen P to specify the port that we want to pass through and that's going to be 9000 from the host to 9000 on the uh the container so finally we'll say go hyphen containerized API latest which is the name of the image with the tag and hit enter now you can see inside of the output it's not colorized that's because it's running within the context of that container and not in my zsh session um it detected that poor 9000 was set on the environment variables and it's running on Port 9000. so if I go back to my test.hcp and I click Send request again this should fail because it is not running on port 8080 but if I change this to 9000 like that and run it we actually get that same response back um because the container image handled the request instead of the the local go code which is not running hopefully after watching this video you have a better understanding on how you can put your go applications inside of a container and easily distribute them to any system that supports containers do me a favor if you've ever built containers whether with go or any language leave a comment below and let me know what you thought of that process if you enjoyed this kind of content you also might enjoy my series on how to build a Discord bot written with go and with that I'll see in the next one bye-bye
Info
Channel: Brian Morrison
Views: 20,598
Rating: undefined out of 5
Keywords: developer, golang, golang tutorial, learn golang, golang for beginners, golang programming, docker tutorial, docker, api, dockerfile, golang tutorial for beginners, docker container, learn docker, golang docker, go, docker images, golang tutorials
Id: C5y-14YFs_8
Channel Id: undefined
Length: 11min 55sec (715 seconds)
Published: Tue Apr 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.