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.
? 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
$ mkdir my_app
Prepare the following files in
$ 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.
.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 ...
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"]
Here, the user name and password used in Postgresql are
db in the service.
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
source 'https://rubygems.org' gem 'rails', '~> 6'
Gemfile.lock You don't need to edit the contents, just generate the file.
#!/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
The Gemfile has been rewritten with the previous
building 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
config / database.yml as follows.
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
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.
config / webpacker.yml generated by
rails webpacker: install as follows:
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.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.