Chào các bạn. Lại là mình, Quân
Ở Rails 6 có sự thay đổi rõ rệt nhằm hỗ trợ về tích hợp framework Javascript trong dự án bằng Rails. Thư mục javascript được đưa ra bên ngoài assets và quản lý hoàn toàn bằng webpack. Vì vậy, chúng ta phải thay đổi 1 chút để cài đặt project
Thiết lập project
Chúng ta sẽ viết Dockerfile như sau:
FROM ruby:2.7
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client npm\
  && rm -rf /var/lib/apt/lists/* \
  && curl -o- -L https://yarnpkg.com/install.sh | bash
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
# install yarn
RUN npm install -g yarn
RUN yarn install --check-files
COPY . /myapp
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]Tiếp đó, tạo 1 Gemfile chỉ load Rails. File này sau đó sẽ bị ghi đè khi chạy rails new
source 'https://rubygems.org'
gem 'rails', '~>6'Tạo 1 cái Gemfile.lock rỗng
touch Gemfile.lockTạo entrypoint.sh gồm
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"Cuối cùng ở bước này sẽ là tạo docker-compose.yml
version: '3'
services:
  db:
    image: postgres:11
    environment:
      POSTGRES_HOST_AUTH_METHOD: 'trust'
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - dbKhởi tạo project
docker-compose run web rails new . --force --no-deps --database=postgresql --webpackVà chúng ta đã có 1 app hoàn toàn mới
Chạy lệnh để xem các file
$ ls -l
total 736
-rw-r--r--    1 quanhoang  staff     605 Sep 28 11:19 Dockerfile
-rw-r--r--    1 quanhoang  staff    1971 Sep 28 15:11 Gemfile
-rw-r--r--    1 quanhoang  staff    5537 Sep 28 15:11 Gemfile.lock
-rw-r--r--    1 quanhoang  staff     374 Sep 28 15:11 README.md
-rw-r--r--    1 quanhoang  staff     227 Sep 28 15:11 Rakefile
drwxr-xr-x   11 quanhoang  staff     352 Sep 28 15:11 app
-rw-r--r--    1 quanhoang  staff    1876 Sep 28 15:11 babel.config.js
drwxr-xr-x   10 quanhoang  staff     320 Sep 28 15:11 bin
drwxr-xr-x   18 quanhoang  staff     576 Sep 28 15:11 config
-rw-r--r--    1 quanhoang  staff     130 Sep 28 15:11 config.ru
drwxr-xr-x    3 quanhoang  staff      96 Sep 28 15:11 db
-rw-r--r--    1 quanhoang  staff     309 Sep 28 15:14 docker-compose.yml
-rw-r--r--    1 quanhoang  staff     202 Sep 27 15:32 entrypoint.sh
drwxr-xr-x    4 quanhoang  staff     128 Sep 28 15:11 lib
drwxr-xr-x    4 quanhoang  staff     128 Sep 28 15:11 log
drwxr-xr-x  762 quanhoang  staff   24384 Sep 28 15:14 node_modules
-rw-r--r--    1 quanhoang  staff     332 Sep 28 15:14 package.json
-rw-r--r--    1 quanhoang  staff     224 Sep 28 15:11 postcss.config.js
drwxr-xr-x    9 quanhoang  staff     288 Sep 28 15:11 public
drwxr-xr-x    3 quanhoang  staff      96 Sep 28 15:11 storage
drwxr-xr-x   12 quanhoang  staff     384 Sep 28 15:11 test
drwxr-xr-x    7 quanhoang  staff     224 Sep 28 15:11 tmp
drwxr-xr-x    3 quanhoang  staff      96 Sep 28 15:11 vendor
-rw-r--r--    1 quanhoang  staff  310152 Sep 28 15:14 yarn.lockVới trường hợp chạy Docker ở Linux, các file từ lệnh rails new sẽ đặt quyền root. Ta sẽ sửa lại
sudo chown -R $USER:$USER .Cuối cùng ta sẽ build lại
 docker-compose buildKết nối tới cơ sở dữ liệu
Sửa lại config/database.yml như sau:
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5
development:
  <<: *default
  database: myapp_development
test:
  <<: *default
  database: myapp_testSau đó chạy
$ docker-compose upKết quả như sau là ok
myapp_db_1 is up-to-date
Creating myapp_web_1 ... done
Attaching to myapp_db_1, myapp_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.Sau đó chạy lệnh tạo database:
docker-compose run web rails db:createLúc này, truy cập http://localhost:3000 và chào mừng bạn tới với Rails 6
 
Kết
Để tắt ứng dụng, Ctrl+C ở cửa sổ đang chạy docker-compose up và chạy docker-compose down
Khi có thay đổi ở Dockerfile, docker-compose.yml và Gemfile, chạy docker-compose up --build
Bài viết có sự tham khảo lại từ https://docs.docker.com/compose/rails/ và chỉnh sửa lại cho phù hợp với Rails 6



 
	        	        		 
	        	        		 
	        	        		 
	        	        		 
	        	        		 
	        	        		 
	        	        		
Leave a Reply