・ Docker: 19.03.8 -Docker-Compose: 1.25.5 ・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The folder name will be myapp
(anything is fine).
Terminal
$ mkdir myapp
Terminal
$ cd myapp
Dockerfile
Terminal
$ vi Dockerfile
Dockerfile
FROM ruby:2.7.1
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
#Added a script that runs every time the container starts
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
#Start main process
CMD ["rails", "server", "-b", "0.0.0.0"]
FROM
: Set the image and version to use
RUN
: Command execution
WORKDIR
: Set the working directory
COPY
: Copy local files to container
ʻENTRYPOINT: Set the command to be executed first ʻEXPOSE
: Set the port number on which the container listens
CMD
: Software execution inside the image
** ◎ Obtain the latest stable version as of July 19, 2020 as an image to use. ** **
FROM ruby:2.7.1
** ◎ Install Node.js and PostgreSQL packages. ** **
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
** ◎ Create a myapp
directory inside the container. ** **
RUN mkdir /myapp
** ◎ Set the working directory to the myapp
created above **
WORKDIR /myapp
** ◎ Copy the local Gemfile
to myapp
in the container. ** **
COPY Gemfile /myapp/Gemfile
** ◎ Copy the local Gemfile.lock
to myapp
in the container. ** **
COPY Gemfile.lock /myapp/Gemfile.lock
** ◎ Execute bundle install
. ** **
RUN bundle install
** ◎ Copy the current directory (local myapp) to myapp
in the container. ** **
.
= Current directoryCOPY . /myapp
** ◎ Copy the local ʻentrypoint.sh to ʻusr / bin
in the container. ** **
COPY entrypoint.sh /usr/bin/
** ◎ Allow access to ʻentrypoint.sh` copied above. ** **
RUN chmod +x /usr/bin/entrypoint.sh
** ◎ Set ʻentrypoint.sh` as the first command to be executed when the container is started. ** **
ENTRYPOINT ["entrypoint.sh"]
** ◎ Set the port number to 3000
. ** **
EXPOSE 3000
** ◎ Launch the Rails server. ** **
CMD ["rails", "server", "-b", "0.0.0.0"]
Gemfile
Specify the Rails version. This time we will make it compatible with Rails 6.
Terminal
$ vim Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 6'
Gemfile.lock
Terminal
$ touch Gemfile.lock
Terminal
$ vi entrypoint.sh
entrypoint.sh
#!/bin/bash
set -e
#Rails compatible file server.Delete the pid as it may exist.
rm -f /myapp/tmp/pids/server.pid
#Run the container process. (The one set in CMD in the Dockerfile.)
exec "$@"
docker-compose.yml
Terminal
$ vi docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
version
: Set the version of docker-compose.
services
: Create services in the hash below. You can name it freely, but usually you name it web and db.
ʻImage: image to use (specify PostgreSQL in db)
volumes: Directory mount settings (db data etc. can be left)
build: Path with Dockerfile etc. (basically current directory)
command: Command (delete the server.pid file and then start the rails server)
ports: Port number (host: set in container)
depends_on`: Shows the dependencies and allows you to specify the startup order. Here, start from db → web.
Terminal
myapp
- Dockerfile
- Gemfile
- Gemfile.lock
- entrypoint.sh
- docker-compose.yml
Terminal
$ docker-compose run web rails new . --force --no-deps --database=postgresql --skip-bundle
--force
: Overwrite Gemfile
--no-deps
: Do not start the linked service
--database = postgresql
: Specify PostgreSQL for DB
--skip-bundle
: Skip bundle
bundle install
Terminal
Could not find gem 'pg (>= 0.18, < 2.0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
I think that the above error occurred in 7
, so execute the following command to solve it.
It will bundle install
when you build the Docker image.
Terminal
$ docker-compose build
database.yml
config/database.yml
#Change before
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
#After change
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
Terminal
$ docker-compose up
Terminal
Error: No such file or directory @ rb_sysopen - /myapp/config/webpacker.yml (RuntimeError)
webpacker.yml
Terminal
touch config/webpacker.yml
webpacker.yml
# Note: You must restart bin/webpack-dev-server for changes to take effect
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
webpack_compile_output: true
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
additional_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
# Extract and emit a css file
extract_css: true
static_assets_extensions:
- .jpg
- .jpeg
- .png
- .gif
- .tiff
- .ico
- .svg
- .eot
- .otf
- .ttf
- .woff
- .woff2
extensions:
- .mjs
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Set to false if using HMR for CSS
extract_css: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
pretty: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: '**/node_modules/**'
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Cache manifest.json for performance
cache_manifest: true
Terminal
docker-compose run web rake db:create
Terminal
could not translate host name "db" to address: Name or service not known
Terminal
Error: Database is uninitialized and superuser password is not specified.
docker-compose.yml
docker-compose.yml
#Change before
environment:
POSTGRES_PASSWORD: password
#After change
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
Recommended Posts