Build an environment for reading the source code of redmine with Docker. The goal is as follows.
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.
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.
I chose Redmine for the following reasons.
We will proceed with reference to the following.
redmine
directorycode_reading
)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
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"
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
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.
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