Skip to content

Instantly share code, notes, and snippets.

@hbin
Forked from briankung/docker-pry-rails.md
Created March 10, 2017 18:07
Show Gist options
  • Save hbin/b8dbfbf7ae612b78c4d6398fea0c3bae to your computer and use it in GitHub Desktop.
Save hbin/b8dbfbf7ae612b78c4d6398fea0c3bae to your computer and use it in GitHub Desktop.

Revisions

  1. @briankung briankung revised this gist Oct 22, 2016. 2 changed files with 76 additions and 69 deletions.
    76 changes: 76 additions & 0 deletions docker-pry-rails.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    First, add pry-rails to your Gemfile:
    https://github.com/rweng/pry-rails

    ```ruby
    gem 'pry-rails', group: :development
    ```

    Then you'll want to rebuild your Docker container to install the gems

    ```sh
    docker-compose build
    ```

    You should now be able to add a break point anywhere in your code

    ```ruby
    binding.pry
    ```

    The problem is that Docker just kind of ignores it and moves on. In order to actually halt execution and use pry as normal, you have to these options to docker-compose.yml:

    ```yaml
    app:
    tty: true
    stdin_open: true
    ```
    Source: http://stackoverflow.com/a/37264588/1042144
    The next time `binding.pry` is executed, the process should halt and display an IRB-like console on the rails server screen. But you still can't use it directly. You have to attach a terminal to the docker container.

    In order to attach to a docker container, you need to know what its ID is. Use `docker ps` to get a list of the running containers and their ids.

    ```sh
    docker ps
    ```

    Source: https://www.liquidweb.com/kb/how-to-list-and-attach-to-docker-containers/

    Then you can use the numeric ID to attach to the docker instance:

    ```sh
    docker attach 75cde1ab8133
    ```

    It may not immediately show a rails console, but start typing and it should appear. If you keep this attached, it should show the rails console prompt the next time you hit the pry breakpoint.

    Neat things you should try with pry:

    ```ruby
    show-routes
    show-models
    show-source edit # If you're in a controller, for instance
    cd # Navigate up or down class hierarchies. try `cd BasicObject`
    ls # Check out what's inside of a class - variables, etc.
    ```

    And of course you can access local variables just by typing their names.

    ```ruby
    params
    #=> <ActionController::Parameters {"controller"=>"lists", "action"=>"index"} permitted: false>
    ```

    To end your pry-rails session, you can type exit as you would exit any irb instance.

    ```ruby
    exit
    ```

    However, this does not detach the terminal from the docker container. This can be useful to continue debugging.

    Additionally, I had two terminals open, one to review the server log and one to attach to my container to use pry-rails, but docker will timeout the server log while the attached terminal continues to run the rails server process. I'm okay with that - I can just leave the terminal attached. However, if you want to detach from the container without ending the process, you need to press ctrl-p + ctrl-q.

    Source: http://stackoverflow.com/a/19689048/1042144
    69 changes: 0 additions & 69 deletions docker-pry-rails.rb
    Original file line number Diff line number Diff line change
    @@ -1,69 +0,0 @@
    # First, add pry-rails to your Gemfile
    # https://github.com/rweng/pry-rails

    gem 'pry-rails', group: :development

    # Then you'll want to rebuild your Docker container to install the gems

    docker-compose build

    # You should now be able to add a break point anywhere in your code

    binding.pry

    # The problem is that Docker just kind of ignores it and moves on.
    # In order to actually halt execution and use pry as normal, you have to
    # these options to docker-compose.yml:

    app:
    tty: true
    stdin_open: true
    # Source:
    # http://stackoverflow.com/a/37264588/1042144
    # https://github.com/docker/compose/issues/423#issuecomment-141995398

    # The next time `binding.pry` is executed, the process should halt and display
    # an IRB-like console on the rails server screen. But you still can't use it
    # directly. You have to attach a terminal to the docker container.

    # In order to attach to a docker container, you need to know what its ID is.
    # Use `docker ps` to get a list of the running containers and their ids.

    docker ps
    # Source: https://www.liquidweb.com/kb/how-to-list-and-attach-to-docker-containers/

    # Then you can use the numeric ID to attach to the docker instance:

    docker attach 75cde1ab8133

    # It may not immediately show a rails console, but start typing and it should appear.
    # If you keep this attached, it should show the rails console prompt the next time you
    # hit the pry breakpoint.

    # Neat things you should try with pry:

    show-routes
    show-models
    show-source edit # If you're in a controller, for instance

    cd # Navigate up or down class hierarchies. try `cd BasicObject`
    ls # Check out what's inside of a class - variables, etc.

    # And of course you can access local variables just by typing their names.

    params
    #=> <ActionController::Parameters {"controller"=>"lists", "action"=>"index"} permitted: false>

    # To end your pry-rails session, you can type exit as you would exit any irb instance.

    exit

    # However, this does not detach the terminal from the docker container. This can be useful to
    # continue debugging.

    # Additionally, I had two terminals open, one to review the server log and one to attach to my container
    # to use pry-rails, but docker will timeout the server log while the attached terminal
    # continues to run the rails server process. I'm okay with that - I can just leave the terminal
    # attached. However, if you want to detach from the container without ending the process, you need to
    # press ctrl-p + ctrl-q.
    # http://stackoverflow.com/a/19689048/1042144
  2. @briankung briankung revised this gist Oct 22, 2016. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions docker-pry-rails.rb
    Original file line number Diff line number Diff line change
    @@ -53,3 +53,17 @@

    params
    #=> <ActionController::Parameters {"controller"=>"lists", "action"=>"index"} permitted: false>

    # To end your pry-rails session, you can type exit as you would exit any irb instance.

    exit

    # However, this does not detach the terminal from the docker container. This can be useful to
    # continue debugging.

    # Additionally, I had two terminals open, one to review the server log and one to attach to my container
    # to use pry-rails, but docker will timeout the server log while the attached terminal
    # continues to run the rails server process. I'm okay with that - I can just leave the terminal
    # attached. However, if you want to detach from the container without ending the process, you need to
    # press ctrl-p + ctrl-q.
    # http://stackoverflow.com/a/19689048/1042144
  3. @briankung briankung created this gist Oct 22, 2016.
    55 changes: 55 additions & 0 deletions docker-pry-rails.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    # First, add pry-rails to your Gemfile
    # https://github.com/rweng/pry-rails

    gem 'pry-rails', group: :development

    # Then you'll want to rebuild your Docker container to install the gems

    docker-compose build

    # You should now be able to add a break point anywhere in your code

    binding.pry

    # The problem is that Docker just kind of ignores it and moves on.
    # In order to actually halt execution and use pry as normal, you have to
    # these options to docker-compose.yml:

    app:
    tty: true
    stdin_open: true
    # Source:
    # http://stackoverflow.com/a/37264588/1042144
    # https://github.com/docker/compose/issues/423#issuecomment-141995398

    # The next time `binding.pry` is executed, the process should halt and display
    # an IRB-like console on the rails server screen. But you still can't use it
    # directly. You have to attach a terminal to the docker container.

    # In order to attach to a docker container, you need to know what its ID is.
    # Use `docker ps` to get a list of the running containers and their ids.

    docker ps
    # Source: https://www.liquidweb.com/kb/how-to-list-and-attach-to-docker-containers/

    # Then you can use the numeric ID to attach to the docker instance:

    docker attach 75cde1ab8133

    # It may not immediately show a rails console, but start typing and it should appear.
    # If you keep this attached, it should show the rails console prompt the next time you
    # hit the pry breakpoint.

    # Neat things you should try with pry:

    show-routes
    show-models
    show-source edit # If you're in a controller, for instance

    cd # Navigate up or down class hierarchies. try `cd BasicObject`
    ls # Check out what's inside of a class - variables, etc.

    # And of course you can access local variables just by typing their names.

    params
    #=> <ActionController::Parameters {"controller"=>"lists", "action"=>"index"} permitted: false>