Vagrant + Docker Provider+ Windows

adkarigar
4 min readJun 22, 2020

--

windows 10 + cygwin
dockertoolbox ( docker-machine )

lets create the workspace to spinoff vagrant with docker provider

lets create two files here
1. DockerHostVagrantfile
2. Vagrantfile

DockerHostVagrantfile

vagrantfile which defines docker host VM

now to use this above VM, as a Docker Host instead of a default adding two lines to the a.vm.provider block in the original Vagrantfile

specifying custom dockerhost

configuring a custom docker host , we can specify custom forwarded ports
This can be done by adding 1 line in DockerHostVagrantfile

config.vm.network “forwarded_port”,
guest: 8080, host: 8080

$ vagrant upCommand: ["docker", "version", {:notify=>[:stdout, :stderr]}]                                                                                    

Stderr: error during connect: Get https://192.168.99.108:2376/v1.40/version: read tcp 192.168.43.101:52719->192.168.99.108:2376: wsarecv: An exis
ting connection was forcibly closed by the remote host.

this is the error i got, when trying to do vagrant up
this was solved by starting DockerToolBox, thats where 192.168.99.108 is coming from ( default doker host created from docker-machine )

Bringing machine 'nginx_demo' up with 'docker' provider...                                                                                       
==> nginx_demo: Creating and configuring docker networks...
==> nginx_demo: Creating the container...
nginx_demo: Name: nginx_demo
nginx_demo: Image: nginx
nginx_demo: Volume: D:/vagrant/second_example/ubuntu:/vagrant
nginx_demo: Port: 80:80
A Docker command executed by Vagrant didn't complete successfully!
The command run along with the output from the command is shown
below.

Command: ["docker", "run", "--name", "nginx_demo", "-d", "-p", "80:80", "-v", "//d/vagrant/second_example/ubuntu:/vagrant",
"nginx", {:notify=>[:stdout, :stderr]}]

Stderr: Unable to find image 'nginx:latest' locally
C:\Program Files\Docker Toolbox/docker.EXE: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while wa
iting for connection (Client.Timeout exceeded while awaiting headers).
See 'C:\Program Files\Docker Toolbox/docker.EXE run --help'.

again i encountered this above error, this was basically due to proxy setup which was already there ( in my case i had to remove the proxy, as i was connected to my wifi )

after this the container started successfully on my docker-host ( dockertoolbox )

$ vagrant up                           

Bringing machine 'nginx_demo' up with 'docker' provider...
==> nginx_demo: Creating and configuring docker networks...
==> nginx_demo: Creating the container...
nginx_demo: Name: nginx_demo
nginx_demo: Image: nginx
nginx_demo: Volume: D:/vagrant/second_example/ubuntu:/vagrant
nginx_demo: Port: 80:80
nginx_demo:
nginx_demo: Container created: 43d237ce9aac9be7
==> nginx_demo: Enabling network interfaces...
==> nginx_demo: Starting container...
==> nginx_demo: Provisioners will not be run since container doesn't support SSH.

the above error is because:
1. we are running docker as a provider ,not VM as a provider.
2. vagrant flow is broken as you cannot ssh into docker container.

solution for the above is to run the command using “vagrant docker-run” .

status of the provider can be checked using the following command

$ vagrant global-statusid       name       provider   state    directory                                                                                                
----------------------------------------------------------------
b5d34c3 nginx_demo docker running D:/Vagrant/second_example

some commands to verify the container status

$ vagrant docker-run nginx_demo -- nginx -v         

==> nginx_demo: Creating and configuring docker networks...
==> nginx_demo: Creating the container...
nginx_demo: Name: nginx_demo_1592855138
nginx_demo: Image: nginx
nginx_demo: Cmd: nginx -v
nginx_demo: Volume: D:/vagrant/second_example/ubuntu:/vagrant
nginx_demo:
nginx_demo: Container is starting. Output will stream in below...
nginx_demo:
nginx_demo: /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_demo: /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_demo: /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_demo: 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_demo: 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx_demo: /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_demo: /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_demo: nginx version: nginx/1.19.0

to verify the nginx , you can visit it in browser
http://Docker_host_Ip:80
in my case its
http://192.168.99.108:80 or

$ curl 192.168.99.108      

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

for logs

$ vagrant docker-logs -f               

==> nginx_demo: /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
==> nginx_demo: /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
==> nginx_demo: /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
==> nginx_demo: 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
==> nginx_demo: 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
==> nginx_demo: /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
==> nginx_demo: /docker-entrypoint.sh: Configuration complete; ready for start up
==> nginx_demo: 192.168.99.1 - - [22/Jun/2020:18:23:11 +0000] "GET / HTTP/1.1" 200 612

thats all folks, take it easy!!!

--

--

No responses yet