To follow this tutorial, the only thing you need is to have Docker installed in your computer. Previous knowledge about Docker or Rails isn't necessary. This a newbie to newbie documentation so you can use it "by the book" to start your studies or if you want to run a rails application using docker for the first time.
Run this code line in your computer bash. It will run a new container with Ruby image
docker run -it -v $HOME:/var/home -w /var/home ruby:3.0.2 bash
Installing Ruby package manager (bundler). Run this code line in your computer bash.
gem install bundler
Installing Rails. Run this code line in your computer bash.
gem install rails
Creating your app. Run this code line in your computer bash. The command --database=postgresql is only necessary if you want to work with postgre. By default, your application will be created using SQLite.
rails new dummy --database=postgresql
Installing node. Run this code line in your computer bash.
curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
echo 'deb https://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list &&\
wget -q -O - https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - &&\
apt-get update -qq && \
apt-get install -y build-essential nodejs yarn
Creating a Dockerfile (it will contain the instructions to create your app image inside a container). Go inside your application and, at the global directory, create a document with the name 'Dockerfile'.
FROM ruby:3.0.2
RUN gem install bundler
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
echo 'deb https://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list &&\
wget -q -O - https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - &&\
apt-get update -qq && \
apt-get install -y build-essential nodejs yarn
WORKDIR /var/app
COPY . .
RUN bundler install
ENTRYPOINT ["./entrypoint.sh"]
Creating an entrypoint (it will contain some commands that your application will run by default, if you dont specify any other command). Create the 'entrypoint.sh' inside your app's global directory.
#!/bin/bash
rm -f /var/app/tmp/pids/server.pid
bundle check > /dev/null 2>&1 || bundle install -j4
echo "> Executing 'yarn install' on web"
yarn install
if [ "$#" == 0 ]
then
bundle exec rake db:create db:migrate
exec bundle exec rails server -b '0.0.0.0'
fi
exec $@
Adding permission to your app execute it's entrypoint. Insert this command line in your bash, inside your app's directory.
chmod +x entrypoint.sh
Building your application. In other words, executing your Dockerfile. Insert this command line in your bash, inside your app's directory.
docker build -t myapp .
Starting your container. Insert this command line in your bash, inside your app's directory.
docker run -it myapp bash
Executing your container (remember, you started it before in the previous step). Insert this command line in your bash, inside your app's directory.
Get your container id, using this command line
docker ps
then execute it
docker exec -it "id do container" bash
Docker compose is a way to compose your application with diferent containers("services").
Inside your application's global directory, create a document with the name 'docker-compose.yml' with the following code block. If you didn't specify the postgre database, paste it without the 'db' code block.
version: '2'
services:
web:
build:
context: .
volumes:
- .:/var/app
ports:
- '3000:3000'
db:
image: postgres:9.6.2-alpine
ports:
- '5432:5432'
volumes:
- postgres96:/var/lib/postgresql/data
volumes:
postgres96:
Insert this command line in your bash, inside your app's directory.
sudo apt install docker-compose
Building the docker-compose you have just created. Insert this command line in your bash, inside your app's directory.
docker-compose up
If you get a webpack error, this will solve it. Insert this command line in your bash, inside your app's directory.
Open your container bash
docker-compose run web bash
then install the webpacker
bundle exec rake webpacker:install
If you get a postgre error, this will solve it
- Search the document config/database.yml
- Inside it, search for the following code block:
development:
...
"database: database name"
then change the "database" line to:
url: postgres://postgres:@db:5432/dummy_development
Now search for the following code block:
test:
...
"database: database name"
then change the "database" line to:
test:
...
url: postgres://postgres:@db:5432/dummy_test
After finishing these changes, execute the command
docker-compose run web bash
A Makefile is a way to create shortcuts as commands to handle your application's usability.
Go inside your application and, at the global directory, create a document with the name 'Makefile'. Inside it, insert the following code block:
start:
docker-compose up
bash:
docker-compose exec web bash
console:
docker-compose exec web bundle exec rails c
# specs:
# docker-compose exec web bundle exec rspec spec
Now, if you want to start your application, access it's bash or console, you can use:
make start
make bash
make console