Skip to content

Instantly share code, notes, and snippets.

@aloucas
Created September 28, 2017 08:45
Show Gist options
  • Save aloucas/68b4d825eb22d4965746ec4ce218f411 to your computer and use it in GitHub Desktop.
Save aloucas/68b4d825eb22d4965746ec4ce218f411 to your computer and use it in GitHub Desktop.
Rails binding.pry usage with docker
  1. Initially install your pry-rails to group development on your apps Gemfile
group :development do
  # ..
  gem 'pry-rails'
  # ..
end
  1. Then make sure that your docker-compose.yml contains in the app container the following two:
app:
  ..
  ..
  tty: true
  stdin_open: true
  1. Add binding.pry to the desired place you want to have a look on your rails code:
def index
  binding.pry
end
  1. Run your docker app container and get the container id
docker-compose up app

and open a seperate terminal to run docker ps and get the container id:

docker ps

and you will get something like the following:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
e5410d059f31        mariadb:10.1        "docker-entrypoint..."   13 days ago         Up 13 days          3306/tcp                           msdsecs_mariadb_1
2557eef7f95c        redis               "docker-entrypoint..."   13 days ago         Up 13 days          6379/tcp                           msdsecs_redis_1
5fd1e9f0d850        msdsadd_sidekiq     "/app/docker_helpe..."   7 weeks ago         Up 42 hours         3000/tcp, 0.0.0.0:6666->6666/tcp   msdsadd_sidekiq_1
a38aaff4b63e        mariadb:10.1        "docker-entrypoint..."   3 months ago        Up 42 hours         3306/tcp                           msdsadd_mariadb_1
f2c0047944dd        redis               "docker-entrypoint..."   3 months ago        Up 42 hours         6379/tcp                           msdsadd_redis_1

With container id in hand, you can attach the terminal to the docker instance:

docker attach e5410d059f31

where e5410d059f31 is the container id of the app. You will get your pry on the attached terminal.

@neohunter
Copy link

You can do one single command:

docker attach $(docker-compose ps -q app)

@neohunter
Copy link

You can also do:

 docker attach $(docker-compose ps -q NAME_OF_CONTAINER)          

@andersonreinkordt
Copy link

I have been recently having an issue where the pry prompt just hangs after entering a command while attached. Anyone else have this issue?

@bobber205
Copy link

@andersonreinkordt Same issue! No input recognized it seems

@hienbx-0488
Copy link

@andersonreinkordt same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment