4

I'm start working rails development with Docker. Currently, I follow some tutorial to setup development environment. Everything work well. (for build, run). But now, I want to setup Ruby Remote SDK for Rubymine, so I installed SSH on docker container (the ruby container; I INSTALLED SSH BECAUSE IT'S NEEDED FOR SETTING REMOTE SDK).

Here is Dockerfile

FROM ruby:2.2.0
# Install package
RUN apt-get update -qq && apt-get install -y \
    build-essential \
    libpq-dev \
    nodejs \
    openssh-server

# Setting sshd
RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

And docker-compose.yml

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
      - "22"
    depends_on:
      - db

(For ssh -> in flow this link https://docs.docker.com/engine/examples/running_ssh_service/)

Then I connect ssh to the container. Here is my steps:

  1. Get port of ssh:

    docker port demorailsdocker_web_1

    # Here is result

    22/tcp -> 0.0.0.0:32768

    3000/tcp -> 0.0.0.0:3000

  2. Connect ssh to container

    ssh root@localhost -p 32768

    # Result ssh_exchange_identification: Connection closed by remote host

I figure out the problem is related to setup in Dockerfile.

Because when I remove those lines in docker file:

RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

And remove those lines in docker-compose.yml

 volumes:
      - .:/myapp

Then I can connect to SSH.

I think the problem is about setting work dir.


I can connect SSH well to the container by removed this line in docker-compose.yml

command: bundle exec rails s -p 3000 -b '0.0.0.0'

So I think the problem is about rails. But I don't know how to fix it.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
quangson91
  • 1,044
  • 1
  • 16
  • 30
  • Hi, I need to SSH connect. (To settup Remote Ruby SDK for Rubymine) – quangson91 Sep 29 '16 at 03:15
  • Understood, but you can directly connect to get to the SSH/system logs and check the SSH service status (to be sure it is actually running) for troubleshooting. I think your `WORKDIR` is correct and not part of the issue. – DevOps Dan Sep 29 '16 at 03:20

1 Answers1

9

I've managed to use RubyMine to remote debug rails running inside a docker, without using SSH.

Versions of software in my environment are as follows

  • RubyMine 2017.2.4 (Build #RM-172.4155.44, built on September 26, 2017)
  • Ruby inside docker (ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux])
  • Ruby SDK and Gems used by RubyMine (ruby-2.4.2-p198)

Note: for Ruby SDK, I am just using a local Ruby interpreter /usr/bin/ruby, not a remote one.

enter image description here

enter image description here

Below are the detailed steps as a demo

1. Start the docker

docker run --name rails-demo -p 1234:1234 -p 3080:3000 -it ruby bash

2. Steps inside the docker

Be sure you have the following gems in your Gemfile, and better to comment out gem pry-byebug if it's there, to avoid possible interference.

# gem 'pry-byebug'
gem 'debase', '0.2.2.beta10'
gem 'ruby-debug-ide'

Update the dependencies of your application if necessary

bundle install

Start your rails server

/home/hello_rails# rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 -- bin/rails s

3. Remote debug from RubyMine

Now start RubyMine, Run -> Debug... -> Edit Configurations...

Click plus sign '+' to add new configuration, and choose Ruby remote debug.

enter image description here

Fill the form as shown above and click the Debug button. Now you'll see in the docker the Rails server gets started:

/home/hello_rails# rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 -- bin/rails s
Fast Debugger (ruby-debug-ide 0.6.0, debase 0.2.2.beta10, file filtering is supported) listens on 0.0.0.0:1234
=> Booting Puma
=> Rails 5.1.4 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.2-p198), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Now, you can set breakpoints in RubyMine, and start remote debugging it :-)

Go to browser with URL: http://localhost:3080/say/hi (Note port 3080 is mapped from 3000, see the command of starting docker)

The breakpoint is hit as shown below, where you can inspect variables, etc.

enter image description here

One more caveat worth mentioning is that be sure the web server puma starts in single mode. For me the cluster mode does not work for remote debugging, where you'll have errors like "terminating timed out worker". For development, single mode should be good enough.

Yuci
  • 27,235
  • 10
  • 114
  • 113