How to Run/Deploy Spring Boot & MySQL in Docker? | Tech Primers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever run a my sequel server as a whole on your own let's see how we can run my sequel in a container inside docker and separately a spring good application and how we can access them from one arm let's get started [Music] press the bell icon on the YouTube app and never miss any update from tech primers I have a spring boat application which I had created some time back which is using some basic database connections and it is going to connect to my local my sequel database but I'm going to use the same project to deploy that into docker and we are going to connect to a different doc can't inner which is going to have the my sequel database in it so before getting started with this particular project what I'm going to do is I'm going to start the docker my sequel contain because I wanted to run the my sequel as a separate container because that is how people do it so the general norm is that you create a separate container for the my sequin or any external entity and then you have a separate container for every micro servers don't merge everything into a single micro source or a single container basically so what I'm going to do is I'm going to create a separate container and I'm going to start the my sequel image so if you go to the docker hub docker hub has a official my sequel image available so I'm going to use that particular my sequel image so if you go to this particular you are half.com slash my sequence so you can see this is the official repository of the docker engine so you have a my sequel image as well there so you have different versions of the my sequel here so I'm going to use the Phi dot 6 version of my sequel right and there are different commands so you can have a look at the commands which you would be running right so I'm going to use I have already gone through this particular website and then I have tried it as well so I'm going to use the similar approach here so what I'm going to do is I'm going to start our my sequel server and so I'm going to run something similar right so I'm gonna run a my sequel instance so I'm going to show you how we can do it also I have a docker engine installed in my laptop so you can see that there is a docker engine instance which is running here right so what I am going to do is I'm going to open my command prompt and here what I'm going to do is I'm going to show you what are the different docker images which are there currently in my system right so I have these my number of talker images so if you notice here I already have a docker image for my sequel there is a there's one for a springboard which I had K some time back there is one for open JDK has been these to write my sequel and the open JDK these are official images which I downloaded from the docker hub same way this docker has been so dr. in itself is an image so here now what we are going to do is we are going to create a docker instance using the my sequel image right which we have which I have already downloaded so if you don't have an image already don't worry you can do this you can do docker pool and my sequence so you can see that will be website here so you can use this to download the image okay so I had done that already so I'm gonna not go to do that I'm going to run the docker image basically I already have the image now here which is the my sequel server and this is nothing but the whole my sequel server which is compatible funny talker engine right so I'm going to run this so I'm going to provide a name to this particular server which I am going to run I'm going to say my sequel standalone ok so just for our reference so we know that ok it's a standalone my sequel instance which we have ok so I'm gonna say what is my my sequel root password so I need to pass all these arguments so these arguments are available here to the website same thing right you can see what you need to pass right so you can notice here right don't say here you need to pass some stuff so let me go I mean by let me go and create that for you guys to provide a password so I'm going to create a password as passwords right I'm not going to give it any complex password so the next one will be my sequel database so the database which I'm going to use is called test because I already have that test database in my local and I I want to create give the same name so because in the code I have already added the test as the properties right the next one would be my sequel user name right so the my sequel user would be SA and the my sequel password so the my sequel password this mine secure password is different from the root password right so I'm going to use the same password because I want a root permission so I'm just going to say my sequel on let's go password equal to password right and what is the image I'm going to use right so I'm going to use the image basically what is the docker image which I have so I'm going to use the my sequel docker image and the version of that particular image basically these are called tags inside docker images so I'm going to use the tag qualify torchic so if you notice here in the website there are tags here so these are different tags right I'm gonna use 5.6 which is going to be 5.6 of 38 so that is the version of my sequel which it is going to use right so I'm going to use I'm gonna run this command so what this is going to do is this is going to create a docker container with my sequence terminal so what I'm gonna do is i'm going to say docker container unless so this is going to list the container which is currently running so if you notice here there is only one container which is running and i have just created that container which is called my is equal - stand-alone and you can notice here it was created 15 seconds ago okay we don't have any other container and inside docker images there is nothing for us so so you notice that there is nothing here so now let's go to the project so we need to create a spring boot image now so we have now used the my sequel image and we have created a my sequel container now we need to create a springboard container right so what I'm gonna do is I'm gonna go to the applications or properties I have basically springport the sorry the server door quote because by default information 8080 is used by Jenkins which is already running so I don't want to touch that so I'm going to use the port called 8086 okay so now what I need to do is I need to provide the username and password for their data source right so what is the username I killed I gave the username us say the password is password that's what I gave right so that's what I'm going to use here as well and what is the data base which I need to connect right this is nothing but the name of the container which I give so the container name which I gave is my sequin - stand alone so I'm going to use this particular container right so what else do I need so I need to give this project a name right right now I think it might be it is not build so I need to give this project a name so what I'm going to do is I'm going to use the configuration and then I'm going to give some name for this particular spring boot obligations so I'm going to give some name what let's let's give it as a spring okay so I'm going to give this name spring boot my sickle or maybe find anything that should be looking right so what does this do you those okay let's name it a different key right users item my sequin okay so I'm going to name this particular project of user second my my sequin and I'm gonna build it right so let me build it so this is going to create a final jar which is going to have this users - my sequin okay so let's see that here once this gets built I'll just fall forward if it is taking a while so the build is successful now what I'm going to do is I'm going to see whether the jar got created so if you notice here we have got the job created right so we have two jars basically both are the same just but since we wanted a different name I just created it as a part of the build itself right so I it's going to create a user's - my sequel to jar so now our jar is ready now we need to package this as a image so image is what gets deployed you know docker docker engine or a docker container alright so what I'm going to do is I'm going to create a talk of file basically so I'm going to create a docker file so inside the docker file I need to mention what is the image I need to package along with this particular docker image right so I need to package the JDK right so because during the runtime week might require the JDK right so my Springwood application requires a JDK to run so what I'm gonna do is I'm gonna use the open JDK so the version number which I'm gonna use for open JDK is 8 so that is nothing but the time so I need to mention the image : the tag so open JDK is nothing but the image repository which is already there inside the docker hub so that is why I know it is open JDK and it is officially available for us to use so I am I'm just using the same so the next one would be adding this particular jar right so there is the jar currently so the jar is present under target and it's called user my sequel tour competition I am going to use the same name right so I'm gonna give them same name so I'm just saying that okay use the jar from the target folder and then rename it as whatever so I'm going to use the same name the next one would be exposing the port number so I have modified the port number to be eight zero eight six so I'm going to expose that the last one is the entry point so entry point is where your start command or the main command main main function inside so the entry point for this particular application is the Java - jar comma right so I'm going to is Java if you need any space then you have to break this particular come on right so you have to do like this so I'm gonna do so I'm going to say java jar user my sequel touch on that this basically runs as Java space hyphen jar then space user - my sequel dot Josh right so this is how docker will be running and it will be using the Java from the open JDK so open JDK is another version of Java I will make a separate video on what is open JDK and how is the normal JDK different from the open JDK right so I'm going to use open JDK for our purpose here so that's it so now what we need to do is we need to create a image right so we have the docker file ready we have built the package now we need to build it build this particular package to create a docker image so for that we need to use the command docker build right and before that we need to go to that particular directory right so let's do the docker build from here itself so let me go to the terminal I'm going to use the dock builder in the current directory because the docker file is in the current directory so let me do some see Binaca file in there yeah I see baraka fine here so what I'm going to do is I'm going to say doc build current directory and I need to tag this particular image as something right so I'm going to say user - mine's the same name which I have given for the jar actually the jar name is users right just now I noticed it is user so I am going to change it whatever users in my silcoon to the consistent yeah so what this is going to do is this is going to create a docker image so previously we will be created when you saw the images are not the series of the different images the image did not talk or did not have any image con users of my sequel so now it is going to create an my sequel image so if you notice here there are four different steps using the from it downloads the image if it is not there I already have the open JDK 8 image so that is why I did not download then it then it proceeds to the next step it copies the jar and then it finally creates a image if you notice you finally built an image and it just renamed to some name right so if you go to if I just say docker images yeah I can see that image here so if you see here I gave the image tag as users - in my sequence I did not give an attack so it just says latest but if you want to give any time you can just say : and then give some version of money it will use that particular version it will just tag that particular image with that version because that could be a case where you wanted to have multiple images but with different version number so you can do that right so a remainder set now what we need to do like how we learn the my sequel image as a docker container we have to run the spring boot obligation which is nothing but the user's my cycle as a docker container right so let's run it for that let me clear this particular it's clear somebody will stop her um so taco run is the command to run the container so if you want to run any container I'm going to expose the port number eight zero eight six because that would be for some bug which you are going to be using and I'm going to going to tag that lily 8 0 8 6 port number of the system right so it's 0 8 6 coordinates 0 6 and I'm going to provide a name for this particular container I'm going to call this as users my sequel right then I need to link this particular application with my my sequel container correct so I need to link this particular application with a container which I already created which is nothing but my sequel happens down the road right and it's nothing but um I see it has a my sequel image so I'm going to use the multiple emails and leave my school standalone right and finally what is the image which we have right which is nothing but users before my sequel so that is our image so it is going to run the users - my sequel by exposing these two port numbers and I'm providing a name for it which is exactly the same name and I'm going to link thee my sequel standalone docker container which I already had okay that's all right so this create a docker container and then it is going to use it so I'm gonna see the container whether it has got created yep it has got if you notice you the entry point is Java a job that job right and now how do we check the logs right so how do we check it so I'm going to say talker box and you can provide the standard so if I do this this is going to give me the logs of P my sequel database so if you notice here these are my sequel database box so my sequel database has come up with the port three zero three threes in the six right so in my case I want to see the talker logs for my spring boat application which is nothing but users and for my sequel so I just entered it yup and see the port is also up seals right so this is the logs and this is the current gloss okay that's being good application is up and then these are the just the logs okay so now what I'm going to do is I'm going to hit the rest endpoint which I already have so let me check what is the rest endpoint which I have right so the rest endpoint which I have is on okay under that I don't have anything okay it is just going to the repository and then fetching in some data ignore this it is just doing nothing so it's not any help so it is just going to the user repository and then fetching the data and just displaying for us and this user repository has a table called users right yep the table is called users and it has these fields our user our unusual example right so let me hit that particular rest endpoint so see whether it is working or not right so what was that / all right so this should return okay what is the just saying that unexpected you are no message available right so let me see what type of rest endpoint was that okay I need to give a all slash right because I have put a slash here so I'm going to give all slash okay yep this is returning an empty response okay so this is just returning an empty response for me so I have a plug-in installed so that is why I did showing in this way so this is basically an empty response empty JSON response let's go to the logs and then check what happened in the rocker container so yeah if you notice here in the logs we do have some hibernate logs because when I hit the on we had a select query generated from the hibernate layer and this is got generator this is what concentrator right so literally it is now accessing my spring boot application and then it is connecting to the database and then it is retrieving the data so let me check if I have any repository I think I don't have any rest endpoint to create a user so what I'm going to do is I'm going to create a I'm gonna add a rest endpoint just to create a user just to show you quickly this is not following any rest standard so don't complain in the comment section saying okay this is not following this is not the right way to do it I know it is not the right way to do it but I just wanted to show you as an example because there's just the learning phase right I don't want to I want to make it simple so that everyone can understand right so I'm going to say hit I'm gonna hit the create options and this is going to recreate a user's repository so it is going to create a new user face so what is the name I'm gonna give it a SAM right I'm going to give the salvias and also I'm gonna give the team name as the okay I'm going to use the user repository to save the user and also I am going to return okay let's return the whole list right so that what all is left so I'm going to return a list here okay so once I once we hit the clear it is going to create that in the repository and then it is called a return so let me rebuild this particular project okay so meanwhile netted are labeled let's go duty doc in a now we need to stop this doctor camping site so how do we stop it let me clear this so I need to stop this so I need to stop I need to say doctor stop and then I need to give the container me okay so this is going to stop the docker container which is using this spring boot application so let's check if it is up now let me do what occur logs it is just going to show the logs if the last logs okay now I'm gonna remove this particular image so if you notice your docker Arum say that there is no container because the container is down right so if I say docker images it is going to show me the image but I don't want this image I'm going to remove this particular image so I'm going to say docker and then face this I think I don't know if I can give this okay unable to remove the repository container is being referenced okay so we need to [Music] create we need to delete this particular volume you know such volume let me remove that particular right so I just remove the dependent container and then I'm just removing the image so if I do a docker images it should not show ya my docker image is so clean now I don't have the same product right I don't have this image now let me go and check if the build is successful yep the build is successful and I'm going to create the docker image in the same vein so I'm going to use the same come on I'm just saying darker build with the tag users F and my sequin this is going to again repackage everything and then I'm going to have a new docker image you so the docker image is ready now we need to run the same command so I'm going to use the same command charm yep the docker run - P right this is going to linked my application to thee my sequel this is already running so I'm gonna say docker logs and you can notice that the dog the JVM is coming up and it's already up base P so let's go and hit the restraint point again so this should return the empty response again yep we got it and we created a new resting point right which is called create let's hit that create and this should know okay this is under which this is under on slash create okay pardon me the if the naming conventions are wrong so yeah if you notice here there is an exception let's see what is the exception right I think I know what is the problem let's go to the logs and then check it the long huge okay basically IDs for the class must be manually assign so basically I did not have these IDs auto-generated so that was the problem I can do that I can just do this strategy yep so let me create this automatically right I'll have to be build it again let me rebuild it meanwhile I'm not to just talk to the same process right so I'll just stop my image basically the container I just taught the container then I will remove my image you I've never tried reusing the same image so I'm not sure if it works but yeah let's see okay something need to remove some okay good talk my babies we don't have it right I will just let me check if the police put up the build is good let me build it again let me create the build right create the docker image again docker build up Indy users happy my sequel you you you yep the build is successful and the image would have got created yep the match got created let me run the talker run command again I'm just running the same docker run come on which is going to link my my sequel standalone with this particular spring good application so it's up let's do the docker logs this should yep you notice here the spring boot obligation is up let me hit the cast endpoint just to make sure it's all good all should return empty yep nothing here to create so now this should read on something and I again get an exception saying 505 I should have added the ID automatically - - - what is this exception Heidi doesn't have a default value oh my god what I'm gonna do is I'm not going to add the auto-generated instead I just assign a value right because I'm gonna call this only one so I'm not bothered about as any fun oh but what you can do is you can just have a proper auto-generated ID generated here right so you can use that let me rebuild it meanwhile I have to stop the container container this you getting sloppy of the Pantanal stop now I don't know this eh okay my images are the moon so hopefully I don't have with you I don't have money anymore okay we'll be successful as well I'll create the image again don't couple D use those - my sequel you who you give the images quad sorry the jar has got built so let me sorry the images got build let me run the docker container and I think it is run now let's quickly yep it is coming up it is it is still yep the server is up let's directly do it read this should create and then it should get as a response yep if you notice here we have got the response and let's go to the logs and then see if it really get got created yep it has got created and then it has got inserted and then it has got select it right so there is a insert query as well and then there's a select command as well so basically this has got created inside the my sequel instance and you can see that the responses here if I directly go to all that also should return the same yep we have the same response here which is Sam this is what we created right now okay so this is how you can access a my sequel container which is running as a standalone so if I show you the container right so don't contain so you can see two different containers one is the my sequel container which I named it as my sequel - standalone which is running under the board double 3:06 which is the default port right and then I have a spring put application which I have created and then from the spring boot apllication I am calling T this particular container how I am doing that if you notice here in the properties file where I will be modifying it so we did this change right we just changed D if you just have a T what do you call the hostname to be the container itself right and then we use the database we use the username and password we have start of the container using that username and password so that particular username and password is available for this particular container and we are using that from our application right so apart from that we using this dockerfile for creating our package so I'm using openjdk I'm adding the target which is nothing but the jar file on to this particular image I'm exposing the port number 8 0 8 6 MB empty point for this particular spring put a plication is Java jar and the jar name right so this is how we are creating the image so what I initially did is I started the docker image of the my sequel using the docker command so I'll just paste all these commands in the github repository so what I will do is I will upload this particular whole project into the github so that you can directly take it from that I will also add some documentation so I haven't reached me here but whatever Louise I'll add some more documentation onto the readme right so I like that and then you can see what commands I ran it will be in my history you can see there different commands which other but I just add those step-by-step so that you can use that particular project to do it in your local machine so that's what we did we started the my sequel server one thing my sequel server got started then we created a jar from the spring good application then we built it using the doctor will come on once the docker images build then we need to run that so using the docker run come on I am just running this particular docker image in a container where I'm exposing 8 0 8 6 port and then we are linking the my sequel standalone to that particular Springwood instance right so that is how you have you can use talker to connect from single container to a different container which is having a MySQL database so you can use the docker locks to view the logs both both occur for both the containers you can use the dropper logs will give you these knobs right so this is how you can use docker to create a spring coat application or deploy a spring put application and same way how to start an my is equal so using daughter right I hope you guys understood if you have any queries or questions to drop that in the comment section below I'll be responding to it whenever I save hope you guys found it useful meet you again in the next video thank you very much [Music] [Music]
Info
Channel: Tech Primers
Views: 117,408
Rating: undefined out of 5
Keywords: run mysql in docker, java application in docker, run java application in docker, run spring boot in docker, run database in docker, run spring boot and mysql in docker, spring boot docker, mysql in docker, mysql in a docker container, how to deploy mysql in docker, how to run mysql container in docker, how to run mysql in docker, how to run spring boot in docker, how to create mysql docker image, how to create spring boot docker image, mysql docker, deploy jar in docker
Id: fvEWoy1xOvo
Channel Id: undefined
Length: 33min 11sec (1991 seconds)
Published: Fri Dec 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.