I will introduce how to build a Rail 6 environment with Docker. Webpack was introduced from Rails6, and the procedure is a little different from Rails5, so I also considered that.
Docker official page Quickstart: Compose and Rails
Docker3 Rails6.0.3 Ruby2.7.1 DB:PostgeSQL
This time, we will build the environment in a folder named "myapp". If you want to change the name, please change "myapp" accordingly.
The file structure to be created from now on is as follows.
file organization
myapp
--Dockerfile
--Gemfile
--Gemfile.lock
--entrypoint.sh
--docker-compose.yml
First, create a folder.
Terminal
$ mkdir myapp
$ cd myapp
1.1 Dockerfile
Terminal
$ touch Dockerfile
Dockerfile
FROM ruby:2.7.1
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
##nodejs and yarn are required when installing webpack
#Install yarn package management tool
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
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 && apt-get install -y yarn
# Node.install js
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
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
1.2 Gemfile
Terminal
$ touch Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>6'
1.3 Gemfile.lock
Terminal
$ touch Gemfile.lock
Nothing is listed here.
1.4 entrypoint.sh
Terminal
$ touch entrypoint.sh
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 "$@"
1.5 docker-compose.yml
Terminal
$ touch docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
#######add to#########################
environment:
POSTGRES_HOST_AUTH_METHOD: 'trust'
###################################
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
2.1 rails new
Terminal
$ docker-compose run web rails new . --force --no-deps --database=postgresql --skip-bundle
About command options
--force
: Overwrite the file even if it exists
--no-deps
: Do not start the linked service
--database = postgresql
: DB specifies PostgreSQL
--skip-bundle
: skip the bundle install (after creating the app)
2.2 docker-compose build Now that a new Gemfile has been created, use the following command to bundle install.
Terminal
$ docker-compose build
config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
#####add to#####
host: db
username: postgres
passowrd:
#############
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Terminal
$ docker-compose up
It is OK if the container starts and connects to the server. I haven't created the DB yet, so I get the following error.
Since I was able to connect to the server, I created a DB. After disconnecting with Ctrl + C
Terminal
#Stop Docker once
$ docker-compose stop
$ docker-compose down
#DB creation
$ docker-compose run web rake db:create
#Launch Docker again
$ docker-compose up
If you connect to [http: // localhost: 3000 /](http: // localhost: 3000 /), you can confirm that it has started successfully. The version is Rails6, Ruby2.7.1 as set.
You need to use Docker commands when developing within Docker.
$ docker-compose run web bundle exec rails command
Example
$ docker-compose run web bundle exec rails g model User
$ docker-compose run web bundle exec rails g controller Homepages index
$ docker-compose run web bundle exec rails g rspec:install
Docker official page Quickstart: Compose and Rails Steps for building a Ruby on Rails environment with Docker [Rails 6 compatible] I was addicted to building a Rails 6 development environment with Docker Postgres Docker Image has changed and can no longer be started [Rails] ActiveRecord::NoDatabaseError [Docker]
Recommended Posts