Build a Rails 6 development environment using Docker.
The goal is to do everything on Docker, from rails new
to rails db: create
.
The actual code is uploaded to here.
$ sw_vers
ProductName: macOS
ProductVersion: 11.1
BuildVersion: 20C69
$ docker -v
Docker version 20.10.0, build 7287ab3
$ docker-compose -v
docker-compose version 1.27.4, build 40524192
First, create a file under the root of the project as shown below.
$ tree
.
├── Dockerfile
├── Gemfile
├── Gemfile.lock
└── docker-compose.yml
0 directories, 4 files
Dockerfile
FROM ruby:2.7.2
RUN apt-get update && apt-get install -y --no-install-recommends build-essential libpq-dev nodejs \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN gem install bundler:2.0.2 && bundle install
COPY . /myapp
docker-compose.yml
docker-compose.yml
version: "3"
services:
#Container for Elasticsearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.10.1
environment:
- discovery.type=single-node
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
ports:
- 9200:9200
volumes:
- esdata:/usr/share/elasticsearch/data
#Container for Kibana
kibana:
#Match the version numbers of elasticsearch and kibana images
image: docker.elastic.co/kibana/kibana:7.10.1
ports:
- 5601:5601
depends_on:
- elasticsearch
#Container for Postgres
db:
image: postgres
ports:
- 5432:5432
volumes:
- pgdata:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
#Container for Rails
rails:
build: .
command: bundle exec rails server -p 3000 -b '0.0.0.0'
depends_on:
- db
- elasticsearch
ports:
- 3000:3000
environment:
DATABASE_HOST: db
tty: true
stdin_open: true
volumes:
#Abandon consistency with delegated and focus on performance
- .:/myapp:delegated
#Volume used by elasticsearch and db
volumes:
esdata:
pgdata:
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.2'
gem 'rails', '~> 6.1.0'
Gemfile.lock
Gemfile.lock
#Sky
rails new
docker-compose run --rm rails rails new . --force --database=postgresql --skip-test --api
Optional points
----rm
Since it is a one-time use of the container, it is discarded after execution.
--rails new .
Create a Rails application in the current directory
---- database = postgresql
Specify Postgres for the database
Modify it to the value set as the environment variable in dockr-compose.yml
.
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
#Add user and password
username: <%= ENV.fetch('POSTGRES_USER') { 'postgres' } %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') { 'password' } %>
development:
<<: *default
database: myapp_development
#DATABASE set in rails container_Set HOST as host
host: &development_host <%= ENV.fetch('DATABASE_HOST') { 'localhost' } %>
test:
<<: *default
database: myapp_test
#Set up a host similar to development
host: *development_host
After building the image, start it and make sure each container is up.
docker-compose build
docker-compose up
Don't forget to create a DB.
docker-compose run --rm rails bundle exec rails db:create
If you access http: // localhost: 3000 and the following screen is displayed, it is successful.
If you access http: // localhost: 5601 and the following screen is displayed, it is successful.
If you make a request with the Curl command and return a response that includes cluster and version information as shown below, it is successful.
-> $ curl -XGET http://localhost:9200/
{
"name" : "36e3fc003013",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "9U_MQqxLQ1adh3jHpxEfEA",
"version" : {
"number" : "7.10.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa",
"build_date" : "2020-12-05T01:00:33.671820Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
--Book "Construction of cloud infrastructure docker from the basics" --Book "Elasticsearch Practical Guide" ―― Create a search function with Rails and Elasticsearch and try various things ―― Part 1: Create a sample application ―― Qiita -Move Elasticsearch + Kibana quickly with docker-compose --Qiita
Now that you have an environment where you can use ElasticSearch, which you use for business, even for individuals, it seems that you can use it easily. I'm not very familiar with Docker, but I think that Docker's power has improved a little by building an environment by combining various middleware and Rails.
Recommended Posts