I feel like I'm checking it every time I create an app, so I've summarized it as a memorandum.
Please enter the app name in the app name
section below.
First, create a directory that will be the base of your app. In addition, use the touch command to create two empty files.
$mkdir app name&&cd app name
$ touch Gemfile Gemfile.lock
Because I'm developing using VScode It is started using the code command. By the way, the code command will start and create for you.
Edit the Gemfile.
$ code Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5.2'
Create and edit a Dockerfile.
$ code Dockerfile
Dockerfile
FROM ruby:2.5
RUN apt-get update
RUN apt-get install -y \
build-essential \
libpq-dev \
nodejs \
postgresql-client \
yarn \
vim
WORKDIR /app name
COPY Gemfile Gemfile.lock /app name/
RUN bundle install
Create and edit the docker-compose.yml file.
$ code docker-compose.yml
docker-compose.yml
version: "3"
volumes:
db-data:
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- ".:/app name"
environment:
- "DATABASE_PASSWORD=postgres"
tty: true
stdin_open: true
depends_on:
- db
links:
- db
db:
image: postgres
volumes:
- "db-data:/var/lib/postgresql/data"
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
- "POSTGRES_USER=postgres"
- "POSTGRES_PASSWORD=postgres"
Start the container, enter the web container, and rails new.
$ docker-compose up --build -d
$ docker-compose exec web bash
$ rails new . --force --database=postgresql
Add to the database.yml file created by rails new.
database.yml
default: &default
adapter: postgresql
encoding: unicode
host: db #Postscript
user: postgres #Postscript
port: 5432 #Postscript
password: <%= ENV.fetch("DATABASE_PASSWORD") %> #Postscript
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
$ rails db:migrate
$ rails s -b 0.0.0.0
If you access Chrome's search bar by typing localhost: 3000, you'll see hello world!
Recommended Posts