[RAILS] Build Redmine code reading environment on Docker

Overview

Build an environment for reading the source code of redmine with Docker. The goal is as follows.

motivation

The reason for building the operating environment is that we want to read the code while linking the actual application behavior and the code content in our head.

Why do you want to code read?

We believe that reading code written by humans will lead to new discoveries and learning. In my case, I often have trouble with class division, naming, test code writing, error handling, etc., so I have a motivation to increase the number of withdrawals.

Why Redmine?

I chose Redmine for the following reasons.

Environment

We will proceed with reference to the following.

Preparing the repository

  1. Fork the repository of redmine
  2. Clone the forked repository
  3. Move to the redmine directory
  4. Cut another branch from the master branch (I made it code_reading)

Creating Docker related files

Create the following files directly under the redmine directory.

The contents of the file are as follows.

Dockerfile

Since Redmine's Gemfile references config / database.yml, you need to do COPY. / Redmine and then bundle install. By the way, what I refer to and do is change the installed Gem according to the DB to be used. For example, if the connection information of postgres is written in config / database.yml, the gem for connecting to postgres will be installed. I didn't have much idea of dynamically changing the gem specified in the Gemfile, so I feel that I have already learned.

Dockerfile


FROM ruby:2.5

RUN 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

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client yarn
RUN mkdir /redmine
WORKDIR /redmine
COPY Gemfile /redmine/Gemfile
COPY Gemfile.lock /redmine/Gemfile.lock
COPY . /redmine
RUN bundle install

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

entrypoint.sh


#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /redmine/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

docker-compose.yml

The commented out is the setting for ruby-debug / ruby-debug-ide. I introduced it, but the operation was not stable in my environment, so I try not to use it once.

docker-compose.yml


version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    ports:
        - "5433:5432"
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    # command: bash -c "rm -f tmp/pids/server.pid && bundle exec rdebug-ide --host 0.0.0.0 --port 1234 -- bin/rails s -p 3000 -b 0.0.0.0"
    command: bash -c "rm -f tmp/pids/server.pid && bin/rails s -p 3000 -b 0.0.0.0"
    volumes:
      - .:/redmine
    ports:
      - "3003:3000"
      # - "1234:1234"
      # - "26162:26162"
    stdin_open: true
    tty: true
    depends_on:
      - db

Addition of Gem for debugging

Add the following to Gemfile. Refer to the articles such as.

Gemfile


group :development do
  gem "yard" #Add below this
  gem "better_errors"
  gem "binding_of_caller"
  gem "pry-rails"
  gem "pry-byebug"
  #Commented out because it did not work stably
  # gem "ruby-debug-ide"
  # gem "debase"      
end

Also, in order to use better_erros in the environment on Docker, add the following to development.rb.

config/enviroments/development.rb


BetterErrors::Middleware.allow_ip! "0.0.0.0/0"

database.yml settings

Create config / database.yml and make it as follows.

config/database.yml


default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: password

development:
  <<: *default
  database: redmine_development

test:
  <<: *default
  database: redmine_test

production:
  <<: *default
  database: redmine

Launching the environment

Now that the file is ready, start the container and run the application.

#Empty Gemfile.Make a lock
$ touch Gemfile.lock
#Build container image
$ docker-compose build
#Start container
$ docker-compose up -d
#DB creation
$ docker-compose run --rm web rake db:create
#Perform migration
$ docker-compose run --rm web bin/bundle exec rake db:migrate
#Redmine default data entry task
$ docker-compose run --rm web bin/bundle exec rake redmine:load_default_data

If the startup is successful, the Status will be Up as shown below with docker-compse ps.

$ dcom ps
    Name                   Command               State    Ports                                  
--------------------------------------------------------------------------------
redmine_db_1    docker-entrypoint.sh postgres    Up      0.0.0.0:5433->5432/tcp                                                  
redmine_web_1   entrypoint.sh bash -c rm - ...   Up      0.0.0.0:3003->3000/tcp

After this, if you access http: // localhost: 3003, the Redmine screen will be displayed. Also, at first, you can log in with ʻadmin` for both ID and password. This completes the environment construction.

Debugging with pry-byebug

Attach to the container while the container is running with docker-compose up.

$ docker attach redmine_web_1

Add binding.pry to the relevant part of the code you want to check. The following example is added to wellcome # index which is executed when accessing the root path of Redmine.

app/controllers/welcome_controller.rb


  def index
    binding.pry #Add where you want to look
    @news = News.latest User.current
  end

If you access http: // localhost: 3003 in this state, the following contents will be displayed on the terminal and you will be able to execute steps.

From: /redmine/app/controllers/welcome_controller.rb:25 WelcomeController#index:

    23: def index
    24:   binding.pry
 => 25:   @news = News.latest User.current
    26: end

[1] pry(#<WelcomeController>)> Started GET "/" for 172.18.0.1 at 2020-09-27 07:41:34 +0000
[1] pry(#<WelcomeController>)> 

You can execute steps by typing a command at [1] pry (# <WelcomeController>)>.

that's all

Recommended Posts

Build Redmine code reading environment on Docker
Build Unity development environment on docker
Redmine on Docker
Build an environment with Docker on AWS
Build an Ultra96v2 development environment on Docker 1
Run VS Code on Docker
Easily build Redmine on Windows using WSL2 and Docker
Redmine (Docker) environment construction memo
Build docker environment with WSL
Build Java development environment with WSL2 Docker VS Code
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Try using Redmine on Mac docker
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build Clang x VSCode on Docker (1)
Build a XAMPP environment on Ubuntu
Rails on Docker environment construction procedure
Build docker + laravel environment with laradock
Build Redmine on Azure App Service
Build an ASP.net Core Web API environment on Docker (VSCode) Part 1
Build an ASP.NET Core Web API environment on Docker (VSCode) Part 2
Building a haskell environment with Docker + VS Code on Windows 10 Home
Build a PureScript development environment with Docker
Use docker in proxy environment on ubuntu 20.04.1
Build a WAS execution environment from Docker
Build a Java development environment on Mac
Build Java 8 development environment on AWS Cloud9
Build a Wordpress development environment with Docker
Build Cakephp environment from docker installation Ubuntu
[Docker] Build Jupyter Lab execution environment with Docker
Build a JMeter environment on your Mac
Try the Docker environment on AWS ECS
Launched Redmine with Docker on Raspberry Pi 3
Build TensorFlow operation check environment with Docker
How to build Rails 6 environment with Docker
Build a simple Docker + Django development environment
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Docker] How to build when the source code is bind-mounted on the container
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Build a Docker-based development environment on Windows 10 Home 2020 ver. Part 2 VS Code should make the Docker development environment comfortable
Build a development environment for Docker + Rails6 + Postgresql
Build a Doker-based development environment on Windows 10 Home 2020 ver. Part 1 Until WSL2-based Docker build
Liberty on Docker
Just install Laravel8 on docker in PHP8 environment
Build a Laravel / Docker environment with VSCode devcontainer
Environment construction command memo with Docker on AWS
Build a WordPress development environment quickly with Docker
[Java] Build Java development environment on Ubuntu & check execution
Build Apache / Tomcat development environment on Cent OS 7
Build Java x Spring x VSCode x Gradle on Docker (1)
Building OpenPose (Pytorch_Realtime_Multi-Person_Pose_Estimation) environment on Docker: training part
Build a Laravel environment on an AWS instance
[Ruby on Rails] Let's build an environment on mac
Build a development environment to create Ruby on Jets + React apps with Docker
Build a development environment for Docker, java, vscode
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[Rails] How to build an environment with Docker