Getting Started with Docker Compose for Golang & MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there solomon here with i code stuff bringing you back another video um i know i've been uploaded a video for the past nine months i believe uh i've just been busy with uh my current full-time job notary uh then i also have been working on this side project called hoop spots uh so with those two things i've just really haven't had time to upload videos um but in the past nine months i've been working with a new tech stack uh with uh my side project with hoop spots uh we've been building a mobile app uh it's a react native mobile app and then we have a golang and graphql back end and so in these past nine months i've learned a lot about golang and graphql as well as docker and there's been a lot of growing pains a lot of issues i've run into um so i felt like that was an opportunity to create a video um specifically today i just want to show you how to get started with docker compose for golang and mysql um it's a it's going to be very very simple api that we set up that calls on a mysql database and then it's going to be powered by docker so the actual api is going to be powered by docker and the database the mysql database will be hosted by a docker container as well so let's hop into the code and let's get started so being a golang application the first thing we're going to do is run go mod init and we'll just call this golang docker mysql tutorial oh we're gonna have to export our go mod here all right rerun that cool so we've created a go mod that just allows us to uh have a directory or go directory to work with to actually run the go project um and the next thing we're going to want to do is create a main.go file here we are um and we'll just call this main and then we're gonna need our main function of course oops funk main if i can type all right as i mentioned we're just going to be setting up a simple api um and golang has a lot of internal packages that allows us to set up our server so what we're going to do is we're going to create a function called uh homepage and this will be the function uh that handles the uh when we hit the home page url so that's going to take in a response writer and it's also going to take in a http request request all right um and then all we're going to do is we're just going to print to the screen that this is the home page let's do a printf uh welcome to the home page and this is not just print f f print f and then we're going to print another line and just saying hit the home page endpoint all right and then what we're going to do is we're going to uh handle that uh homepage method so we got to do http um handle function and we're going to pass in the root url and then we're going to say home page all right um and i believe if we actually run this we should be good to go go run me oh whoops we're gonna have to listen on the server so log.fatal this will just log.fatal will only run if the uh if it fails to run our server so we're just going to run it on port 8080 pass in nil rerun that and then if we pull up the browser localhost 8080. pull that over alright so here we are welcome to the home page so that uh basic uh url is set up um now we're gonna actually set up the um database schema uh we're just going to we're gonna want to pull data from a mysql database of course so we're going to eventually create a another method we'll probably just like have a database called users and we'll just create a user's page method or something like that and that will just essentially call the mysql database powered by docker and pull that data into our api um so we're going to do is we're just going to create a database directory and we're going to have a file and we're going to call it migration.sql and what we're going to do is we're just going to create a a single table we'll just call it users and we're going to have a couple columns uh we're gonna have an id and we're gonna have a name varchar 255 and it can't be null and then the primary key is going to be our id of course and then we're just gonna have some uh fake data in there so we're gonna insert some names we'll just say john doe um jane doe and any other fake name i can think of uh that should be good for now actually um cool so that's just uh for when we run the migration with docker um we're gonna pull that up in a second um so we have our database schema setup we have the uh boilerplate for the golang epi now we're gonna have to get into uh setting up the actual docker files so we're gonna have a couple docker files we're gonna have a docker file for the api we're gonna have a docker file for the database and then of course we're gonna have the docker compose.yaml file so what we can do first is we can create our docker compose file so we'll say docker compose.yaml um so we're going to use version 3.7 that's just the one that i i've been using lately and we're gonna have two services we're gonna have a database service and we're gonna call this uh tutorial db um platform or not platform oh wait i am on an m1 so i have to use platform linux x86 64. and this is only four if you have an m1 because i know there is uh some like incompatibility with docker and uh the new mac uh m1 chips and then we're going to have our build for context we're just going to use the current directory and then for our docker file we're going to call it database dot docker file as i mentioned we're gonna have uh the database docker file we're gonna have the api docker file um then for our networks we're just gonna use the default network uh default and then restart uh we'll say always and then our port so we're going to use a standard uh 3306 for our mysql ports you can change it to whatever you want um and then lastly we're going to need our environment variables so we're going to have the mysql random root password we're just going to give it a basic password of course you would never want to do this in production but we're just going to call it secret we're going to have the mysql database we'll call it test database uh mysql user we're going to call them test user uh for the mysql password for that user we're just going to use secret again um all right so i think we have our necessary variables set up uh now what we're going to want to do is we're going to want to set up our volumes and we're just going to bind our mysql data to the mysql library and then for our command that we want to run this is a common command that you'll see whenever you run mysql for the first time um this is just a script uh pretty quick quick and easy um all right so i think we have everything set up for our database of course we need to create our db.docker file to make sure that exists we're not going to add anything quite yet all right and then next up the next service we want is the api um and for our container name we're just going to call it uh tutorial api um for our build we're going to use the current directory again and then we're going to use a docker file called api.dockerfile and we'll create that really quickly api dot dockerfile and these are just our so if you guys are oops that's not right and if you guys aren't familiar with uh oh i kind of lost my train of thought i'll probably get it back later but we'll just create our api.docker file properly this time all right so next up uh we just want to set our ports again we'll just use port 8080 to be easy and then this container is going to depend on our database so the api is dependent on the database so we we're going to pass that in depends on we'll just say that and then for our volumes we're just going to bind our current directory to the app directory for docker all right then for our networks we're just going to use our default network again and then for our volumes we're going to pass in that mysql data so volumes mysql data set cool um so now we're just going to want to uh add to our database docker file um so we're going to use mysql8 mysql eight i think it's point zero point twenty three i believe and this is very very simple all we're gonna do here is we're gonna copy the uh database migration file so all migration files here so we'll say dot sql and then we're gonna export that to the docker entry point and that will initialize our database dot d all right um so yeah that should be very simple that's all we need for the database docker file um we're gonna have to create another file and we're just gonna it's gonna be a basic uh shell script so one thing i've noticed or learned along my way of working with docker and the database and the api um it's not enough to just say depends on it also has to wait for the database to start so we're going to rely on this third party package called wait for it and so we're going to do is create create our entry point dot shell file that's fine we can install that oh yeah so it's basically wait for our database port to be set up database host and database database port and this is just using the environmental variables from docker and the reason why like as i mentioned is um like if you don't have this file uh and you run the doc compose build and docker compose up uh your api will start after the database is uh started however it won't um because we have this command in our docker compose file we have to wait for this command to run so that's why we have that wait for it script here um and then we're also going to use a convenient um package called compile daemon and what this will allow us to do is it'll allow us to update our main.go file and it will automatically update uh every time we make a change to it so go is a compiled language so typically you have to restart uh like you have to like cancel the command then re-run go run um to like start up your command but with this package it automatically rebuilds for us so it's nice and convenient go and so it's just going to build the main file for us and it's just it acts like an npm watcher if you guys are familiar with that so just watch for dot go file changes we'll just say that um and we're gonna use this entry point dot shell script in our api.docker file the api docker file is a bit more complex um so we're gonna use an alpine golang alpine um image if i can spell alpine and our working directory is going to be the app directory so if we go back to our docker compose file we can see that's where our working directory is going to live so we can just say working dir and then slash app and then what we have to do is we're gonna have to run some apk updates and then apk um add a couple libraries so let's see apk add the gcc compiler and then apk add the make script all right and then what we're going to want to do is we're going to want to copy our go mod and when we so when we uh we're going to install a database package so it's going to create a go mod and then go.some within the gomod directory or file so what we're going to do is we're just going to copy those two files go.some into our working directory and what we want to do is run go mod download and go mod verify that those packages are correct next up we just want to install that compile daemon that allows us to watch our go files so we can say run go get github.com i believe his package is github nemo and it's just compiled daemon and as i mentioned earlier that's just for when we run this script here um next up we're going to want to copy that directory into their directory and then copy the entry point dot shell file into the directory and then lastly we're gonna run and run um we're gonna want to add that wait for it package here um so we're gonna copy and paste that script i believe it's on github uh that allows us to use the wait for it script um it doesn't ship with alpine by default so that's the reason why we have to use the wait for it but if we copy it from over here and we paste it we can just see that it's just calling that uh wait for it script it's adding it um and it's running some uh it's changing some directory permissions to allow us to run the wait for it directly from our shell file and then lastly we're going to need the entry point for the docker file and we're just going to use this entry point dot shell file so we can just say shell sh and this is what docker is going to run on its side and then entry point dot sh so this command will get ran uh every time our docker uh compose is run all right so it looks like we have all of our docker style or docker styles docker files set up um the next step what we're going to want to do is we're going to want to set up that mysql and go lane connection so as i mentioned earlier we don't want to just have the home page route we're going to want to have a users page so it's going to grab our users from our database when it's created but what we have to do first is we're going to have to get the sql driver for mysql so go sql driver mysql alright and so if we open this we can see the go.some file this is what the file i was talking about earlier within our api.docker file so that's where this lives right here but now we have our we have the actual driver for it so the next thing we're going to want to do is we're going to actually want to create our get user script so this is going to get users from the database so the first thing we want to do is create a get users method and before that we should probably create a struct called user and it's just going to mirror what we have in our migration file so the users table only has an id and a name so that's what our struct will reflect so we'll say type user struct we're gonna have an id and that's gonna be an int cool and then we're gonna have a name and it's that's gonna be a string and for json here we are um cool so the first thing now with the get users method um we're going to want to uh initialize our database connection so open up db connection and that's where that uh sql driver comes in handy so db error sql.open for our driver using mysql and then the data sources i believe is just going to be the the user calling the password at uh at the host of the docker image i believe so for our variables let's go back to our docker compose file so for our user we have test user so the test user for our password we use secret and we're going to use a tcp connection and this is where so our host is literally just going to be the docker compose so this is considered our host here so db so most off more often than not you'll use like localhost or 127.0.0.1 but with docker it's much simpler so we'll just call copy db and we'll say db port 3306 and we're just going to use the database name so if we go back to our doc compose file we'll just use that perfect um and then if there's an error we want to check if there's an error with our connection so check if air with db connection so what we're going to do is we're going to check if this is nil or no they say nil in go um and we're going to panic so just break here we'll just say error all right and then we're going to want to defer oop we're going to want to defer uh that database connection so we're going to say defer db.close so that won't do it right away cool so this is literally just our initializing uh database initializer um more often than not you would abstract it to a different method but because this is a very simple tutorial i'm just gonna have it all in one method um cool so next up uh we're going to want to just grab our results so this is going to execute a query so we're going to have results and a potential error and we're just going to say db.query and we're just going to select all from users cool and then same thing here if an error just panic otherwise we're going to loop through our results um and assign uh assign it to a user's variable so we're going to say var users and it's going to be a pointer for the user it's going to be a a pointer of a i don't know how how to say it so it's going to be an array of user pointers so we're going to loop through our results we'll say results.next we're going to create a temporary user variable um and then we're just gonna assign uh this user variable to the results id and name so what we're gonna do is for each result to scan scan each result so error is equal to results dot scan and we're just going to assign the user.id and the user.name all right and then if there's an error again we're just going to want to panic cool otherwise we're just going to append the users to our or we're going to append the user to our users variable up here say users and it's going to be a pointer perfect and then with the get users uh function we want to return a array of user pointers so what we're going to do is we're just going to say return users but we're going to get an error or some squiggly line saying hey add that as a return type so basically what we have to do is copy this right here and this will be our return type because go is a strict language um cool so that's our get users method and we're going to use that get users method within our users handler so next up we're going to create a function called users page func user page and very similar we're just going to copy this and kind of bunch them together so put that down here but yeah we're just going to grab our users well first off we want to make sure our parameters are correct so we're going to grab some users from that get users method and then we're going to say this is hit the um users page endpoint and then we're gonna encode we're gonna return some json as if this were an actual api um passing the writer and then we're going to encode our users perfect um cool so i haven't tested any of this code quite yet because we haven't run the docker compose build and up commands um so first thing we got to do now is run docker compose build all right so the first thing we're going to do is run docker build or let's make sure our docker dashboard looks correct um if you guys are following along this tutorial i expect you guys to have uh docker installed already um otherwise it's kind of counterproductive to watch this so it looks like i have a docker container running already i'm just going to remove these containers right now i don't need them and then any images we can delete these right now cool all right so what we're going to do is docker compose build and that might take a little bit so i can pause the video until that finishes so our docker build had run successfully so what we can do next is run docker compose up we might see some errors let's check it out crossing our fingers let's hope it works let's hope it works all right so what we're going to do is we're going to go to our browser so it looks like it works um oh we gotta add our users route and that will take in the users page and with that compile daemon that we added the compile daemon here this should actually upload or update live so if i click my terminal it re-ran the build command and updated our files so let's refresh that's there if we go to users all right so it looks like the user's path is not working let's check it out why so unknown driver mysql uh perhaps we forgot to import it yep we did forget to import it um so this is a sneaky one i i hated this problem we ran into this issue before my developers and i so we have to do is import the mysql driver directly so sql driver mysql uh sort imports and for my ides i'm using goland if you guys are familiar with that i know uh vs code's pretty popular for go as well um but i ended up just forking the cash for uh this jetbrains id it's just really really good um but yeah so let's go back so reran all right so if we go back and refresh perfect okay so looks like it passed it worked properly but i don't think our database was seated correctly so let's check out what's going on there so after doing some digging i believe i found our culprit of why our database wasn't properly populated and that was because in the script i didn't specify what column uh we are updating uh we were supposed to be updating the name column um but it was missing this right here so uh mysql had no idea what to do when it was adding this um so what i'm going to do is i'm going to start from scratch i'm going to actually remove our database containers so i'm going to stop our docker compose script i'm going to pull up our docker dashboard and what i'm going to do is you don't have to do this every time this is just for the tutorial uh i want it to run smoothly this time so i'm just going to delete our container and then for our images i'm just going to remove these as well perfect and then what i'm going to do is we're just going to rerun our docker compose build i'm going to have a uh github for this so you guys probably won't run into this issue uh perhaps you guys probably caught my error already with my sql script um if you did great job um but yeah so we're gonna rerun our docker compose build we'll pause there our docker compose ran correctly this time and we'll rerun docker compose up hopefully it actually populates our database correctly this time perfect huh that did not go through this time either so what ended up happening um i believe oh our volume we have to update our volumes so give me a moment to do that so as many of you may know um docker uses volumes to persist its data uh so in order for us to actually uh get our script to work properly we're gonna have to remove uh our docker volume so we'll remove docker volume um for this here and hopefully oh we're missing the remove perfect um and hopefully if we rerun our docker compose and docker build it'll properly run this time so we'll just say docker compose build that's done docker compose up and that might take a little bit um but yeah this was definitely not intended um had i not had i do properly put uh the name column here we probably wouldn't be running into these uh errors or issues right now um but it's also a good uh learning experience by just like watching me debug our issue here so hopefully it works this time it's still loading takes a while for the database volume to get set up um so yeah i'll just pause it until that is done so i believe our script had properly went through this time um so if we go to our [Music] endpoint so welcome to the homepage again and then go to the users endpoint we can see our data so john doe and jane doe and that's directly from our um cedar command right here so inserting uh into the users these two values um so that went through successfully um so yeah that's really i know uh this video went a bit longer than expected um but i feel this could probably save someone a lot of time compared to uh you know the time that my developers and i put into research how to set this up so it probably took us like two days to figure out how to get this to work if you are exploring this video or coming across it you can probably figure out how to set up a docker container for your go api uh mysql database within what 40 minutes now um so yeah this is this is really all we have here um so yeah if you guys made it to the end i appreciate it um i will probably be making more uh golang videos uh as i did learn a lot these past nine months so i really want to pay that knowledge forward just to help some people uh who are also going on their golang journey as well um so i appreciate you guys uh appreciate you guys watching till the end and uh i i'm out to the next one take care bye-bye
Info
Channel: Icodestuff
Views: 832
Rating: undefined out of 5
Keywords:
Id: ioa02xkqRII
Channel Id: undefined
Length: 38min 35sec (2315 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.