Using Docker Compose with Mysql And Node

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's happening guys so previously i showed how to dockerize a node api and why you would want to do that now i'm going to take it one step further and show how and why you should use docker compose to start apps that not use more than one service or even actually apps that use one service docker compose is a really handy tool [Music] so first off why do you need docker compose and docker compose helps when you have lots of different containers that need to be started with different configurations in them this helps you put all that configuration into one file so here's an example here's a node.js app that i've made it basically connects to a mysql database when you hit this connect route then it creates a table afterwards which creates a numbers table and then when you go to the insert route it inserts a random number into that table and when you go to the fetch route it fetches the number we just inserted a very handy application so how would you start this using docker normally well without docker compose well if we go into the readme i've laid it out here so first off we need to build our node api version one image and that builds this docker file into an image and this is what i went through in my last video then what we need to do is we actually need to create a docker network and i called this network node api network and the reason we need this network is because we need mysql container to be able to talk to our node api container without having to expose any ports publicly so then we need to start our my sequel server to do that i'm using the docker run command which runs a container based on an image and the image i'm using is my sequel version 8.0 and i got this image off of docker hub and it's tagged with 8.0 so that's where i got that then i passed through the dash dash rm command what this does is it removes the container once we stopped it the hyphen d argument what that does is it detaches to from our container once we started to allow us to use the terminal again then i passed through a name of mysql server and this mysql server name is used as the host name because we're running these two services in the same network then i pass through these environment variables so i got these environment variables from the docker hub readme here and what these environment variables are they one is the name of the database one is the name of the user the password of the user and then the password of the root user then i tell this container that it must be part of this node api network cool and that's how we start the mysql server then to start the node api it's very similar we pass through the rm argument to remove the container once we've stopped it the name i gave it the name of node app so that when we stop that we can just use stop node app instead of looking up the id then the network i joined it to the same network as the mysql server and then i passed through the ports 3000 so i'm mapping our localhost port 3000 to the containers port 3000 and i also pass through this volume command which says this present working directory that we're in here should be mapped to the internal app directory of the container and then the image we're using is this node api v1 image which we built here using our dockerfile here cool so after running all of that we'll have our app started and then we can stop it using these two commands so this is how we run this normally without using docker compose to use docker compose we can start this app by running daku compose up and when we're finished using it we can run docker compose down to shut down the containers it's clear which way is a bit more user friendly for this project so let's start by creating our docker compose config so i'm going to create a new file here i'm going to call it docker compose dot yaml and this is a file name that docker compose understands itself so the first thing we need to do is pass through a version here and i'm going to use dr compose version 3.8 and then we need to declare our services so our services will be our app which is our node api then we'll need to say what image this app service is based off of so i'm going to pass through the build field here and bill takes a context as well as a docker file the context will be this directory and the docker file is our dockerfile here which is just called dockerfile and that will turn our image into a running container for us then if we go back here to our readme then we can also see that we need to map our ports so if we go to docker compose we can just pass through the ports key and we need a map 3000 on our local machine to pour 3000 in the container then we need to also map our volumes so our volumes will be mapped as our current directory with the app directory and we also need to pass through this anonymous volume here that's in our docker file and what this does is it basically says use the node modules folder that's inside the container and ignored the node modules folder inside of our local directory because we don't have one in our local directory because node modules are being installed installed inside of the container and then we don't need to add the network key because automatically when you use docker compose and docker compose creates a network for all your services so they all run in the same network and we don't need to pass the name because this is going to be used as our container name cool so now we have that let's start on my sql server so i'm going to give it the name of mysql server and like i said this name is going to be the name used as our host and because we're not building this image we can just use the image field directly that's gonna be this image here that i've taken straight from docker hub and if we go back here we can see that we don't need this command we don't need this command we don't need this command we do need our environment variables though so to use these environment variables we need to pass the environment key and i'm going to paste these here and it's going to clean it up so this becomes a list of key values and then i don't need that don't need that don't need all of this cool and you can see here i started my sql first and that's because node api actually requires my sql to run so what i can do here is i can just add the depends on field and this will just take the name of a service so every time we start app now it's going to run it will look at this see it depends on my sql server and also start mysql server so we can save this and that is it we've created our config and we should just be able to run our application so now we can run this app by using docker compose and if we run docker compose up it'll start our app and i want to show you here you can see it's creating a network for us then it's creating our mysql server and then it's creating our app awesome and then you can see it's listing on port 3000 so everything should have started if i refresh this cool we get hello world back if we go to connect we can see we can connect to my sql if we go to create table we can see we've created our numbers table then if we insert we can just do this a bunch of times we can see we've inserted numbers into our table and if we fetch cool we can see we have all the numbers that we created into from our table and to stop our services we can just press ctrl c here this stops the services we can see we stopped them and now none of this should run everything should just say can't be reached and then if we go docker compose down it removes those containers and that's it that's how to use docker compose cheers guys
Info
Channel: on the code again
Views: 3,826
Rating: undefined out of 5
Keywords: html, html5, learntocode, learningnewthings, tutorial, developer, learnfromhome, codingisfun, coding, code, web development, css, javascript, code bootcamp, basic coding, docker, docker-compose, docker compose, mysql, mysql docker, docker mysql docker compose, docker mysql node, node, nodejs, api, docker-compose api, how to mysql docker, docker tutorial, mysql docker tutorial, node mysql, simple docker setup, node docker setup
Id: 9-iLqe-E9iI
Channel Id: undefined
Length: 8min 24sec (504 seconds)
Published: Mon Mar 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.