The Rails Charlotte Group previously offered a Rails class to our community.
In creating the class, we could not find any up-to-date, solid installation process for creating a new Rails App.
So, we’ve create this Rails Setup repo to help students get started with their first Rails app.
This setup uses Docker and docker-compose to build and maintain a Rails application.
The following software needs to be installed on your computer before you can start
- Docker: https://www.docker.com/products/docker-desktop
- Git: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
- Visual Studio Code: https://code.visualstudio.com
- We recommend Visual Studio Code because it comes with a built-in terminal. You don’t have to use Visual Studio Code. You are welcome to use any coding editor/terminal you are comfortable with. Just keep in mind, these instructions will be using Visual Studio Code.
You will need to create a new folder on your computer. You can place that folder in any location you want on your computer. You can also name the folder anything you want. I am going to name the folder LearnRails
Once you have created the empty folder, open the folder in Visual Studio Code.
In Visual Studio Code, click on Terminal
and New Terminal
.
In your newly opened terminal, run
git clone https://github.com/railscltgroup/rails_setup_docker_compose.git
Once you run this command, a new fold should appear called rails_setup_docker_compose
This folder will have all the Rails and Docker files needed to create your first app.
run
cd rails_setup_docker_compose
To move your terminal into the new folder
First, make sure you have Docker running on your computer. You can type docker info
in your terminal to see that it is working.
Once Docker is running, run the following command in your terminal
docker build --build-arg APP_NAME=myapp -f setup/createNewApp.Dockerfile -t web .
This command will use the file rails_setup_docker_compose/setup/createNewApp.Dockerfile
to install Rails software and build a new Rails app named myapp
. It may take awhile to install and build.
run
docker create --name temp-app web
To create a temporary container in docker.
run
docker cp temp-app:/docker-build/myapp ./
to copy the Rails application from your temporary docker container, to your computer.
A new folder should appear in your rails_setup_docker_compose
. That new folder is your Rails App. You can choose to keep is in the rails_setup_docker_compose
, or you can move it to another location on your computer.
run
docker rm temp-app
To delete the temporary container from docker
run
docker image rm web
To delete the setup image from docker.
You have created a new Rails App!
This means you are done with the setup folder. You can keep it or remove it from your computer. It’s your choice!
Next, you want to use Visual Studio Code to open the myapp
folder you just created
You’ll also need to click on Terminal
and New Terminal
to open a terminal in your app.
Feel free to checkout the files and app you just created!
You already built software to create the app. You have not yet built software to run the app.
run
docker-compose run --rm web rails db:create db:migrate
to build app software and create a database. This may take awhile to finish.
run
docker-compose up
and visit http://localhost:3000/ in your browser.
If your app is working, you should see the Rails logo on the webpage.
Congratulations! You’ve made a Rails application!
Checkout (https://railsclt.com/learn) for next steps and additional tutorials.
Docker will keep running in the background on your computer. If you don’t turn it off, it may drain your battery.
When you are done working on your app, make sure to run
docker-compose down
This will not delete your docker images, but it will shut down a lot of running processes.
When you are ready to turn it back on, run
docker-compose up
If you are done working on the app and you want to delete all docker images from your computer, run the following commands in your terminal
run
docker-compose run --rm web rails db:drop
run
docker-compose down
run
docker image rm $(docker images | grep 'web')
run
docker image rm $(docker images | grep 'ruby')
run
docker image rm $(docker images | grep 'postgres')
run
docker volume rm $(docker volume ls -qf dangling=true)