How to build an application in Ruby on Rails using Docker-compose by referring to books, videos, and Qiita articles I had a hard time, so I tried various things and finally got it to work, so I will leave the final file and execution procedure.
Dockerfile
docker-compose.yml
Gemfile
Gemfile.lock
#Ruby in image name(Ver2.6.5)Specify the image of the execution environment of
FROM ruby:2.6.5
#Update the list of packages and install the packages required to build the rails environment
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
#Create a directory for your project
RUN mkdir /myapp
#Set to working directory
WORKDIR /myapp
#Copy to project directory
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
#Execute bundle install
RUN bundle install
#Copy all the contents of the build context to myapp
COPY . /myapp
docker-compose.yml
version: '3'
services:
db:
#Get image of postgres
image: postgres
environment:
POSTGRES_USER: 'postgresql'
POSTGRES_PASSWORD: 'postgresql-pass'
restart: always
volumes:
- pgdatavol:/var/lib/postgresql/data
web:
#Build and use images from Dockerfile
build: .
#Executed when container starts
command: bundle exec rails s -p 3000 -b '0.0.0.0'
#Current directory/Bind mount to myapp
volumes:
- .:/myapp
#Publish at 3000 and transfer to 3000 in container
ports:
- "3000:3000"
#Start the db service before starting the web service
depends_on:
- db
#Create pgdatabol volume for data persistence and mount postgresql data area
volumes:
pgdatavol:
source 'https://rubygems.org'
gem 'rails', '5.2.4.2'
Gemfile.lock
docker-compose run web rails new . --force --database=postgresql
database.yml
default: &default
adapter: postgresql
encoding: unicode
# --------add to--------
host: db
username: postgresql
password: postgresql-pass
# --------So far--------
docker-compose up -d
docker-compose build --no-cache
docker-compose run web rails db:create
docker-compose run web bin/rails g scaffold User name:string
docker-compose run web bin/rails db:migrate
http://localhost:3000/users
[I've just started Docker, so I've summarized it in an easy-to-understand manner](https://qiita.com/gold-kou/items/44860fbda1a34a001fc1#%E3%83%9B%E3%82%B9%E3%83%88% E5% 9E% 8B% E4% BB% AE% E6% 83% B3% E5% 8C% 96)
How to delete Docker image and container
[Creating a Docker container, starting and stopping](https://qiita.com/kooohei/items/0e788a2ce8c30f9dba53#5%E3%82%B3%E3%83%B3%E3%83%86%E3%83% 8A% E3% 81% AE% E5% 81% 9C% E6% AD% A2)
Solution for Docker not bundle install in container
Recommended Posts