Rails environment construction with Docker (personal apocalypse)

Creating a Dockerfile

-#Build context
% mkdir product-register
% cd product-register
-#Gemfile and Gemfile.Creating a lock
% touch Gemfile Gemfile.lock
-#Editing Gemfile
% vim Gemfile


source 'https://rubygems.org'
gem 'rails', '~>5.2'
-#Editing the Dockerfile
% vim Dockerfile


FROM ruby:2.5
RUN apt-get update && apt-get install -y && \
    buile-essential \
    libpq-dev \
    nodejs \
    postgresql-client \
WORKDIR /product-register
COPY Gemfile Gemfile.lock /product-register/
RUN bundle install
-#Creating a container
% docker build .

Description of docker-compose.yml

-# docker-compose.edit yml
% vim docker-compose.yml


version: '3'

    build: .
      - '3000:3000'
      - '.:/product-register'
    tty: true
    stdin_open: true
-#Container launch
% docker-compose up -d
-#When working inside a container
% docker-compose exec web bash

Rails setup

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

I can connect with localhost: 3000, but I get an error page because I have not set the DB.

DB setup


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.

Modify docker-compose.yml


version: '3'
#docker volume Data is saved here

    build: .
      - '3000:3000'
      - '.:/product-register'
#Environment variable settings of the container, originally the password should not be written directly.
      - 'DATABASE_PASSWORD=postgres'
    tty: true
    stdin_open: true
#Created once the db service is created
      - db
#You can access db from the web
      - db
#postgres container
    image: postgres
#Host db-Store data in data
      - 'db-data:/var/lib/postgresql/data'
      - 'POSTGRES_USER=postgres'
      - 'POSTGRES_PASSWORD=postgres'

Start with docker-compose

% 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 an app and check the operation

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

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

Rails environment construction with Docker (personal apocalypse)
[Docker] Rails 5.2 environment construction with docker
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
Rails Docker environment construction
Rails6 [API mode] + MySQL5.7 environment construction with Docker
React environment construction with Docker
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Rails on Docker environment construction procedure
Docker environment construction
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
GPU environment construction with Docker [October 2020 version]
Building Rails 6 and PostgreSQL environment with Docker
Laravel development environment construction with Docker (Mac)
Create Rails 6 + MySQL environment with Docker compose
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Laravel + MySQL + phpMyadmin environment construction with Docker
How to build Rails 6 environment with Docker
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Rails deploy with Docker
Spring Boot environment construction with Docker (January 2021 version)
Environment construction command memo with Docker on AWS
[Personal memo] Ruby on Rails environment construction (Windows)
React + Django + Nginx + MySQL environment construction with Docker
[Rails] How to build an environment with Docker
Wordpress local environment construction & development procedure with Docker
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
Pytorch execution environment with Docker
Rails engineer environment construction ruby2.7.1
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Stable development environment construction manual for "Rails6" with "Docker-compose"
MySQL 5.7 (Docker) environment construction memo
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Run Rails whenever with docker
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Rails / MySQL] Mac environment construction
[Docker] postgres, pgadmin4 environment construction
Database environment construction with Docker in Spring boot (IntellJ)
Construction of data analysis environment using Docker (personal memorandum)
[Docker] Use whenever with Docker + Rails
Easy environment construction of MySQL and Redis with Docker and Alfred
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)
How to build Rails, Postgres, ElasticSearch development environment with Docker
Create Rails5 and postgresql environment with Docker and make pgadmin available
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Laravel + Docker Laradock usage environment construction
Build PlantUML environment with VSCode + Docker
Create SolrCloud verification environment with Docker
Ruby on Rails 6.0 environment construction memo
Create Laravel environment with Docker (docker-compose)