[RUBY] I tried to create a padrino development environment with Docker

I often see articles about running Ruby on Rails with Docker, but I don't see padrino so much, so a memo of the procedure when I tried it after studying. Creating a Docker image is a bit cumbersome because the padrino g project forces a subdirectory to be created.

Verification environment

Windows10 Home Edition
VirtualBox 6.1.10

# Docker Host OS (CoreOS)
$ uname -a
Linux default 4.9.93-boot2docker #1 SMP Thu Jul 19 18:29:50 UTC 2018 x86_64 GNU/Linux

Docker version 18.06.1-ce, build e68fc7a
docker-compose version 1.20.1, build 5d8c71b

Overview of installation procedure

  1. First create a Docker image containing the initial project created by padrino g project (called the base image)
  2. Create a container from the base image and copy the initial project folder to the host OS side with docker cp
  3. Create an image that overwrites the project on the host OS side based on the base image (called the ʻapp` image)
  4. Subsequent development work will change the source code of the project on the host OS and recreate the ʻapp` image.

Since the subdirectory is created by padrino g project, WORKDIR changes before and after the project is created, so the above procedure is taken.

Initial directory structure

Initially only docker-compose.yml and Dockerfile in the padrino folder

padrino
|   docker-compose.yml
|   Dockerfile

docker-compose.yml

Contents for creating a base image

docker-compose.yml


version: "3.4"
services:
  base: 
    build:
      context: .
      dockerfile: ./Dockerfile
      target: "padrino-base"
    image: takaya030/padrino-base
    ports: 
      - "3000:3000"
    working_dir: /workspace/myapp
    command: "bundle exec padrino s -h 0.0.0.0"

Dockerfile

This is also the content for creating a base image

Dockerfile


FROM ruby:2.7.1 as padrino-base
LABEL maintainer "takaya030"

RUN apt-get update -qq && \
    apt-get install -y build-essential libpq-dev nodejs && \
	apt-get clean && \
	rm -rf /var/lib/apt/lists/*

# insall padrino
RUN mkdir /workspace
WORKDIR /workspace
RUN bundle init && \
	echo 'gem "padrino"' >>Gemfile && \
	bundle config set path 'vendor/bundle' && \
	bundle install && \
	bundle exec padrino g project myapp -t rspec -e erb -c sass -s jquery -d activerecord && \
	rm -rf Gemfile Gemfile.lock .bundle vendor

# create project
WORKDIR /workspace/myapp
RUN bundle config set path 'vendor/install' && \
	bundle install

CMD ["true"]

Build the base image

$ cd padrino
$ docker-compose build base

Checking the operation of the base image

Start Docker container

$ docker-compose up -d

If you access http://192.168.99.100:3000/ with a web browser and the following is displayed, it is working properly.

padrino_1.png

Stop the container once

$ docker-compose stop

Copy the project folder to the host OS side

Copy the project folder in the stopped container to the host OS side This is the actual development environment

$ docker cp padrino_base_1:/workspace/myapp .

# "padrino_base_1"Is the name of the container you just stopped

When the copy is completed, the development environment will be created under the padrino / myapp / folder.

padrino
|   docker-compose.yml
|   Dockerfile
|   
\---myapp

Delete the container when the copy is complete

$ docker-compose rm

ʻApp` Add settings for image creation

docker-compose.yml

Change to the following contents

docker-compose.yml


version: "3.4"
services:
  base: 
    build:
      context: .
      dockerfile: ./Dockerfile
      target: "padrino-base"
    image: takaya030/padrino-base
  app: 
    build:
      context: .
      dockerfile: ./Dockerfile
      target: "padrino-app"
    image: takaya030/padrino-app
    ports: 
      - "3000:3000"
    volumes:
      - ./myapp:/workspace/myapp
    working_dir: /workspace/myapp
    command: "bundle exec padrino s -h 0.0.0.0"

Dockerfile

Change to the following contents

Dockerfile


FROM ruby:2.7.1 as padrino-base
LABEL maintainer "takaya030"

RUN apt-get update -qq && \
    apt-get install -y build-essential libpq-dev nodejs && \
	apt-get clean && \
	rm -rf /var/lib/apt/lists/*

# insall padrino
RUN mkdir /workspace
WORKDIR /workspace
RUN bundle init && \
	echo 'gem "padrino"' >>Gemfile && \
	bundle config set path 'vendor/bundle' && \
	bundle install && \
	bundle exec padrino g project myapp -t rspec -e erb -c sass -s jquery -d activerecord && \
	rm -rf Gemfile Gemfile.lock .bundle vendor

# create project
WORKDIR /workspace/myapp
RUN bundle config set path 'vendor/install' && \
	bundle install

CMD ["true"]

#=========================================
FROM padrino-base as padrino-app

COPY ./myapp /workspace/myapp
WORKDIR /workspace/myapp

CMD ["bundle","exec","padrino","s","-h","0.0.0.0"]

ʻApp` Image build

$ docker-compose build app

ʻApp` Image operation check

Start Docker container

$ docker-compose up -d

Go to http://192.168.99.100:3000/ with a web browser and check that the same screen as before is displayed.

Confirmation of reflection of source code changes

With the container running, change myapp / app / app.rb as follows

myapp/app/app.rb


--- a/app.rb      Sat Jun 06 19:19:23 2020
+++ b/app.rb      Sat Jun 06 22:30:27 2020
@@ -62,5 +62,10 @@
     #     render 'errors/500'
     #   end
     #
+
+    get '/' do
+      "Hello World"
+    end
+
   end
 end

If you access http://192.168.99.100:3000/ again with a web browser and it is displayed as shown in the image below, it is working normally.

padrino_2.png

How to add a gem package

Check currently installed packages

$ docker-compose run --rm app bundle list
Gems included by the bundle:
  * activemodel (6.0.3.1)
  * activerecord (6.0.3.1)
  * activesupport (6.0.3.1)
  * concurrent-ruby (1.1.6)
  * diff-lcs (1.3)
  * erubi (1.9.0)
  * ffi (1.13.0)
  * i18n (1.8.3)
  * mail (2.7.1)
  * mime-types (3.3.1)
  * mime-types-data (3.2020.0512)
  * mini_mime (1.0.2)
  * minitest (5.14.1)
  * moneta (1.1.1)
  * mustermann (1.1.1)
  * padrino (0.15.0)
  * padrino-admin (0.15.0)
  * padrino-cache (0.15.0)
  * padrino-core (0.15.0)
  * padrino-gen (0.15.0)
  * padrino-helpers (0.15.0)
  * padrino-mailer (0.15.0)
  * padrino-support (0.15.0)
  * rack (2.2.2)
  * rack-protection (2.0.8.1)
  * rack-test (1.1.0)
  * rake (13.0.1)
  * rb-fsevent (0.10.4)
  * rb-inotify (0.10.1)
  * rspec (3.9.0)
  * rspec-core (3.9.2)
  * rspec-expectations (3.9.2)
  * rspec-mocks (3.9.1)
  * rspec-support (3.9.3)
  * ruby2_keywords (0.0.2)
  * sass (3.7.4)
  * sass-listen (4.0.0)
  * sinatra (2.0.8.1)
  * sqlite3 (1.4.2)
  * thor (0.20.3)
  * thread_safe (0.3.6)
  * tilt (2.0.10)
  * tzinfo (1.2.7)
  * zeitwerk (2.3.0)
Use `bundle info` to print more detailed information about a gem

Describe the package you want to add to myapp / Gemfile

myapp/Gemfile


--- a/Gemfile     Sat Jun 06 19:19:23 2020
+++ b/Gemfile     Sun Jun 07 21:57:33 2020
@@ -22,6 +22,10 @@
 gem 'activerecord', '>= 3.1', :require => 'active_record'
 gem 'sqlite3'

+# OAuth
+gem "omniauth"
+gem "omniauth-twitter"
+
 # Test requirements
 gem 'rspec', :group => 'test'
 gem 'rack-test', :require => 'rack/test', :group => 'test'

Install the package with the following command

$ docker-compose run --rm app bundle install

Check if it was installed successfully (Confirm that ʻomniauth` related packages have been added)

$ docker-compose run --rm app bundle list
Gems included by the bundle:
  * activemodel (6.0.3.1)
  * activerecord (6.0.3.1)
  * activesupport (6.0.3.1)
  * concurrent-ruby (1.1.6)
  * diff-lcs (1.3)
  * erubi (1.9.0)
  * ffi (1.13.0)
  * hashie (4.1.0)
  * i18n (1.8.3)
  * mail (2.7.1)
  * mime-types (3.3.1)
  * mime-types-data (3.2020.0512)
  * mini_mime (1.0.2)
  * minitest (5.14.1)
  * moneta (1.1.1)
  * mustermann (1.1.1)
  * oauth (0.5.4)
  * omniauth (1.9.1)
  * omniauth-oauth (1.1.0)
  * omniauth-twitter (1.4.0)
  * padrino (0.15.0)
  * padrino-admin (0.15.0)
  * padrino-cache (0.15.0)
  * padrino-core (0.15.0)
  * padrino-gen (0.15.0)
  * padrino-helpers (0.15.0)
  * padrino-mailer (0.15.0)
  * padrino-support (0.15.0)
  * rack (2.2.2)
  * rack-protection (2.0.8.1)
  * rack-test (1.1.0)
  * rake (13.0.1)
  * rb-fsevent (0.10.4)
  * rb-inotify (0.10.1)
  * rspec (3.9.0)
  * rspec-core (3.9.2)
  * rspec-expectations (3.9.2)
  * rspec-mocks (3.9.1)
  * rspec-support (3.9.3)
  * ruby2_keywords (0.0.2)
  * sass (3.7.4)
  * sass-listen (4.0.0)
  * sinatra (2.0.8.1)
  * sqlite3 (1.4.2)
  * thor (0.20.3)
  * thread_safe (0.3.6)
  * tilt (2.0.10)
  * tzinfo (1.2.7)
  * zeitwerk (2.3.0)
Use `bundle info` to print more detailed information about a gem

reference

-How to get started with Padrino-- Qiita -Try using Padrino without polluting the environment --Qiita -Create a development environment with Docker Compose by making the DB withdrawn in the application server independent --Qiita

Recommended Posts

I tried to create a padrino development environment with Docker
I tried to create a java8 development environment with Chocolatey
I tried to build a Firebase application development environment with Docker in 2020
I tried to create a Spring MVC development environment on Mac
Create a Spring Boot development environment with docker
Create a Vue3 environment with Docker!
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Build a development environment to create Ruby on Jets + React apps with Docker
I made a development environment with rails6 + docker + postgreSQL + Materialize.
[Rails] I tried to create a mini app with FullCalendar
I tried to create React.js × TypeScript × Material-UI on docker environment
Build a PureScript development environment with Docker
Create a MySQL environment with Docker from 0-> 1
Create Spring Boot-gradle-mysql development environment with Docker
Build a Wordpress development environment with Docker
I tried to build the environment of PlantUML Server with Docker
I tried to build an http2 development environment with Eclipse + Tomcat
I tried to build a laravel operating environment while remembering Docker
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
[Memo] Create a CentOS 8 environment easily with Docker
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
Build a WordPress development environment quickly with Docker
I tried to create a LINE clone app
I tried to break a block with java (1)
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)
Create a java web application development environment with docker for mac part2
I tried migrating the portfolio created on Vagrant to the Docker development environment
I tried to take a look at the flow of Android development environment construction with Android Studio
I tried BIND with Docker
[Note] Create a java environment from scratch with docker
Try to build a Java development environment using Docker
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
I tried to build an environment using Docker (beginner)
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
I tried to create a shopping site administrator function / screen with Java and Spring
I tried to make an introduction to PHP + MySQL with Docker
Build a Node.js environment with Docker
I tried to modernize a Java EE application with OpenShift.
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I tried to interact with Java
Create SolrCloud verification environment with Docker
Create Laravel environment with Docker (docker-compose)
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
I tried to build the environment little by little using docker
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
I tried to build the environment of WSL2 + Docker + VSCode
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
Docker command to create Rails project with a single blow in environment without Ruby
[SAP] Create a development environment with NW AS ABAP Developer Edition (1)
I tried to create a simple map app in Android Studio
How to install Pry after building Rails development environment with Docker
PostgreSQL environment construction with Docker (from setup to just before development)
I tried deploying a Docker container on Lambda with Serverless Framework
Build a development environment for Django + MySQL + nginx with Docker Compose
Steps to build a Ruby on Rails development environment with Vagrant
I tried to make a group function (bulletin board) with Rails
Create a web environment quickly using Docker