Syntax Video
Docker Crash Course for Developers | Hands on Examples
- 0:00 Intro
- 0:13 Docker Desktop Alternatives
- 0:50 How do we run a redis database locally?
- 1:39 Simple Example API Setup
- 2:30 Run an image with the docker CLI
- 6:21 docker CLI commands
- 8:50 A simple docker compose example
- 11:45 More complex example API setup
- 15:41 docker compose example with postgres
- 17:48 Persisting data with docker volumes
- 21:51 The case for custom images and containerization
- 23:32 Creating a custom image with a Dockerfile
- 30:02 Ignoring files with .dockerignore
- 31:03 Examine container files and execute commands with Docker Desktop
- 32:31 Run migrations and seeds from within the container
- 33:58 Make code changes without re-building image
- 34:55 Create a volume for node_modules to prevent internalBinding errors
- 36:35 Better development workflow with devcontainers
- 38:03 Optimize docker layer order for faster builds
- 40:09 Create a multi-stage Dockerfile for dev, build and prod
- 46:23 Share docker-compose.yml configurations
- 48:41 Thanks!
Intro
so you already know what Docker is and what it does in this video I will show you Hands-On examples of when and where you can start using Docker in your apps today so let's do it I'm CJ welcome to [Music] syntax so in this video I am going to be using Docker desktop but you should know they have a license that says if you work at a larger company or company that makes a lot of money you do have to purchase a license uh but it is free for personal use education purposes and non-commercial purposes but if the license agreement does not agree with how you're going to be using Docker there are alternatives such as podman desktop Rancher desktop orb stack which works on a Mac there's also a command line tool called kma that works on Mac and Linux uh but in this video like I said I'm going to use Docker desktop but if you use any of those Alternatives everything I show you will work there as [Music] well now your first example of using Docker is to use the docker CLI to
Docker Desktop Alternatives
How do we run a redis database locally?
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 this directory and then go through the setup which tells me that I need to do
Simple Example API Setup
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 vs 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] 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 uh postgress there is a postgress image um pretty much any any major service that you can think of
Run an image with the docker CLI
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 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 could also see this inside of Docker desktop so if you pull up Docker desktop you'll see that there is a redest 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 you also add a-h 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 matches 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 DD 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 and 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 to the response so we know that this API is running and it's able to cach those responses inside of [Music] reddis 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 can see that node is freaking out because it doesn't have anything to connect to if I want to start it back up
docker CLI commands
I can do Docker start pass in the name that'll start it back up and then nodejs should start to connect to it and and not throw it 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-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 I 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 compose so take this command that we ran earlier where essentially we specified an image name from dockerhub 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 exposed is 6379 so there's a ports
A simple docker compose example
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 rdus 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 an important thing to note here is the image section here when you specify just the image name be 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 our 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'll 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 composed so for this I want to run this API called backpack d buggers it's part of the backpack debuggers game and it gives you an API it's running nodejs and express and it has a few more requirement 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 with connect to it with this info so if
More complex example API setup
we look at Docker hub 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 environment variables that we need to specify like the database name and the database user but let's start here so I'm actually just going to copy this command and then we're going to figure out the full CLI command that we need I'm just going to put it in my readme because I I don't want to like be editing text in my terminal okay so we're going to start here uh we'll give it a name in this case we'll just call it uh backpack DB and then we need to specify these other environment variables so if we take a look in the EnV we're going to want the password to be super secret and then we need to figure out how to specify these other environment variables as as well what's nice about the postgress image is they actually have docs here on all of the other exposed environment variables so the postgress user is also exposed and we can specify that so in our Command we'll
do- e post user and then we can make it match up with our EnV so that will be EP debug like that now we also need one for the database name and they expose another uh environment variable postgress DB so let's set that so we'll do- e post DB and then we want it to be uh backpack debuggers like that and that should be all the environment variables because we are connecting to Local Host the last thing is we need to expose the port and so uh this is always going to be- P but the port that we expose will change depending on the kind of service that you're running in our case 5432 is the default port for post so I'm going to expose 5432 uh on my local machine and connecting to 5432 and inside the container so this should do it let's try running this command and hopefully we get ourselves a nice little postest image running locally so yeah just like last time because we don't have the image it's
going to have to download it but it'll only have to do this once then from here on out it'll it'll uh use the downloaded image uh so it looks like it's running if we do a Docker PS yep so it's running in the background the PO the port is exposed so let's try running the commands in the readme here so let's try migrating the database it didn't fail I'm going to assume it worked and then let's try seeding the database all right and then we'll spin up the app so npm runev and let's see if we can get some monsters it works awesome so I was able to get a container running I specified all the same connection info so that way my nodejs app connects to the same data that's running inside of a Docker container now uh this is this is very cumbersome right I don't have to run this from the command line every time every time so this is a much better use case for Docker compose where we can
take all of this and and wrap it into a nice compose [Music] file so just like last time we will create a file called docker-compose.yml we'll specify our services we have a DB service our image is going to be postgress but again we want to specify the version number so let's look at their docs here let's grab 16.2 and then we will specify the ports and this needs to be 5432 and 5432 now we need these environment variables so here we'll say environment and we'll specify the name but here we can use our our own environment variables from our EnV file as well so for example postgress password needs to match up to DB password so let's specify that here and then we need our postgress user and that needs to match up to DB user Etc so let me fill out the rest of
docker compose example with postgres
these and then we'll we'll see if we can spin it up okay this looks good we've got image name the exposed Port actually this needs to be from our environment variable as well so this is DB Port cool so this is the exposed Port the port inside the container and then we have our other environment variables this should be all we need so let's go ahead and stop this uh container that we just SP spun up from the command line directly and then we'll run Docker compose up so we're going to kill our API we'll do a Docker compose up it's got our image and then we can see it's ready to go so database is ready to accept connections so let's try spinning up our app no we get an error because we remember we have to run the uh migrations and seeds and uh if you didn't know this already these containers are ephemeral like they're they're very temporary so because we just spun up a new container it doesn't it the migrations have not been run against it and the seeds haven't been run so we need to migrate it we then need to seat
it and now we've got the data good to go so uh we we've wrapped up that CLI command into a nice compos file but there is still one more thing we need to [Music] do now another thing we can do is specify a place to actually store the data for the database itself Now by default if you take a look inside of Docker desktop you can see that behind the scenes there actually is a volume that it creates um and that's where it stores the data and so that's why we technically are able to uh shut down the containers from Docker compose start them back up and then the migrations and seeds have persisted so we can still access that data from the API but one of the things you might do periodically is like delete running containers delete images and if you want that data to persist in a consistent way then you can create a volume inside of the docker compose as well so let's do that now if you take a look at their documentation
Persisting data with docker volumes
they have a section on uh PG data and any other type of database that you're dealing with is very likely going to document this well this as well like where are they storing the data now they're storing that data here and what we want to do is we want to store that data on our computer and then map that to this location inside of the container and you can see in their example command here- V is how you specify volumes from the command line if you want to do that but we can also do this inside of our Docker compose so in our Docker compose we can specify volumes and then we're going to map some place on our computer to the data folder inside of the container and what I typically like to do is just create like a DB data folder in this folder right here so this will create it here and that's where all the data will be stored and that way um if if I ever blow away the containers or the images or whatever else it will map it back to this folder and all of the changes that have been made to the the data in the database will persist so let's use this we'll Now kill the container start it back up and
it will now use that volume um but because it's using that volume now it doesn't have any seated data it doesn't have any migrations that have been applied so we're going to run into that same error that uh it doesn't exist but if we run the uh seed and migrations commands again so let's do an mpm run DB migrate so that'll create the tables and then we'll seed it to insert the data and then we'll start it back up now that data does exist and if we take a look it actually is putting all of this stuff inside this DB data folder and so this is all the stuff associated with postgress and um we want to make sure that we don't commit this info to our our git repo because this is a lot of stuffs you typically only want this locally so that way you can pers persist your data locally so always want to make sure that you add this to your get ignore just so you don't accidentally commit and push it up so we're going to add DB data in there and so now uh this
won't get pushed up or won't get automatically tracked whenever I'm I'm committing or adding files uh but locally if I ever remove and delete the container all of the data is stored and so let's actually see an example of that so if I kill this container and then I go over here and delete it from Docker compose and then we have some volumes sticking around let's actually delete those but I will show you I'm all over the place but let me show you there's a command line thing you can do to this so you can say Docker uh volume prune and this will delete all of the volumes that are on your machine so that's the command line way of deleting that container you could also just click the trash button but uh at this point there are no more volumes that exists the containers are gone but because I mapped it to that specific folder if I start this container back up it's going to use the data inside of that folder and now I don't have to run migrations and seeds again it still has that persisted data now we just scratching the surface of the docker composed CLI and then also the docker file itself so I'm going to link these articles in the description
they talk about other commands you can run from the CLI tool and then also they talk about the docker compos file specification so they they talk about like the services section and the volume section and all of the other things that you might want to do inside of a Docker compos so you can look at that [Music] here next up I will show you how to make our own custom images and containerize this API itself but let's first talk about why we we would want to do that now I already showed you the endpoints that are talking to the database those endpoints work just fine this endpoint here resizes images dynamically and it it needs image magic behind the scenes so let's try visiting this inpoint so if we go to Local Host 5000 we can specify the monster number we want let's say we grab monster number five we then specify the size so this is size in pixels and then you do image.png and so this will use the base image and then resize it to 100 pixels uh wide now you notice we get an error image magic is not installed so
The case for custom images and containerization
this is another potentially cumbersome thing that needs to be configured on my computer to get this API running now because I'm on a Mac I can use Brew to install this so let's just do that if I do a brew install of image magic that's going to uh add it to my machine and then the API is actually using that command behind the scenes okay so it's installed and so now if I hit this endpoint we actually get the image back um but that's I don't want to have to do that right and and so this is something that i' I've also glossed over is that anybody that wants to run this API also needs nodejs and mpm which again they're fairly trivial to install but it's going to vary depending on operating system depending on potentially where you're deploying this to so what we want to do is we want to wrap this up into a nice little box a container if you will that has nodejs inside of it has npm inside of it and has the image magic CLI inside of it all ready to go where I can just run one command and instantly spin up the
Creating a custom image with a Dockerfile
[Music] API so to containerize this API here we're going to need a base image and because our app needs no JS and mpm we're going to pick the image that is nodejs so this is a base image that includes node and mpm and this is the official one and uh if you take a look at their docs here they actually link to how to use this image and they give us some examp examples of also a a Docker file and and a Docker compose we're going to specify a Docker file because this is how we'll create our own image now uh the first line in that Docker file is going to be which specific uh image we want to pull in in this case the node image so let's do just that in our source codes we're going to create a Docker file right here and then we're going to say from and the base image here is node now like earlier like I showed you in the docker compos you can leave off the version number but we want to specify a version number so that way uh if there are any breaking changes introduced um we're we're locked to a
specific version and you can see all the versions available here uh I'm going to pick version 20 because that's currently the LTS you can use just LTS as the version number like this but that will move as LTS changes so I'm actually just going to lock it down to to version 20 from here we want to set up this image to have everything that it needs so if we look back at the docs it requires the image magic CLI so we want to install that inside of this container now now this container is actually based on a version of Linux called ebn and that has a package manager called apt so if I say run app to install image magic like that image magic um that'll install the image magic CLI inside of the container now depending on what your base image is your package manager might be different but I know that this one's based on ebn which uses the APT package manager and so this will install the image magic CLI next up we want to copy the files into the Container so we want to take all of our source Cod codes here put it in the image and for that we'll use the copy
command so uh this says copy everything from this directory into the Container but uh I don't want to just put it at the root of the container I want to put it in a specific folder so before we copy I'm going to specify the working directory as slapp now this can be whatever you'd like in my case this is just easy to remember so all of my codes will live inside the slapp directory inside the container now in order to run this app we will need to install the dependencies so at this point because all of the source codes is inside the container it should have access to that package.json so we're going to run an mpm install from here we should be good to go so we have a base image that has node inside of it it's going to have that image magic CLI uh it's going to have all of our codes inside it and it will be ready to run because we've installed all of the dependencies so let's actually use this Docker file to build an image and we're going to do that inside of our Docker compose so here in our Docker compose we're going to add another service in this case I'm going to call it app and here I'm going to say build current
directory so this will look in the current directory find that Docker file and actually build it and then in this case I want to expose the ports because uh Port 5,000 is running inside the container and so for that I will specify 5,000 outside 5,000 inside and then lastly we actually want to run the nodejs app itself so there is a command you can run here and I'm just going to say mpm run Dev and so this is going to uh build the container expose the support and then actually run it so now let's actually spin down our Docker compose let's kill the local instance of our API and then let's try a Docker compose up now it will need to pull that node.js image because this is the first time that we're running it uh but then let's see what happens great so now it's going through each of those commands in that Docker file and then running them and I'll just show you so it actually ran the uh install of image magic that was good to go he created the working directory copied all the did an mpm
install and now the image is there and now we can see here Docker compos is spitting out logs and it actually differentiates the logs depending on the container that it's running in so any of these logs that have app one these are logs coming from the app container and then anything we have uh with DB are logs from the database container so we can actually see that the the image is running uh now as a container with with our Docker compos and we can try visiting it uh but now we run into an issue where we need the app to be able to talk to the database and because right now it's actually attempting to connect to Port 5432 uh and it can't because it's trying to do that with from within the container and we exposed 5432 to our local machine but we didn't expose it to this container here but what's nice is if I don't need to access this database outside of the container I can actually just remove that Port mapping and now I can just add a depends
on uh Clause here and I'll say that this depends on the database and so now that they depend on each other they're going to be in the same network so that they that way they can connect to each other now the other thing we'll do is in our EnV now our database isn't running on Local Host anymore it's running on this container called DB and so if I specify the host as DB uh Docker compos and Docker have set this all up to where inside this comp container DB will resolve to the IP address of the container that's running the postgress image and so now this should allow the app container to connect to the DB container so we'll kill the containers okay so the containers are stopped but we actually need to do a rebuild of our app container here because earlier inside of our Docker file we said copy all files into the image that got built and because we've changed this Dov we actually Chang that dbhost value and because this EnV is being built into the image we need to
rebuild it so we get the latest version of this andv inside the image so let's do a Docker compose up but if I pass pass in-- build that will rebuild the container to make sure we have all the latest files inside of it cool and at this point if we visit the app we're good to go so now the app container is connected and and able to talk to the DB container and we can also test that image res sizing inpoint just to make sure that image magic is working inside of the container so if I specify let's say size of 50 we get back an image that means that it was able to use image magic inside the container to resize that [Music] image one of the things we did earlier is we copied everything into this uh image for our node app but if you'll remember we also created that DB data folder and we don't want that to exist inside the node container so let's add ourselves a Docker ignore and anything listed in this file won't get copied
Ignoring files with .dockerignore
into your built images so we're going to add DB data there also because we're running an mpm install inside the container we don't need to copy the node modules in so we're going to add that to our Docker ignore as well and also if you're in a git repo you can add dogit to that ignore file now from here if I want to rebuild the images I can do it from the command line so I'll do Docker compose build and in this case I'm actually also going to pass in Das Das no cache uh so that way it doesn't use the cach and just builds everything uh from scratch we're good to go and let's just do a Docker compose up so we can take a look at what happened so containers are running [Music] again now if you look inside of Docker desktop uh there's a few things we can do here that I actually didn't show you so if we take a look at this app container um we can see all of the files inside of this app container and because we're put we put all the source code in slapp if we open that up we shouldn't see that DB data folder which is good
Examine container files and execute commands with Docker Desktop
which means it uh ignored that whenever it built the image uh the other thing is we can actually run commands inside of this container so if you click on the exec tab here uh I'm literally inside this container and I can run commands and and do everything else now I wouldn't recommend doing this often because typically when you spin the container down and bring it back up all of those changes are going to get lost but if you ever need to poke around and kind of see what got built into your image you can use this as well you can also do this from the command line so if you click this here this will actually launch your terminal and then you get a nice little interactive prompt where you can interact inside of that that running container um and you can see the command that it runs so Docker exec - I for interactive DT for TTY and then we have the container image ID but we can also just run this ourselves so if you do a Docker PS you'll see all of your running containers you're going to want to gra e grab either the container ID or the name of the container and then we can do that Docker exec command so if I do Docker
exec interactive TTY the name of the container and then sh I get dropped into a shell and now I can start running [Music] commands now this is very useful especially if you need to run like seed or migration commands inside the container so let's look at that as an example let's actually kill all of our running containers and then I am going to completely remove that DB data folder so we're starting from scratch the other thing I'll do is I'll do a Docker system prune and now this is good to run every now and then because it'll free up some space on your machine let's see how much space this frees up yeah I freed up about 300 megabytes so um all all of the cache all of the images all of the stuff that's not in use right now got deleted and so that kind of freed up the space as well and so from here if I do a Docker compose up that's going to spin up our database and also spin up our app but remember uh we need to run the migrations and the
Run migrations and seeds from within the container
seeds because if I attempt to visit my API here it doesn't have those TBL so I need to run DB migrate and DB seed inside the container so we can use Docker exec to do that so if I do Docker exec and in this case I don't need to be interactive I just want it to run the command I can specify the name of the container and then the command I want to run in this case mpm run DB migrate and then we'll run the seeds and so this is running those commands inside the container and from here we should be able to access the data from the [Music] API now at this point we want to be able to work on the API and like add new features without having to rebuild the container every time we make a change and we can do that with volumes so just like earlier we specified this DB data volume to map the data from within the postgress container we can have a volume
Make code changes without re-building image
here that Maps the current directory which has the source code to the slapp directory inside the image so let's do do that right here so we'll spe specify the volumes we'll say map the current directory to slapp inside the container and now when we rebuild it whenever that container spins up it's going to be looking at the current working directory and so because we're running mpm runev if we make a file change that's mapped inside the container and it should automatically restart so let's give it a go we're going to kill these containers we will rebuild the app container so I'll do Docker compose up and we'll do a build now it rebuilt but when it tried to start we actually get this very cryptic error here and uh this has to do with our node module so when the app is running inside the container it's running in Linux in this case I'm on a Mac so locally my node modules were were built for a Mac and uh we need to avoid that issue and so because we mapped the
Create a volume for node_modules to prevent internalBinding errors
current folder into the Container it is essentially like overriding the node modules that got created inside of the container so if you ever see these weird cryptic node errors you should try doing this we're going to create a volume that is literally just app SL node modules and so that way it doesn't use the node modules that are in my directory that were potentially installed for Mac OS or Windows or or wherever you're you're doing this and it only uses the node modules inside of the container so let's do that and try rebuilding and it looks like it's working this time so I should be able to visit the inpoint good to go and I should be able to grab an image as well good good to go now let's try adding a change and see if the container restarts so let's go over here at this point you don't have to actually know anything about nodejs just know that I'm going to add a new inpoint and it's just going to be a test inpoint so let's say I add a little uh inpoint here okay so I have this little inpoint I added it if we look at the logs we can
see that this this change was actually actually detected inside of the container and the container restarted so I should be able to go to images banana and see those latest changes great so when I make changes locally in the folder it actually is seen inside the container and that process restarts which is [Music] great now this kind of development workflow works perfectly fine for this simple little API um I can make my changes the container sees them it restarts the process but if you're working in other Frameworks it's not always this quick and easy so if you ever try to do this with like a react app or a nextjs s app or uh even like spel kit a lot of times this is a very cumbersome uh development workflow because it actually takes a few seconds before those changes are detected and then it needs to be rebuilt and reloaded inside the container and that is not ideal now the answer to this is Dev containers and I'm not going to show an
Better development workflow with devcontainers
example in this video because I could do an entire video on dev containers but the idea is you have a specific dodev container folder inside of your source code and that Dev container folder will have a Docker compos it'll have a Docker file and then uh VSS code actually attaches itself to the running container itself so you're not doing this thing where we're mapping files on on your local machine into the Container you will actually be developing literally inside of the container and it handles all of the intricacies of that but all you'll need from that point is a Docker file and a Docker compose uh but like I said um I can make a whole video on this definitely check out Dev containers if you are uh if you have a really slow development process when you're trying to like work inside of a of a Docker container like [Music] this now from here you should be good to go for basic development with Docker containers this isn't how you should do things if you're actually deploying Docker containers we'll talk about that
Optimize docker layer order for faster builds
next but another thing I want to show you is actually uh kind of what's Happening Here in this Docker file so let's kill our containers and then just try building our app image directly now to build this image we can say Docker build build and we'll just do period because that means use the docker file in the current directory and then we're going to tag it and we're going to use the same name that Docker compose was using which is backpack debuggers app and so this will then build the container and we've seen this already we can see all of the what are called layers that are happening here so like two out of five three out of five four out of five each of these is a separate layer and uh whenever we make code changes uh that is actually affecting this layer here because if if one of the files changes when Docker runs again it's going to see that that's not cached and it will need to recopy and then R redo the mpm install so let's make a very simple change inside of our app and in this case something super simple like we'll just remove the semicolon right here from this route but if we run the build command again you're going to notice that it actually needs to run the mpm install command again and that's
because these layers are cached in order so if any any file changed it's always going to have to run an npm install so one thing we can do is we can say uh not don't don't care about any file we really only want to run the mpm install if the package or package.json file has changed so if I do a copy of package star. Json so that'll that'll pick up both the package.json and package-lock Json and then copy that into the Container now npm install only has to rerun if either of the package.json files changed and not if any of my source code changed so with this one changed now we we'll have to rebuild one more time and it's going to run that npm install but as long as I don't change the package.json or the package lock then those layers are going to stay cached and so we can go in and make another code change like this and now when we don't do a rebuild um the only thing it had to change was this copy command so the build is super [Music] fast now what I've showed you so far
Create a multi-stage Dockerfile for dev, build and prod
will work perfectly for development and if that's all you're going to use Docker for you can stop there but if you would like to start using Docker in production as well there's a few things we need to do to the image we've created to make make sure that it's production ready now one thing is because this is a typescript API it has a build process so whenever we build it that's actually going to Output some JavaScript files and this is actually what we want to be running when we're in production and so we need to create a container that only has the built files here it it we don't want to be using TS node in production so for that we're actually going to use what are called multi-stage builds so you can check out the docker documentation on this and essentially what allows you to do is create uh different stages of a build inside of a single Docker file so we're going to take the base Docker file that we wrote and create some other stages now we'll rename the base code here as base so I'll just say as base and now I have defined a stage called base from here we'll create an image based on this base image so this base image has all of our dependencies right it has image magic
and then all of our node modules as well but now we want to create a Dev container that has all of the source code inside of it as well so I'll say from base as Dev and so now I have a stage called Dev that has all of the files inside of it and so from here we can use these stages inside of our Docker compos so in my Docker compose file instead of just saying build with the context of this directory we're going to specify some options so we'll say context this directory but we can Target and specify which stage we want in this case we'll specify Dev and so now we're going to be using this uh stage which is based on this stage so before we rebuild the container let's actually update our Docker ignore because uh locally I ran a build and it has this disc folder here and I just want to make sure that that disc folder does not get copied into the image so I'm going to add disc to my Docker ignore as well now we can attempt to build so I'll do a Docker compose up and build it so it's up and running I can go ahead and check and see yes the API works so we're good to go with our Dev
container from here I want to create a prod container so a container that only has the disc folder inside of it doesn't have all of the the source code so let's do just that so we're going to create another stage and we'll call this the build stage so I'll say from Dev we'll say this is the build stage and we're pulling from Dev because we need all of the source code to be able to run the build and here we'll just run an mpm run build and so this command is going to generate that disc folder from here we want to create our production container now our production container is going to be a combination of the base image which has all of our dependencies and the actual built files that were created here in the build container so we're going to base it off of the base image so I'll say uh from Bas as prod and then inside of this image I want to copy the built files that happened inside of the build stage so I will say copy and from here you can specify which stage you want to copy from and so I want to copy
from the build stage and we want to copy uh app slist into slapp and so now inside the prod container it's only going to have the bundled built files inside that app directory it's not going to have all of the source code so this should be good to go uh we now have this prod stage but now we want to actually use it so one thing you'll see people do is actually have multiple Docker compos files and they'll just prepend it with like the environment so let's do that let's copy our existing one and name it.
prod. yo and then inside of here we're going to Target the prod stage instead of Dev and then I don't need any volumes here because I'm I'm not using this for development I just want to use the container itself and then the command is a little bit different so instead of running npm runev or npm start I specifically want to uh run the built files so I'm just going to say node Source index.js because for this app when the files are built and bundled that's all that will exist here is this uh this index file that needs to be run so with that we should be good to go now I I have my my prod stage ready to go I'm pulling from it inside of this Docker compose now let's try to run it so if you do Docker compose you can specify DF and then pass in a specific Docker compose file so I'm going to pass in the prod file we're going to spin it up and I'll tell it that it should build and uh it's working but we see that we get an error so my
app automatically throws an error if it's missing values inside of the EnV and this is actually a good sign because our container the prod container does not have a Doty EnV file inside of it right thatv file does live in the dev container but it does not live in the prod container because we only copied the built files over and so one thing we would want to do for our uh production file here is to actually pass those environment variables in and so we can do that in a similar way to how we're passing them into the database so I'll do that really quick and so now when we spin this up uh Docker is going to use the EnV file that's in our directory and it will actually pass these into the running container and because this is for Pro we can actually specify this as production directly instead of using uh the node EnV so this is going to run the container and now pass in those environment variables so it should be able to connect to the database as well so let's kill this and then try running it again and we're good to go so now we have the API running inside of that
production container now there's a lot more you should think about and a lot more you can do if you are going to be running these containers in production for one when you're running in production you might not be running a database uh from a Docker compose you might actually want to connect to like a remote instance or something like that and so these environment variables here could point to like something hosted on AWS RDS or something like that but I will point you to this repo here that talks about Docker and node.js best practices so this talks about some things you should take into account if you're going to be running a node container in production and it talks about things as like running as the non-root user and and a few other things that you can take into account now one of the last things we'll do here is remove some duplication in our Docker compos files so because I copy pasted that file both our prod file and our main Docker compos file have this DB service here and I want to be able to share that so we can do that with a base
Share docker-compose.yml configurations
image file and then extend from it so what we'll do is we will create another Docker compos file I'm just going to call this Docker compose do B.O and then we just need this DB so I'm going to copy it from this file throw it in and then uh get rid of everything else so that we only have the DB servers great so now that I have my base file here I want to extend from it in both of my other compose files so in my Docker composed. prod file I am going to remove all of this and just say extends and here I can specify the file I want to extend from I'll say Docker composed. B.O and then uh the service that I want to extend from in this case the DB service so basically all this says is take the definition here and then expand it out here and I'm want to do this same thing inside of my other Docker compos file so down here I can get rid of this and just pull in
that and now we can test it so if I just do a Docker compose up it should still start up my database and my API and we're good to go but in this case I kind of just removed some of that duplication so we have all of our base Services defined in our base file here now now just uh one last little nitpick is instead of having this Docker compose without an extension you might also addev on there I like the convenience of especially if I'm mainly using Docker for development if being able to just do Docker compose up and not specify the file name is pretty great uh but if you want some consistency in your naming here you could also rename this to dodev but now whenever you use Docker compose you're going to need to specify that file name like we did earlier so we' do Docker compose DF specify that we want specifically the the dev file and then say up and so this this starts the container but this is a personal preference but you can you can name these files whatever you want you're just going to have to specify those names uh whenever you're extending them or whenever you're running them from the
command line now I have just scratched the surface of what is possible with Docker and Docker compose but I hope all of the things I showed you about the docker CLI and Docker compose and the docker file uh are at least useful enough to start adding slowly into your app today now if you got Ed out of this video video I'd love to know it let us know how you're adopting Docker into your development workflow and if you'd love to know more so of all the things that I talked about uh what interests you the most and what do you want to see me dive deeper into let us know down in the comments because I'd love to make some videos on those things as well that's all I have for you in this one I'll see you in the next [Music] one
Thanks!
Quote it with credit and a link. Quoting Syntax