Docker + React (Typescript) + Rails6 환경 구축
9451 단어 Rails6React도커TypeScript
Summary
백엔드 Rails API & 프런트 엔드 React & Docker 환경을 실현했으므로 절차를 기록해 둡니다.
파일 구성
1. Rails 초기 설정 파일 작성
Gemfilesource 'https://rubygems.org'
gem 'rails', '=>6'
Gemfile.lock# 空
2. Rails & React 용 Docker 관련 파일 작성
Rails
DockerfileFROM ruby:2.7.2
# Rails6 からは以下のインストールが必要
# https://yarnpkg.com/lang/en/docs/install/#debian-stable
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update -qq && apt-get install -y nodejs postgresql-client vim && \
apt-get install -y yarn && \
apt-get install -y imagemagick && \
apt-get install -y libvips-tools && \
apt-get install -y locales
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
apt-get install nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
entrypoint.sh#!/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 "$@"
React
DockerfileFROM node:14.16.0-alpine3.10
WORKDIR /usr/src/app
3. docker-compose.yml 만들기
docker-compose.ymlversion: "3.9"
services:
db:
image: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
api:
build:
context: ./api/
dockerfile: Dockerfile
command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ./api:/myapp
- ./api/vendor/bundle:/myapp/vendor/bundle
environment:
TZ: Asia/Tokyo
RAILS_ENV: development
ports:
- 3000:3000
depends_on:
- db
front:
build:
context: ./front/
dockerfile: Dockerfile
volumes:
- ./front:/usr/src/app
command: sh -c "cd frontend && yarn start"
ports:
- "8000:3000"
volumes:
postgres-data:
driver: local
4. docker 명령 실행
$ docker-compose run api rails new . --force --no-deps --database=postgresql --api
$ docker-compose build
# yarn でインストール、Typescript 対応
$ docker-compose run --rm front sh -c "yarn create react-app frontend --template typescript"
5. database.yml 변경
config/database.ymldefault: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
6. docker-compose로 시작
$ docker-compose up -d
$ docker-compose run api rails db:create
참고
source 'https://rubygems.org'
gem 'rails', '=>6'
# 空
Rails
Dockerfile
FROM ruby:2.7.2
# Rails6 からは以下のインストールが必要
# https://yarnpkg.com/lang/en/docs/install/#debian-stable
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update -qq && apt-get install -y nodejs postgresql-client vim && \
apt-get install -y yarn && \
apt-get install -y imagemagick && \
apt-get install -y libvips-tools && \
apt-get install -y locales
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
apt-get install nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
entrypoint.sh
#!/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 "$@"
React
Dockerfile
FROM node:14.16.0-alpine3.10
WORKDIR /usr/src/app
3. docker-compose.yml 만들기
docker-compose.ymlversion: "3.9"
services:
db:
image: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
api:
build:
context: ./api/
dockerfile: Dockerfile
command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ./api:/myapp
- ./api/vendor/bundle:/myapp/vendor/bundle
environment:
TZ: Asia/Tokyo
RAILS_ENV: development
ports:
- 3000:3000
depends_on:
- db
front:
build:
context: ./front/
dockerfile: Dockerfile
volumes:
- ./front:/usr/src/app
command: sh -c "cd frontend && yarn start"
ports:
- "8000:3000"
volumes:
postgres-data:
driver: local
4. docker 명령 실행
$ docker-compose run api rails new . --force --no-deps --database=postgresql --api
$ docker-compose build
# yarn でインストール、Typescript 対応
$ docker-compose run --rm front sh -c "yarn create react-app frontend --template typescript"
5. database.yml 변경
config/database.ymldefault: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
6. docker-compose로 시작
$ docker-compose up -d
$ docker-compose run api rails db:create
참고
version: "3.9"
services:
db:
image: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
api:
build:
context: ./api/
dockerfile: Dockerfile
command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ./api:/myapp
- ./api/vendor/bundle:/myapp/vendor/bundle
environment:
TZ: Asia/Tokyo
RAILS_ENV: development
ports:
- 3000:3000
depends_on:
- db
front:
build:
context: ./front/
dockerfile: Dockerfile
volumes:
- ./front:/usr/src/app
command: sh -c "cd frontend && yarn start"
ports:
- "8000:3000"
volumes:
postgres-data:
driver: local
$ docker-compose run api rails new . --force --no-deps --database=postgresql --api
$ docker-compose build
# yarn でインストール、Typescript 対応
$ docker-compose run --rm front sh -c "yarn create react-app frontend --template typescript"
5. database.yml 변경
config/database.ymldefault: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
6. docker-compose로 시작
$ docker-compose up -d
$ docker-compose run api rails db:create
참고
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
$ docker-compose up -d
$ docker-compose run api rails db:create
참고
Reference
이 문제에 관하여(Docker + React (Typescript) + Rails6 환경 구축), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tomokazu0112/items/133fc383094b87571d88텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)