Skip to main content
View on YouTube to comment and like!

Syntax Video

Docker Quick Start for Developers | Hands on Examples

Chapter 1 0:00

How do we run a redis database locally?

now your first example of using Docker is to use the docker CLI to temporarily spin up a container now for this example I'm going to run this Pokemon cacher API that caches responses from the Poke API inside of a reddis database now uh to run this locally I would need node mpm and reddis I don't have reddis so there's a few ways to go about getting it now the first way is to just install it on my local machine and the instructions are going to vary depending on what operating system I'm on I could also install it on a remote machine like a Linux VPS and then connect to that remote instance I could also spin up a cloud instance but I might actually have to pay for that cloud instance and then connect to that cloud instance or I could use Docker to run redus locally inside of a [Music] container now let's get this app running on my machine if you'd like to follow along the link to this repo will be in the description and you can just clone it down and follow along with what I'm doing so I'm going to clone this here in

Chapter 2 0:43

Simple Example API Setup

this directory and then go through the setup which tells me that I need to do an mpm install great and then I need to create my own environment variable file so I'll run this command then we'll open this up with VSS code and edit it further we can see that this example EnV is attempting to connect to reddis here on my local machine on Port 6379 and if we try to start up the app let's just do an mpm run Dev we will see that it's attempting to connect to redus but there is no instance running on Port 6379 on my machine so let's fix that by running reddis with the docker [Music] CLI now for any type of image that you're looking for you can always use dockerhub there are other sites as well but dockerhub has all of the official images listed and for example we're on the reddest image here but if you wanted to run MySQL locally there is a MySQL image if you wanted to run a postgress

Chapter 3 1:34

Run an image with the docker CLI

there is a postgress image um pretty much any any major service that you can think of there is an image already on dockerhub that you can just run locally so uh for any of those images take a look at their page on dockerhub and they typically have just like a oneliner that you can run that will actually start up that container so let's actually just run this command and see what happens so first if you've never pulled this image before it is going to have to pull that image down and then it's going to save it onto your machine but after it has pulled the image the container is actually running and if you run Docker PS you can see there is a container running however we have not exposed the port the docker PS will tell you that hey this container is uh listening on Port 6379 but we need to expose that to our local machine uh you can also see this inside of Docker desktop so if you pull up Docker desktop you'll see that there is a redus container running um also if you take a look at images you'll see that image that it downloaded and then also most images also have some sort of volume associated with them and

this is where they're storing uh the data for that specific containers because containers are temporary but any changes are saved in this volume here um but if I want to connect to this I'm going to need to expose that Port so the first thing I'll do is actually I will just stop and kill this container and then I want to create a new container that actually exposes that Port so I can connect to it on my local machine now uh to figure out what port that is you can always look at the docs for whatever service you're using we know that redus runs on Port 6379 so we're going to add that into the command here so if you run the command they gave you but to also add A- P so- P stands for exposing a port and in this case we want to expose Port 6379 on our local computer and we want to uh link that to 6379 running inside of the container now if you already have something running on your machine on 6379 you could pick a different port like that um but this this is just the port that will be

available on your machine so in the codes here I I could set this to anything I wanted but I would just need to make sure that that match is this value here but now when I run the container it will expose that Port the other option you see here is- D that stands for detached uh but if we wanted to run in the foreground to actually see the logs we can actually just remove that- G and now try running it so there we go redis is running and the port is exposed now to check this we can use Docker PS so if we do Docker PS we'll see that the container is running um but you definitely want to see this this actually tells you that the port is exposed so this is the port inside the container and then this is how it's exposed on your local machine so now if I start up my node.js app it should be able to connect to that Port but we're getting a different error now now it's saying that we have the wrong username and password now to find or set this username and password you can usually look at the docs for any any container that you're running locally but this reddis instance actually is running in nonprotected mode which means we can connect without a username and password

so for this specific use case in this specific type of container that I'm running I'm just going to remove the username and password and then try starting up my app again great so now the reddest container is running on 6379 we have our node.js app running on 5000 and I should be able to visit it it in the web browser cool and then if we look at the docs for this API we can see that SL API V1 pookemon is what we can use and then we'll just specify a Pokemon name like Pikachu we get back the response so we know that this API is running and it's able to cash those responses inside of reddis [Music] now there are a few other things we can do with this container here so I showed you earlier we have the docker PS command this will show me all of my running containers and you can reference a container by its ID or its friendly name and in our case when we started it up we gave it a friendly name so if I do a Docker stop of some reddis that will actually stop the container and then you

Chapter 4 5:26

docker CLI commands

