Tạo project ruby on rails mysql với docker

Lượt xem: 9136

Tạo project ruby on rails mysql với docker

1. Điều kiện cần

Trước tiên máy các bạn cần phải cài đặt docker và docker-compose. Các bạn kiểm tra bằng lệnh sau
 


docker --version
docker-compose --version

2. Chuẩn bị các file cần thiết (Tạo folder rails_docker_app để chứa các file sau)

- Tạo Dockerfile như sau:


FROM ruby:3.1.2
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /rails_docker_app
WORKDIR /rails_docker_app
ADD Gemfile /rails_docker_app/Gemfile
ADD Gemfile.lock /rails_docker_app/Gemfile.lock
RUN bundle install
ADD . /rails_docker_appdock

- Tạo Gemfile với nội dụng sau:


source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end
ruby '3.1.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails'
# Use mysql as the database for Active Record
gem 'mysql2'
# Use Puma as the app server
gem 'puma'
# Use SCSS for stylesheets
gem 'sass-rails'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'bootstrap', '~> 5.2.2'

gem 'sprockets-rails', '~> 3.4.2'


- Tạo Gemfile.lock

- Tạo docker-compose.yml với nội dung sau
 


version: "3"

services: 
  app:
    build: .
    command: bundle exec rails s -p 3000 -b "0.0.0.0"    
    volumes:
      - .:/rails_docker_app
      - bundle:/usr/local/bundle
    ports:
      - "3000:3000"
    depends_on:
      - db
    stdin_open: true
    tty: true
    environment:
      RAILS_ENV: development
      DB_USERNAME: root
      DB_PASSWORD: ''
      DB_HOST: db
  db:
    image: mysql:5.5
    ports:
      - "4306:3306"
    volumes:
      - db_data:/var/lib/mysql
      - .:/rails_docker_app
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_USER: root
      MYSQL_PASSWORD: ''
volumes:
  db_data:
    driver: local
  bundle:
    driver: local


3. Build và chạy project ruby on rails với database mysql

 - Tạo mới project ruby on rails với mysql thực hiện chạy lệch sau:
 


docker-compose run app rails new . --force --database=mysql --skip-bundle

- Cập nhật lại config trong file database.yml sang ENV. Cụ thể:


default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  host: localhost

Thành


database: <%= ENV['DB_NAME'] %> 
  username: <%= ENV['DB_USER'] %> 
  password: <%= ENV['DB_PASSWORD'] %> 
  host: <%= ENV['DB_HOST'] %> 

- Chạy docker-compose build


docker-compose build

- Tiếp tục chạy docker-compose up -d  

docker-compose up -d  

Lưu ý: Nếu lỗi thì các bạn cần chạy lệnh sau để create database nhé


docker-compose run --rm app rails db:create

Xem video cùng dandev thao tác nhé