[Rails 6] Dockerize existing Rails apps [Docker]

Introduction

Hello,Qiita! :sunny: This time I'm going to put an existing Rails app on Docker! It's as simple as moving the environment inside a container, I got stuck a little with installing webpack and yarn, so I wrote an article! Let's orchestrate two containers, Rails and MySQL, with docker-compose: musical_score: Let's go! !! : whale2:

Prerequisites

Description of Dockerfile

Describe as follows.

Dockerfile


FROM ruby:2.7.1

ENV BUNDLER_VERSION="2.1.4" \
    APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn \
    TZ=Asia/Tokyo

RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    nodejs \
    mariadb-client \
    sudo \
    vim && \
    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

WORKDIR /app

COPY Gemfile Gemfile.lock /app/

COPY . /app

RUN bundle install -j4 && \
    yarn upgrade && \
    rails webpacker:install && \
    yarn install --check-files

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"]

What are you doing

FROM Get the official image (this time Ruby version 2.7.1) from Docker Hub.

ENV Define environment variables.

APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn ↑ You can hide the Warning message by writing this

RUN Execute the command on the Docker container.

apt-get This command means to operate and manage packages using the APT (Advanced Package Tool) library, which is a package management system for Debian distributions (Debian and Ubuntu). This time I took the image of Ruby, but ruby is installed on the Debian image. Debian uses the Linux kernel, so you can use Linux commands.

WORKDIR Set the work directory. It can be specified multiple times in the same Dockerfile, and the path registered in ENV may be used.

COPY Copy the new file to a folder (compressed files are not uncompressed).

ENTRYPOINT Execute the described command.

EXPOSE Release a specific port.

CMD Set the default value of the container to be executed. Only __ once __ can be used in the same Dockerfile, and it is possible to set an argument for ENTRYPOINT. The Dockerfile should contain ENTRYPOINT or CMD at least once __. __

Description of docker-compose.yml

Describe as follows.

docker-compose.yml


version: '3'

services:
  app:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
      - ./vendor/bundle:/myapp/vendor/bundle:delegated
    ports:
      - "19802:3000" # "Any port:3000"To
    depends_on:
      - db
    tty: true
    stdin_open: true

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: "password"
    ports:
      - "19801:3306" #me too,"Any port:3306"To
    volumes:
      - ./tmp/db:/var/lib/mysql

What are you doing

version Define the version of docker-compose to use.

services Define each element (service) to run the application. (This time app and db)

build Execute the compose file and specify the path when it is built.

command Override the default command.

volumes Specify the path of the configuration file to mount.

ports Specify the port number when launching the Docker image.

depends_on Specify the dependency between services.

environment Specify environment variables.

tty: true Set the container to keep running.

stdin_open: true Set standard I / O permissions in the container.

Description of entrypoint.sh

Describe as follows.

entrypoint.sh


#!/bin/bash
set -e

#Rails server.get rid of pid file
rm -f /app/tmp/pids/server.pid

#Run the main process of the container(What is set as CMD in the Dockerfile)。
exec "$@"

Description of database.yml

Describe as follows.

database.yml


default: &default
  adapter: mysql2
  pool: 5
  timeout: 5000 #Any

development:
  <<: *default
  database: myapp_development
  username: root
  password: password
  host: db
  port: 3306

Checking Gemfile and Gemfile.lock

This time we will put the existing application on Docker, so all you have to do is make sure that Gemfile and Gemfile.lock exist.

Launch docker-compose

At the terminal docker-compose build To execute.

Terminal


Successfully built ~
Successfully tagged ~

If the output is output, the build is successful.

next, docker-compose exec app bash To enter the app container. afterwards, bundle exec rails db:create bundle exec rails db:migrate bundle exec rails db:seed And you're done! : sunglasses:

At the end

Thank you for watching until the end. Personally, it's really fun to work inside a container. I don't know why. (It feels like an inception) Also, I would appreciate it if you could let me know if you have any inappropriate expressions.

See you again! : rocket:

Recommended Posts

[Rails 6] Dockerize existing Rails apps [Docker]
Migrate existing Rails 6 apps to Docker environment
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
Rails Docker environment construction
Docker x Rails 6 (memo)
Rails deploy with Docker
Make Nuxt apps Docker
Rails6 I tried to introduce Docker to an existing application
Run Rails whenever with docker
[Docker] Rails 5.2 environment construction with docker
[Docker] Use whenever with Docker + Rails
Deploy Rails apps to Azure App Service using Docker & Continuous Deployment
Rails + MySQL environment construction with Docker
Build environment with vue.js + rails + docker
Docker + rails + Vue.js localhost connection refused
Build Rails environment with Docker Compose
[SRE / Docker] Start control with Dockerize
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
Deploy Rails on Docker to heroku
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development