can see that node is freaking out because it doesn't have anything to connect to if I want to start it back up I can do Docker start pass in the name that'll start start it back up and then nodejs should start to connect to it and and not throw errors anymore um but you might want to be able to see the logs inside of that container so if you do Docker logs and then specify the container name that'll spit out all of the recent logs now uh the other thing you can do is you can follow these logs so right now this just spits it out and then gives me back my terminal if I pass in a-h F this will just stay there and any new logs that come in you'll see those happen as well now there's a lot of things you can do with the CLI and even don't have these me these commands memorized I had to look them up for this video um but if you want to learn more check out the docs they have the docks on the main Docker CLI command but it's important to note there are a bunch of sub commands and so specifically there is the container sub command and that is actually where things like start and stop and logs that's where all of these

sub commands come from they're just Alias so they're they're easier to run but if you want to control your containers from the command line you can look here for all of the other commands that you can use now personally I just use Docker desktop because it's super easy to just go click the stop button click the play button I don't have to memorize any sort of commands or anything like that um and then the other thing that's nice is when you remove a container here behind the scenes it automatically removes any like Associated volumes you don't have to run special commands for that um but you can go either way you can use Docker desktop you can use the CLI now in the next section of this video I'm going to show you how we actually don't even have to use the CLI at all uh because if we want this to be a reusable process right let's say there are other people on our team we want them to be able to run redus locally technically we could put this little command here in our readme and then you can tell other people hey just go run this command and you'll get yourself a redus container but a better way to kind of like wrap that up and make it reusable is with Docker compose and Docker compose is typically how I

use Docker and how I'm even interfacing with with Docker that's running on my [Music] machine next let me show you a quick and easy example of Docker compos so take this command that we ran earlier where essentially we specified an image name from Docker Hub and then we ran it and we exposed a port we can replicate that inside of a Docker compos so here in my codebase I'm going to create a file called docker-compose.yml and then inside of here I'll specify my services now I'm getting autocomplete that's because I have the docker extension installed inside of VSS code I recommend it and then from here we can specify all of our services in this case I just have one service you can give it a name uh if we wanted the exact example we could call it some reddis but I like to be a bit more descriptive so I'm just going to call this DB and then uh we specify the image that we want now from the command line we said the image name was reddis so I'll add that here and then from the command line we also said that the port

Chapter 5 7:54

A simple docker compose example

Expos is 6379 so there's a ports option in here and we can just do 6379 and 6379 and there we go and so I would actually commit this file to source code so that other developers can instantly spin up this redus instance because now that we have this file I can just do a Docker compose up it'll look at that file see all of the images that I need spin up all the containers that I need and I'm good to go and then if I kill this it's going to stop all of those containers and and I'm I'm done so uh this is honestly the the most of how I interact with Docker is I just have composed files that specify all of the images that I need to run and also like custom images which we'll talk about in a later section in this video now uh an important thing to note here is the image section here when you specify just the image name bear like this it's just using the latest version of that image and pretty often you want to specify a specific version and so on the docker Hub page you can see all of the versions that are available and for instance we

might just pick the latest which is 7.2.4 so I might just lock this down and say pull the 7.2.4 image and now just in case there's like breaking changes in the latest version are our composed file at least locks it down to a specific version now there's a few other things we can do in this file we we'll get more into it uh later in this video but for now one thing of note is that you can also use environment variables so for instance we have this environment variable reddis Port that we want to use and if you want to use that inside of a composed file you can just do dollar sign curly braces and then the environment variable name now by default Docker will look at the current folder and if it sees a EnV file it's actually going to read in those environment variables you don't have to do anything special but now this is actually linked to our environment here so if I were to specif specify a different port here uh now when we spin up the container it's going to expose that port instead and because I specified it inside of that EnV file my node.js app is actually also going to use that port to connect to the

service and so everything is still working here as [Music] well this next example will be a slightly more complicated use of the docker CLI and then also Docker compos so for this I want to run this API called backpack debuggers it's part of the backpack debuggers game and it gives you an API it's running node.js and express and it has a few more requirements so we're also going to need the image magic CLI and we're going to need a postgress database so I'm just going to get this thing cloned down locally and then we'll try to get it running by spinning up a Docker container with post grass inside so let's clone it down and then if we look at the docs they say we should run an mpm install so let's do that and from there we will create an example. EnV file and now we need a database in order to migrate and Seed it so if I open up vs code here and then we take a look at the EnV we're going to need a postgress database that allows us to connect it

Chapter 6 10:49

More complex example API setup

with connect to it with this info so if we look at dockerhub for postgress uh similar to like the the reddest image that we saw earlier uh they give us a nice little CLI command that we can run but it's going to be a little more complicated than this because we have other environments

Quote it with credit and a link. Quoting Syntax