Docker Basics for Ruby on Rails Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys this episode we're going to be talking about the basics of docker and docker compose to run your applications inside of containers so what is a container well a container is kind of its own little operating system image that has linux and whatever dependencies you need installed on top of that and your application code so this can be contained in a single file which you can then give to someone else they can run your code with all the dependencies and they don't have to do anything other than install and run your docker image and that's it so this is really useful in development so you can run the same code on any operating system no matter what tools are installed and the same thing goes for production so in production you're going to want to maybe add 10 servers well you can just download the docker image and run 10 copies of that and you have 10 instances of that and there's nothing fancy that you need to set up so that's really really handy but it does push a lot of the devops work onto you as the developer who has to maintain the docker file and any other configuration stuff so let's dive into building our first docker file we are going to go to an application we've worked on before the device hotwired example and we're going to add a docker file to the root of that directory so this docker file is where we define all of the things we need to do inside of the docker image to set it up and run our code so let's dive into this first things first you're going to generally have a from line which is going to say hey grab another docker image is the foundation that we're going to build on top of so we're going to use ruby as the docker image and we're going to use 3.0 dash alpine to do that so this is going to give us a base alpine linux image which is a lightweight distribution that you can have and it's going to have a ruby 3.0 pre-installed for us now we need to then run a command to install some dependencies now i'm going to paste this in and we're going to install the postgres client and yarn and node.js and a few other things like image magic and get so that we have all of those dependencies that we need now this is something that you will definitely want to tweak you may not need all of these things you also might need some additional things and so you're going to have to go and modify the dependencies there this last line is going to say hey once you've installed all those go delete those packages that we just downloaded like the zip files of those basically we don't need those install files because in our image we're going to have all the stuff installed so those packages we can get rid of and save up some space in our image so once we've installed all of our dependencies you can run any other configuration things you might need to do in there but we then need to tell docker that we want to work inside of slash app or some folder inside of that image so think of this as an operating system we're going to create a directory called slash app and the root and put all of our code in there and to do that we're going to use workdrawer to create the slash app and then we're going to copy our current directory on our machine so on my mac os install we're going to take the current directory and then put all that inside of the image and slash app then we can do a few other things we're going to use bundle path and override that so that we install our gems to the slash gems folder and then we're going to run yarn install and run bundle install so those will install all of the yarn dependencies and bundle as well and then we can define our entry point and this is going to define what command is run when our docker image is started so we'll say bin rails and then we can add any options we want in there so we will say the server dash b to bind to zero zero zero and then the last thing we need to do is expose port 3000 so rails normally runs on port 3000 we're leaving that as the defaults and we're going to have our docker image say hey you can access port 3000 and access this app on there so that's all we really need to do to set up our docker file and now we need to go build it so in our terminal we can run docker build dash dash tag my app or whatever your app name is and then you can give it the period which will say look for the docker file in this folder so if we run that it's going to download the ruby 3.0 alpine image it's then going to run our dependency command here to install all of those then set up our working directory copy our code over and then run our yarn install and our bundle install in order to get all those dependencies ready to go and it will create this image at the end that we can just run and we will never have to replace these steps unless we need to build a new copy of our image so we can run this over and over again and we won't have to go through any of those steps so we'll give it some time to finish creating our image so now that our image is built correctly we can go and run docker run my app which is what we named or tag for our image and this is going to start up our docker image and start our rail server which we set as the entry point in that docker file and here you can see it booted up puma we can try opening up localhost 3000 in our browser but it's not going to work and that's because we need to tell docker to actually take port 3000 from the image and actually expose it on our machine as port 3000 as well so we can shut this down with control c and we'll add dash p 3000 colon 3000 which is basically saying take docker's port 3000 and map it to your operating systems port 3000 and now if we do this we'll be able to access our rails app now it's not able to see our postgres server inside of mac os 10 because this is the image and it has its own separate networking going on that it cannot access our postgres server so what do we do well we can use docker compose to actually spin up our postgres server our reda server and our application image all at once so let's go and define a docker compose.yaml file so we'll say docker compose.yaml and inside of here we're going to save version and we can use the latest version or whatever version you want as long as it's 3.x that is the latest then you can define your services that you want to run so this can be things like your rails app your postgres server and your redis server so we'll do one for our database we are going to have the image which will be the postgres latest image we are going to have an environment variable here we're going to define postgres password equals password that's a required environment variable for the postgres image and then we're going to set up the port so that 5432 is mapped to 5432 on our operating system and our volumes here so we can persist the data we're going to have db data slash r slash lab postgresql data which is where the postgres data lives then we're going to have a redis image which will come from the redis latest image and ports 63796379 which is the default and you could also add a volume for this if you wanted to persist that but it's redis data and we're not really using it for anything but you know caching and action cable stuff so we don't need to store that data anywhere so then we can have our web a rails app and this one is going to be instead of an image we're going to build the current directory which is our docker file and that will run the same docker build command for us automatically and then we can set our po ports to 3000 3000 so basically is adding that option for command line stuff that we don't have to specify every time and then we can say this depends on our db and our redis servers and then for this one we're going to have a couple environment variables and this will be a database url equal to postgres postgres user is the default we define the password up here on line six so we'll have password and our host is going to be db so when we define the name here for the image the networking inside of docker is going to be able to know hey we want to talk to that image so we can just specify that by name and our port and then we can have the postgres database name and i just want to point out here that this is all the defaults from the postgres docker image and you can take a look at their readme but basically they have the postgres password which is a required environment variable the rest are optional so we're just using the default database name default username and so on and that's how we got to our url here now to fix a quick typo up here environment should be spelled right and then we can add our redis url to point to our redis image with the redis protocol so we'll say redis 63 79 which is the default port and if you wanted to use like port 5000 you can add that here and that would set up your port but we're going to use the default for rails and then last but not least we want volumes where the current directory will be copied to slash app and then we can add volumes db data which we specified here in our volumes for postgres so basically this file is very similar to what you would write if you're building github actions or circle ci or travis ci images so you have your rails app image you have your databases and all of those can talk to each other and get sped up and torn down without a problem so that can run it for your ci delete it and move on but this is kind of the same thing but you can use it for development or deploying to production so once this is all done docker compose has a very simple command docker compose up and it's going to go download all of your images make sure they're all running because we have the depends on it's going to make sure db and redis are running before it starts the web one and that is about it so it is going to take care of everything for us and once that's running we'll have an entire environment with our rails app and our databases that we can use in development so we'll let this run and then we'll be able to open up localhost and try it out so now that our docker compose is all up and running we can refresh our page for a rails app and we're gonna see that it wants us to run our migrations which we can do and that's going to update everything in the database and once that is done we'll be able to use our rails application and that will be perfect so what's cool about this is that we can also go and shut down everything because we have the volume setup we can restart docker compose it's going to restart all of our images run them and it's going to persist that database data so next time we refresh after restarting everything we don't have a blank database we've actually persisted all of that so all of our data is still there and it has not disappeared and that's because we are editing files in a image that's running in memory so if we shut it down and restart it it's going to use the image that we built previously and that's not going to have any of those files that we might have uploaded or added to our image that's the reason why heroku doesn't allow you to upload images to the containers in your application's file system because they're going to get blown away whenever your app restarts so you need some external persistent storage like amazon s3 so that is the basics of docker you just define all these commands to configure your linux image with all your dependencies and then you build the image and you can run it along with other images using docker compose so if you've ever set up ci this is going to be very very similar or familiar to you and that is the basics of running docker and docker compose
Info
Channel: GoRails
Views: 7,472
Rating: undefined out of 5
Keywords: Ruby, Ruby on Rails, Rails, Javascript, HTML, CSS, Servers, Databases, Screencast, Tutorial, Rails 6, Action Text, Active Support, Action Mailbox, Webpacker, Active Storage, Active Record, rails testing, ruby on rails testing, ruby testing, devise, rails devise
Id: J7hUHnQtFNo
Channel Id: undefined
Length: 13min 27sec (807 seconds)
Published: Mon Mar 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.