I wanted to touch on Docker, so I decided to build the above environment.
I repeated trial and error because I had no previous knowledge ...
However, I managed to build it, so I will leave a note.
I added ?
to the annotations and explanations for the parts that I didn't understand well.
Prepare a folder with an arbitrary name, and here it is my_app
.
$ mkdir my_app
Prepare the following files in my_app
$ touch xxx(Required file name)
- my_app
- .dockerignore
- Dockerfile
- docker-compose.yml
- Gemfile
- Gemfile.lock
- entrypoint.sh
Edit each file as follows.
.dockerignore Prevents local modules and debug logs from being copied to the Docker image. The required modules are different between the Mac environment and the Linux environment on Docker ... Is it? Anyway, without this, I get an error related to Node.js 12.x.
.dockerignore:.dockerignore
node_modules
npm-debug.log
.Dockerfile Install postgresql and yarn and Node.js required for rails6 Webpacker. I can only roughly understand each command listed (Fu) I think I need to study ...
Dockerfile
FROM ruby:2.7.1
#Required library installation
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
#Install yarn package management tool
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn
# Node.install js
RUN curl -SL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get install -y nodejs
#Build working folder in container
RUN mkdir /my_app
WORKDIR /my_app
COPY Gemfile /my_app/Gemfile
COPY Gemfile.lock /my_app/Gemfile.lock
RUN bundle install
COPY . /my_app
#entrypoint.sh and connection settings
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
Here, the user name and password used in Postgresql are postgres
.
There are web
and db
in the service.
docker-compose.yml
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/my_app
ports:
- "3000:3000"
depends_on:
- db
Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 6'
Gemfile.lock You don't need to edit the contents, just generate the file.
entrypoint.sh
entrypoint.sh
#!/bin/bash
set -e
#The server that has remained since the last rails server startup.Processing to erase pid?
rm -f /my_app/tmp/pids/server.pid
#Run the main process of the container(Command specified by CMD in Dockerfile)
exec "$@"
run
builds the web alone? And runs rails new
inside the web container
$ docker-compose run web rails new . --force --no-deps --database=postgresql --skip-bundle
--no-deps
: Do not start linked service (do not start db linked to web now?)
--skip-bundle
: Do not run bundle
bundle install
The Gemfile has been rewritten with the previous rails new
.
By build
ing the image, bundle install
is also done, so execute the following command.
$ docker-compose build
I think the working folder already has a set of familiar rails files, so
Edit config / database.yml
as follows.
config/database.yml
default: &default
adapter: postgresql
encoding: unicode
host: db
#docker-compose.Match the Postgresql user name and password written in yml
username: postgres
password: postgres
pool: 5
development:
<<: *default
database: my_app_development
test:
<<: *default
database: my_app_test
Run bundle exec rails webpacker: install
inside the web container.
$ docker-compose run web bundle exec rails webpacker:install
In my case, if I do not make this setting, an error will occur when starting the server.
Edit the config / webpacker.yml
generated by rails webpacker: install
as follows:
config/webpacker.yml
development:
dev_server:
host: webpacker
hmr: true
Also add the following code to config / environments / development.rb
.
But maybe this process alone is not necessary.
config/environments/development.rb
config.webpacker.check_yarn_integrity = false
$ docker-compose up
At this point you can access http: // localhost: 3000 /
,
Since the DB has not been created yet, an error screen will be displayed.
It's really rudimentary, but it's been a long way, so I was pretty impatient (laughs)
$ docker-compose run web rails db:create
If you see "Yay! You're on Rails!" At http: // localhost: 3000 /
, you're successful!
If you have created unnecessary docker images or containers, please delete them yourself.
I think there are many points that cannot be reached for the first posted article. Thank you for staying with us until the end. If you find any mistakes in the article, please let us know.
Docker + Rails6 + PostgreSQL environment construction Problem and solution that container falls due to Yarn error immediately after docker-compose up Make Node.js Web Application Docker I want to use an existing rails6 app with a combination of Docker and webpacker docker-compose I didn't understand the difference between'up',' build'and'start', so I summarized it.
Recommended Posts