How to build Rails, Postgres, ElasticSearch development environment with Docker

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.

environment

$ 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

1. Create a Rails application environment with Docker

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

Create a 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

2. 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

3. Make DB settings

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

4. Start and check

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

Rails confirmation

If you access http: // localhost: 3000 and the following screen is displayed, it is successful.

スクリーンショット 2020-12-30 17.14.07.png

Confirmation of Kibana

If you access http: // localhost: 5601 and the following screen is displayed, it is successful.

スクリーンショット 2020-12-30 16.59.14.png

Check ElasticSearch

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"
}

reference

--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

Summary

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

How to build Rails, Postgres, ElasticSearch development environment with Docker
How to build Rails 6 environment with Docker
[Rails] How to build an environment with Docker
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to install Pry after building Rails development environment with Docker
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to build docker environment with Gradle for intelliJ
How to build Java development environment with VS Code
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Build a PureScript development environment with Docker
[Rails] How to use rails console with docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
Steps to build a Ruby on Rails development environment with Vagrant
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Build a development environment for Docker + Rails6 + Postgresql
Build a WordPress development environment quickly with Docker
Rails6.0 ~ How to create an eco-friendly development environment
How to build API with GraphQL and Rails
[First team development ②] Build an environment with Docker
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to execute with commands of normal development language in Docker development environment
I tried to build a Firebase application development environment with Docker in 2020
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
How to build an environment with Docker, which is the minimum required to start a Rails application
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
Build Java development environment with WSL2 Docker VS Code
How to query Array in jsonb with Rails + postgres
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Try to build a Java development environment using Docker
[Docker + Rails] How to deal with Rails server startup failure
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
Build a development environment to create Ruby on Jets + React apps with Docker
Rails + MySQL environment construction with Docker
How to set environment variables when using Payjp with Rails
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
Build jooby development environment with Eclipse
Build a Node-RED environment with Docker to move and understand
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Environment construction with Docker] Rails 6 & MySQL 8
How to get along with Rails
I tried to create a padrino development environment with Docker
Build Unity development environment on docker
Build docker + laravel environment with laradock
How to build CloudStack using Docker
How to start Camunda with Docker
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
I tried to build the environment of PlantUML Server with Docker
Super beginner builds Rails6 + Postgresql environment with Docker to the end