How I used Docker with Rails

What is docker actually?

What is the use of containers?

Download and Install Docker

/home/admat/Workspace/Rahasak/labs/rahasak-octopus/registrar-web

Creating the Dockerfile

touch Dockerfile
nano Dockerfile
touch Gemfile
nano Gemfile
source 'https://rubygems.org'ruby '2.6.0'
gem 'rails', '~> 5.2.2'
touch Gemfile.lock

Entrypoint

Generate the Rails Project now

docker run --name rails new . --force --no-deps --database=mysql
atom .
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: <%= ENV['DB_NAME'] %>
username: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: <%= ENV['DB_HOST'] %>
development:
<<: *default
test:
<<: *default
production:
<<: *default
# Rails Application
DB_NAME=octopus_app_db
DB_USER=root
DB_PASSWORD=password
DB_HOST=db
# MySQL
MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=octopus_app_db
MYSQL_USER=appuser
MYSQL_PASSWORD=password
docker pull mysql:5.7
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7

Build the Image

docker build -t admat/registrar-web .
docker images
Here is my image

RUN the rails application

docker run -e DB_NAME=registrar_app_db \                                                                                    
-e DB_USER=root \
-e DB_PASSWORD=password \
-e DB_HOST=172.21.0.2 \
-p 3001:3000 admat/registrar-web

Next..

Creating the Docker compose file

build: .
image: admat/registrar-web

Build Docker compose file

docker-compose build

Run the Docker compose

docker-compose up
docker-compose up -d [service-name]
docker ps -a
docker ps
docker exec -it [container-name] bash
rails c
rake commands
etc..

finally

docker login
docker push admat/registar-web:latest

Senior Software Engineer ❤️ Node | Ruby