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.
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
padrino g project
(called the base
image)base
image and copy the initial project folder to the host OS side with docker cp
base
image (called 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.
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"]
base
image$ cd padrino
$ docker-compose build base
base
imageStart 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.
Stop the container once
$ docker-compose stop
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
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"]
$ docker-compose build app
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.
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.
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
-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