-#Build context
% mkdir product-register
% cd product-register
-#Gemfile and Gemfile.Creating a lock
% touch Gemfile Gemfile.lock
-#Editing Gemfile
% vim Gemfile
product-register/Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5.2'
-#Editing the Dockerfile
% vim Dockerfile
product-register/Dockerfile
FROM ruby:2.5
RUN apt-get update && apt-get install -y && \
buile-essential \
libpq-dev \
nodejs \
postgresql-client \
yarn
WORKDIR /product-register
COPY Gemfile Gemfile.lock /product-register/
RUN bundle install
-#Creating a container
% docker build .
-# docker-compose.edit yml
% vim docker-compose.yml
docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- '3000:3000'
volumes:
- '.:/product-register'
tty: true
stdin_open: true
-#Container launch
% docker-compose up -d
-#When working inside a container
% docker-compose exec web bash
-#Since application creation, DB setting, and bundle are done with Dockerfile — skip
# rails new . —force —database=postgresql —skip-bundle
-#Confirm that the contents of Gemfile have changed on the host side
% cat Gemfile
-#I need to bundle install, so stop once
# exit
$ docker-compose down
-# docker-If you do compose up, the previous image will be used, so build again
$ docker-compose up —build -d
% docker-compose exec web bash
-#Start rails server
$ rails s -b 0.0.0.0
I can connect with localhost: 3000, but I get an error page because I have not set the DB.
config/database.yml
default: &default
adapter: postgresql
encoding: unicode
host: db
user: postgres
port: 5432
password: <%= ENV.fetch("DATABASE_PASSWORD") %>
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
Since it is not desirable to write Password directly for security reasons, call it with an environment variable.
docker-compose.yml
version: '3'
#docker volume Data is saved here
volumes:
db-data:
services:
web:
build: .
ports:
- '3000:3000'
volumes:
- '.:/product-register'
#Environment variable settings of the container, originally the password should not be written directly.
environment:
- 'DATABASE_PASSWORD=postgres'
tty: true
stdin_open: true
#Created once the db service is created
depends_on:
- db
#You can access db from the web
links:
- db
#postgres container
db:
image: postgres
#Host db-Store data in data
volumes:
- 'db-data:/var/lib/postgresql/data'
environment:
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
% docker-compose up -d
-#You can see that the two services are running
Creating product-register_db_1 ... done
Recreating product-register_web_1 ... done
% docker-compose exec web bash
-#DB creation
# rails db:create
Created database 'product-register_development'
Created database 'product-register_test'
You can see that the database has been created. From here you can create Rails apps.
-#Make a simple app with scaffold
# rails g scaffold product name:string price:integer vender:string
-#Define the created table
# rails db:migrate
-#Start the server
# rails s -b 0.0.0.0
You can connect with localhost: 3000 and you can see that the rails page is displayed. You can connect to apps made with localhost: 3000 / products.
Recommended Posts