Micah
Micah

Reputation: 116050

Debugging rails app running docker with vagrant

I'm trying to figure out the best development workflow with vagrant and docker running a rails app. In my dockerfile I have this:

FROM quirky/rails:latest

RUN mkdir /opt/app
WORKDIR /opt/app

# Install gems
ADD ./Gemfile /opt/app/Gemfile
ADD ./Gemfile.lock /opt/app/Gemfile.lock
RUN bundle install

# Instal npm packages
ADD ./package.json /opt/app/package.json
RUN npm install

# Expose directories and ports
VOLUME /opt/app
EXPOSE 3000

# Run the web server
WORKDIR /opt/app
CMD rm -f /opt/app/tmp/pids/server.pid && bundle exec rails s

My Vagrantfile looks like this:

 config.vm.define "app" do |app|
    app.vm.provider "docker" do |d|
      d.build_dir = "."
      d.link "db:db"
      d.link "redis:redis"
      d.link "solr:solr"
      d.volumes = ["/app:/opt/app"]
      d.ports = ["3000:3000"]
      d.vagrant_vagrantfile = "./docker/Vagrantfile"
      d.remains_running = true
    end
  end

  config.vm.define "db" do |db|
    db.vm.provider "docker" do |d|
      d.image = "paintedfox/postgresql"
      d.name = "db"
      d.env = {USER: "vagrant", PASS: "password"}
      d.vagrant_vagrantfile = "./docker/Vagrantfile"
    end
  end

  config.vm.define "redis" do |redis|
    redis.vm.provider "docker" do |d|
      d.image = "dockerfile/redis"
      d.name = "redis"
      d.ports = ["6379:6379"]
      d.vagrant_vagrantfile = "./docker/Vagrantfile"
    end
  end

  config.vm.define "solr" do |solr|
    solr.vm.provider "docker" do |d|
      d.image = "quirky/solr"
      d.name = "solr"
      d.ports = ["8080:8080"]
      d.vagrant_vagrantfile = "./docker/Vagrantfile"
    end
  end

Typically if I want to debug something I just stick a debugger statement in the code and I'm running it as a local process and it just hits the breakpoint and brings up pry or whatever the debugger console is. How does this work inside of a container inside vagrant?

This is how I start my dev environment:

vagrant up app --provider=docker

It launches it in the background. There doesn't appear to be a way to launch it and attach to it. Am I missing a command or a flag I can pass in to vagrant?

Upvotes: 3

Views: 971

Answers (2)

Yser
Yser

Reputation: 2106

You are looking for docker exec or nsenter [1]. With one of these tools you can log into the container without SSH and check your logs.

If you want to debug vagrant creating and running the docker-container you can append the --debug flag like so:

vagrant up app --provider=docker --debug

But this won't give you any debug-info from your Vagrantfile directly. If you still want to get debug-messages out of your vagrantfile I recommend you to read about vagrants UI class.

PS: Maybe you simply want puts statements like so: puts "I'm here!"?

PPS: If you want to stick with vagrant and SSH the has_ssh value and a SSH-Server in the container is the way to go.

nsenter/docker exec

Upvotes: 1

jallen7usa
jallen7usa

Reputation: 3198

Have you tried the has_ssh option for the Vagrand Docker provider? It states that:

If true, then Vagrant will support SSH with the container. This allows vagrant ssh to work, provisioners, etc. This defaults to false

As an aside, I haven't tried this myself. I'm using Docker with a CoreOS image and running docker containers manually (with provisioning).

Upvotes: 0

Related Questions