» Docker Commands

The Docker provider exposes some additional Vagrant commands that areuseful for interacting with Docker containers. This helps with yourworkflow on top of Vagrant so that you have full access to Dockerunderneath.

» docker-exec

vagrant docker-exec can be used to run one-off commands againsta Docker container that is currently running. If the container is not running,an error will be returned.

  1. $ vagrant docker-exec app -- rake db:migrate

The above would run rake db:migrate in the context of an app container.

Note that the "name" corresponds to the name of the VM, not the name of theDocker container. Consider the following Vagrantfile:

  1. Vagrant.configure(2) do |config|
  2. config.vm.provider "docker" do |d|
  3. d.image = "consul"
  4. end
  5. end

This Vagrantfile will start the official Docker Consul image. However, theassociated Vagrant command to docker-exec into this instance is:

  1. $ vagrant docker-exec -it -- /bin/sh

In particular, the command is actually:

  1. $ vagrant docker-exec default -it -- /bin/sh

Because "default" is the default name of the first defined VM. In amulti-machine Vagrant setup as shown below, the "name" attribute correspondsto the name of the VM, not the name of the container:

  1. Vagrant.configure do |config|
  2. config.vm.define "web" do
  3. config.vm.provider "docker" do |d|
  4. d.image = "nginx"
  5. end
  6. end
  7. config.vm.define "consul" do
  8. config.vm.provider "docker" do |d|
  9. d.image = "consul"
  10. end
  11. end
  12. end

The following command is invalid:

  1. # Not valid
  2. $ vagrant docker-exec -it nginx -- /bin/sh

This is because the "name" of the VM is "web", so the command is actually:

  1. $ vagrant docker-exec -it web -- /bin/sh

For this reason, it is recommended that you name the VM the same as thecontainer. In the above example, it is unambiguous that the command to enterthe Consul container is:

  1. $ vagrant docker-exec -it consul -- /bin/sh

» docker-logs

vagrant docker-logs can be used to see the logs of a running container.Because most Docker containers are single-process, this is used to seethe logs of that one process. Additionally, the logs can be tailed.

» docker-run

vagrant docker-run can be used to run one-off commands againsta Docker container. The one-off Docker container that is started sharesall the volumes, links, etc. of the original Docker container. Anexample is shown below:

  1. $ vagrant docker-run app -- rake db:migrate

The above would run rake db:migrate in the context of an app container.