Rails on Docker: Create and run Rails app using Docker - Episode #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this episode we are going to create and run a rails application using docker that means I don't need to have any of the packages like Ruby RVM or databases like my sake but will be installed on my operating system but there should be a docker installed basically the docker client and the darker in jinn-demon this video is not an introduction to docker but a quick walkthrough of how one can create and run rails application using docker and compose I have doc are installed on my Mac which is docker for Mac and it is already running if you're using a different OS make sure you had the updated version of docker installed in this video we are going to be building a simple tiny notetaking application in rails it doesn't really matter what the application is but to demonstrate some of the rails commands as how you can use with docker I chose this application and I'll name this application has no type I think of this application as something like Evernote OneNote or Google keep let's get started create a project directory inside user home slash projects slash note app this will be our rails application directory which is empty right now instead of going with installing Ruby rails gem and creating rails application is indwells new command we would start with the gem file first let's open the code editor here I have sublime but you can use any other code editor or even whim create a gem file which would start with source as HTTP rubygems.org the standard one and we'll need rails gem to start with let's add rails gem version Phi dot o and also create an empty gem file dot lock which we will need to run the bundle installed within Tucker let's go back to the editor create a docker file for our rails application so in this we would define the docker image definition so the base image for the applications docker image is Ruby so let's pick the latest version from the docker hub I'll use version 2.3 dotto so on top of the base Ruby image we also need a few deb packages in order to build some of the gems that are required for rails application so let's use sudo apt-get to install those I'd like the work directory of our note app application in the docker image B slash note app create that directory and make slash note app they'll work directly so when I run note app the rails application within docker container rails root for that application would be slash note app and then add gem file to the rails root on the image which is slash note app and also gem file dot lock so the next instruction is bundle install so the first time around when we build it would just install rails gem along with its dependencies because gem file only includes instruction to install rails gem so the last instruction in the docker file is to add the current directory where we're going to run the docker commands to the application directory in the docker image which is slash note app so every time you build the image all the files in the read trees from the current directory will be added onto slash note app in the image now that we have the docker file created let's create a compose file docker - compose dot yamo we will use this to define the services involved for this application for our note app we will need two services one is the rails application itself and the other one is the database which in this case I will use my sequel so the file begins with version which is 2 and then services section start with the database service and then that service as DB I'll paste in a few lines to make it quicker so here we are using my sequel image version 5.7 from the docker hub again you can configure my sequel root password user and database names using the environment variables to learn more about my cycle image go on to docker hub and search for my sequel you a few examples and explanation of different parameters that he can configure my sequel with so back to the dr. Campos yeah Mel let's add another service for the app you can also call it web I prefer app which is our rails application unlike my sequel image we don't have an image from the registry but we need to build it off of the current directory or where the docker file is located so instead of image use build and the value is dot which is current directory where docker file is located so let's set the command to run the service which is bundle exec rails s at port number 3000 and bind it to host IP 0.0.0.0 configure the volume for the service on my development machine we need the current directory or the rails root be shared between the docker container and the host which is Mac in my case in host where I make changes to the code and in the container where it gets executed and this is a purpose in the development environment now make app service depend on the DB service so that when you run app service you have the DB service also running and also link the DB service so that app the rails application can connect to the DB using DB as the hostname the app service or rails it in the container will run at port number 3000 but I have something else running on that port on my host or the Mac so I wouldn't want it to be available on 3000 on my Mac so I'll set the port mapping or port forward as 3001 on the host to 3000 on the guest and do the same thing for DB service as well so 3307 on the host to 3306 on the guest or which is the default so that's about it for the docker compose llamó we have the DB service defined app service defined and docker file gem files look good now this looks like a lot of stuff to begin with but this is only one time so let's go back to the terminal window let's run the docker compose command to create a new rails application we're going to be using a standard rails command to generate a new rails application but using docker compose so the command is docker compose run app which is a service that we defined in the dock compose llamo and then the command which is rails new dot the current folder for database is my sequel and skip bundle this should create a rails application directory structure in the current directory boom it failed typo in the docker compose yam will file the word environment let's fix that and run it again just running it is running now trying to create the DB container first which would download the my sequel image from the docker hub setup the container create my sequel database based on the environment variables set in the docker compose file ok that is complete now by now we have the DB container with the database created and rails application directory structure is also created now we need to edit the database llamó file I'll open that up clean this up a bit and set the database name from reading from the environment variables of the app service similarly read the database host user password from the environment variables notice we don't have this configured in the docker compose yam will file for the app service yet which we will do in a bit looks clean and no hard coding and that should work for production environment as well let's go back to the docker compose file set the environment variables under the app service to set database name user password and the the database host would be DB which is linked in here on the terminal window let us run the docker compose build now this would build an image for the app this time we have an updated gem file which was updated by the rails new command so it will install all the gems for the rails application which will take a little longer image has been created all good so far let us run the rails application along with the database we don't need to run rake to create the database because the DB service has already created that for us and we don't have any migrations to run yet before we create any migrations or model let's do a docker compose up to start both the services and see what happens notice both my sequel and rails are running rails is running at port number 3000 in the container and that is not the port 3000 on my host OS X or Mac so I cannot access 3000 on browser as per our docker compose it should be available on three thousand one so go to localhost three thousand one on my browser there we have rails running cool let's add some controller model views to test some of the commands and to ensure the rails application is actually running with database connected for this note app the key resource or the model will be note so I'm gonna create a scaffold for node using rails generator again using docker compose run app service and the command is rails generate scaffolds node and the model will have two columns or fields a string title and text body go now we have a scaffold generated let's run the migration is in compose again we don't need to restart the service but access port number 3000 one slash notes there we go I can create a node list and view now standard rails scaffold all from the rails running in a container along with the DB container this tester few more commands using docker compose docker compose PS to check the status of the services besides that you can use docker compose run app to run pretty much all the race commands such as for Migration c generators and so on only extra thing that you end up typing is the docker compose run app every time you need to run anything on rails app before we wrap this up i wanted to make this application UI look a bit pretty offline i updated the stylesheet and layout of the node app using a sample bootstrap template let's reload the page and here we have the updated UI only the UI changed nothing on the controller or model that's it for this episode thanks for watching
Info
Channel: Chandra Shettigar
Views: 27,458
Rating: undefined out of 5
Keywords: RailsOnDocker, Docker, Dockerize Rails, Rails on Docker, rails, compose, mysql, dockerize, container, service
Id: a-jcTib9ZPA
Channel Id: undefined
Length: 11min 12sec (672 seconds)
Published: Sat Oct 01 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.