Skip to content

Commit 48ee34a

Browse files
author
Nguyen Van A
committed
Add docker
1 parent 12182eb commit 48ee34a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

Dockerfile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
FROM ruby:3.2.2
2+
3+
# Set timezone để tránh lỗi timezone data source
4+
ENV TZ=Asia/Ho_Chi_Minh
5+
6+
# Cài đặt các package cần thiết bao gồm tzdata
7+
RUN apt-get update -qq && apt-get install -y \
8+
build-essential \
9+
libpq-dev \
10+
nodejs \
11+
default-mysql-client \
12+
yarn \
13+
tzdata \
14+
dos2unix \
15+
&& apt-get clean \
16+
&& rm -rf /var/lib/apt/lists/*
17+
18+
# Set working directory
19+
WORKDIR /app
20+
21+
# Copy Gemfile trước để tận dụng Docker layer caching
22+
COPY Gemfile Gemfile.lock ./
23+
24+
# Install gems
25+
RUN bundle install
26+
27+
# Copy toàn bộ source code
28+
COPY . .
29+
30+
# Fix line endings cho tất cả các file quan trọng
31+
RUN find . -type f \( -name "*.rb" -o -name "*.yml" -o -name "*.yaml" -o -name "*.sh" \) -exec dos2unix {} \; && \
32+
find ./bin -type f -exec dos2unix {} \; && \
33+
find ./bin -type f -exec chmod +x {} \; && \
34+
find . -name "rails" -type f -exec dos2unix {} \; && \
35+
find . -name "rake" -type f -exec dos2unix {} \; && \
36+
find . -name "bundle" -type f -exec dos2unix {} \;
37+
38+
# Fix quyền thực thi cho bin directory
39+
RUN chmod -R +x ./bin
40+
41+
# Expose port
42+
EXPOSE 3000
43+
44+
# Default command với đường dẫn đầy đủ
45+
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000"]

config/database.yml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ default: &default
1313
adapter: mysql2
1414
encoding: utf8mb4
1515
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
16+
# host: db # if use docker
1617
username: root
1718
password:
1819
socket: /var/run/mysqld/mysqld.sock

docker-compose.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version: '3.8'
2+
3+
services:
4+
db:
5+
image: mysql:8.0
6+
restart: always
7+
environment:
8+
MYSQL_ROOT_PASSWORD: password
9+
MYSQL_DATABASE: rails_tutorial_development
10+
MYSQL_CHARSET: utf8mb4
11+
MYSQL_COLLATION: utf8mb4_unicode_ci
12+
ports:
13+
- "3306:3306"
14+
volumes:
15+
- db_data:/var/lib/mysql
16+
command: --default-authentication-plugin=mysql_native_password
17+
18+
web:
19+
build: .
20+
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails server -b 0.0.0.0 -p 3000"
21+
volumes:
22+
- .:/app
23+
ports:
24+
- "3000:3000"
25+
depends_on:
26+
- db
27+
environment:
28+
- RAILS_ENV=development
29+
stdin_open: true
30+
tty: true
31+
32+
volumes:
33+
db_data:

0 commit comments

Comments
 (0)