blob: 08a5f4159efa0aafda4e1b95e35bace71a3a61c7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<h1 id="docker-commands"> Docker Commands </h1> <p>The Docker provider exposes some additional Vagrant commands that are useful for interacting with Docker containers. This helps with your workflow on top of Vagrant so that you have full access to Docker underneath.</p> <h3 id="docker-exec"> docker-exec </h3> <p><code>vagrant docker-exec</code> can be used to run one-off commands against a Docker container that is currently running. If the container is not running, an error will be returned.</p> <div class="highlight"><pre class="highlight shell" data-language="shell">$ vagrant docker-exec app -- rake db:migrate
</pre></div>
<p>The above would run <code>rake db:migrate</code> in the context of an <code>app</code> container.</p> <p>Note that the "name" corresponds to the name of the VM, <strong>not</strong> the name of the Docker container. Consider the following Vagrantfile:</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">Vagrant.configure(2) do |config|
config.vm.provider "docker" do |d|
d.image = "consul"
end
end
</pre></div>
<p>This Vagrantfile will start the official Docker Consul image. However, the associated Vagrant command to <code>docker-exec</code> into this instance is:</p> <div class="highlight"><pre class="highlight shell" data-language="shell">$ vagrant docker-exec -it -- /bin/sh
</pre></div>
<p>In particular, the command is actually:</p> <div class="highlight"><pre class="highlight shell" data-language="shell">$ vagrant docker-exec default -it -- /bin/sh
</pre></div>
<p>Because "default" is the default name of the first defined VM. In a multi-machine Vagrant setup as shown below, the "name" attribute corresponds to the name of the VM, <strong>not</strong> the name of the container:</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">Vagrant.configure do |config|
config.vm.define "web" do
config.vm.provider "docker" do |d|
d.image = "nginx"
end
end
config.vm.define "consul" do
config.vm.provider "docker" do |d|
d.image = "consul"
end
end
end
</pre></div>
<p>The following command is invalid:</p> <div class="highlight"><pre class="highlight shell" data-language="shell"># Not valid
$ vagrant docker-exec -it nginx -- /bin/sh
</pre></div>
<p>This is because the "name" of the VM is "web", so the command is actually:</p> <div class="highlight"><pre class="highlight shell" data-language="shell">$ vagrant docker-exec -it web -- /bin/sh
</pre></div>
<p>For this reason, it is recommended that you name the VM the same as the container. In the above example, it is unambiguous that the command to enter the Consul container is:</p> <div class="highlight"><pre class="highlight shell" data-language="shell">$ vagrant docker-exec -it consul -- /bin/sh
</pre></div>
<h3 id="docker-logs"> docker-logs </h3> <p><code>vagrant docker-logs</code> can be used to see the logs of a running container. Because most Docker containers are single-process, this is used to see the logs of that one process. Additionally, the logs can be tailed.</p> <h3 id="docker-run"> docker-run </h3> <p><code>vagrant docker-run</code> can be used to run one-off commands against a Docker container. The one-off Docker container that is started shares all the volumes, links, etc. of the original Docker container. An example is shown below:</p> <div class="highlight"><pre class="highlight shell" data-language="shell">$ vagrant docker-run app -- rake db:migrate
</pre></div>
<p>The above would run <code>rake db:migrate</code> in the context of an <code>app</code> container.</p><div class="_attribution">
<p class="_attribution-p">
© 2010–2018 Mitchell Hashimoto<br>Licensed under the MPL 2.0 License.<br>
<a href="https://www.vagrantup.com/docs/docker/commands.html" class="_attribution-link">https://www.vagrantup.com/docs/docker/commands.html</a>
</p>
</div>
|