hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.31k stars 4.43k forks source link

Docker provider: cannot 'vagrant ssh' when not using a Docker host VM #3799

Closed FooBarWidget closed 10 years ago

FooBarWidget commented 10 years ago

I'm on Ubuntu 14.04 and Vagrant 1.6.2.

Given this Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.provider :docker do |d, override|
    d.image   = "phusion/baseimage:0.9.9"
    d.name    = "vagrant-docker-test"
    d.cmd     = ["/sbin/my_init", "--enable-insecure-key"]
    d.has_ssh = true
  end

  config.ssh.username = "root"
  config.ssh.private_key_path = "insecure_key"
end

Vagrant ssh does not work:

$ vagrant up --provider docker
Bringing machine 'default' up with 'docker' provider...
==> default: Creating the container...
    default:   Name: vagrant-docker-test
    default:  Image: phusion/baseimage:0.9.9
    default:    Cmd: /sbin/my_init --enable-insecure-key
    default: Volume: /home/hongli/Projects/vagrant-docker:/vagrant
    default:   Port: 2222:22
    default:  
    default: Container created: ac2cfd96cc0b901f
==> default: Starting container...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 172.17.0.2:
    default: SSH username: root
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
$ vagrant ssh
Bad port ''

If I set d.force_host_vm = true then vagrant ssh works.

stefancocora commented 10 years ago

I can confirm this is happening for me too when running vagrant against docker natively. It looks like vagrant is not picking up on the ports itself is setting up for port forwarding, runs the ssh command with -p '' ( see verbose vagrant output below )

Hardware and software specs:

§ uname -a
Linux thoth2 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
§ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04 LTS
Release:    14.04
Codename:   trusty
§ vagrant --version
Vagrant 1.6.2
§ docker --version
Docker version 0.11.1, build fb99f99
§ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]

Vagrantfile:

Vagrant.configure("2") do |config|
    config.vm.define "phusion" do |v|
        v.vm.provider "docker" do |d|
          d.cmd = ["/sbin/my_init", "--enable-insecure-key"]
          d.image = "phusion/baseimage"
          d.name = 'dockerizedvm'
          d.has_ssh = true
          #d.force_host_vm = true
        end
        v.ssh.username = 'root'
        v.ssh.private_key_path = 'phusion.key'
        v.vm.provision "shell", inline: "echo hello"
        #v.vm.synced_folder "./keys", "/vagrant"
    end
end

Creating the container successfully:

§ vagrant up --provider=docker
Bringing machine 'phusion' up with 'docker' provider...
==> phusion: Creating the container...
    phusion:   Name: dockerizedvm
    phusion:  Image: phusion/baseimage
    phusion:    Cmd: /sbin/my_init --enable-insecure-key
    phusion: Volume: /tmp/phusion-with-ssh:/vagrant
    phusion:   Port: 2222:22
    phusion:  
    phusion: Container created: b58b499b8c5ed12a
==> phusion: Starting container...
==> phusion: Waiting for machine to boot. This may take a few minutes...
    phusion: SSH address: 172.17.0.2:
    phusion: SSH username: root
    phusion: SSH auth method: private key
    phusion: Warning: Connection timeout. Retrying...
==> phusion: Machine booted and ready!
==> phusion: Running provisioner: shell...
    phusion: Running: inline script
==> phusion: stdin: is not a tty
==> phusion: hello

SSH-ing

§ vagrant ssh 
Bad port ''

Verbose logging whilst SSH-ing

§ VAGRANT_LOG=INFO vagrant ssh
 INFO global: Vagrant version: 1.6.2
 INFO global: Ruby version: 2.0.0
 INFO global: RubyGems version: 2.0.14
 INFO global: VAGRANT_EXECUTABLE="/opt/vagrant/bin/../embedded/gems/gems/vagrant-1.6.2/bin/vagrant"
 INFO global: VAGRANT_LOG="INFO"
 INFO global: VAGRANT_INSTALLER_EMBEDDED_DIR="/opt/vagrant/bin/../embedded"
 INFO global: VAGRANT_INSTALLER_VERSION="2"
 INFO global: VAGRANT_DETECTED_OS="Linux"
 INFO global: VAGRANT_INSTALLER_ENV="1"
 INFO global: VAGRANT_INTERNAL_BUNDLERIZED="1"
 INFO global: Plugins:
 INFO global:   - builder = 3.2.2
 INFO global:   - bundler = 1.6.2
 INFO global:   - excon = 0.33.0
 INFO global:   - formatador = 0.2.4
 INFO global:   - mime-types = 2.2
 INFO global:   - net-ssh = 2.9.0
 INFO global:   - net-scp = 1.1.2
 INFO global:   - fog-core = 1.21.1
 INFO global:   - multi_json = 1.10.0
 INFO global:   - fog-json = 1.0.0
 INFO global:   - fog-brightbox = 0.0.2
 INFO global:   - nokogiri = 1.5.11
 INFO global:   - fog = 1.21.0
 INFO global:   - rest-client = 1.6.7
 INFO global:   - ruby-libvirt = 0.4.0
 INFO global:   - vagrant-libvirt = 0.0.15
 INFO global:   - vagrant-login = 1.0.1
 INFO global:   - vagrant-share = 1.0.1
 INFO manager: Registered plugin: NetBSD guest
 INFO manager: Registered plugin: Darwin guest
 INFO manager: Registered plugin: Funtoo guest
 INFO manager: Registered plugin: Windows guest.
 INFO manager: Registered plugin: TinyCore Linux guest.
 INFO manager: Registered plugin: SUSE guest
 INFO manager: Registered plugin: ESXi guest.
 INFO manager: Registered plugin: Fedora guest
 INFO manager: Registered plugin: RedHat guest
 INFO manager: Registered plugin: Solaris 11 guest.
 INFO manager: Registered plugin: Debian guest
 INFO manager: Registered plugin: Linux guest.
 INFO manager: Registered plugin: PLD Linux guest
 INFO manager: Registered plugin: Solaris guest.
 INFO manager: Registered plugin: SmartOS guest.
 INFO manager: Registered plugin: OmniOS guest.
 INFO manager: Registered plugin: CoreOS guest
 INFO manager: Registered plugin: Ubuntu guest
 INFO manager: Registered plugin: Mint guest
 INFO manager: Registered plugin: Gentoo guest
 INFO manager: Registered plugin: FreeBSD guest
 INFO manager: Registered plugin: Arch guest
 INFO manager: Registered plugin: OpenBSD guest
 INFO manager: Registered plugin: kernel
 INFO manager: Registered plugin: BSD host
 INFO manager: Registered plugin: Mac OS X host
 INFO manager: Registered plugin: Windows host
 INFO manager: Registered plugin: OpenSUSE host
 INFO manager: Registered plugin: Red Hat host
 INFO manager: Registered plugin: Linux host
 INFO manager: Registered plugin: null host
 INFO manager: Registered plugin: Slackware host
 INFO manager: Registered plugin: Gentoo host
 INFO manager: Registered plugin: FreeBSD host
 INFO manager: Registered plugin: Arch host
 INFO manager: Registered plugin: NFS synced folders
 INFO manager: Registered plugin: RSync synced folders
 INFO manager: Registered plugin: SMB synced folders
 INFO manager: Registered plugin: docker-provider
 INFO manager: Registered plugin: VirtualBox provider
 INFO manager: Registered plugin: Hyper-V provider
 INFO manager: Registered plugin: ssh communicator
 INFO manager: Registered plugin: winrm communicator
 INFO manager: Registered plugin: init command
 INFO manager: Registered plugin: plugin command
 INFO manager: Registered plugin: rdp command
 INFO manager: Registered plugin: help command
 INFO manager: Registered plugin: global-status command
 INFO manager: Registered plugin: package command
 INFO manager: Registered plugin: destroy command
 INFO manager: Registered plugin: provision command
 INFO manager: Registered plugin: list-commands command
 INFO manager: Registered plugin: status command
 INFO manager: Registered plugin: up command
 INFO manager: Registered plugin: ssh command
 INFO manager: Registered plugin: ssh-config command
 INFO manager: Registered plugin: reload command
 INFO manager: Registered plugin: resume command
 INFO manager: Registered plugin: halt command
 INFO manager: Registered plugin: version command
 INFO manager: Registered plugin: box command
 INFO manager: Registered plugin: suspend command
 INFO manager: Registered plugin: CFEngine Provisioner
 INFO manager: Registered plugin: docker
 INFO manager: Registered plugin: puppet
 INFO manager: Registered plugin: file
 INFO manager: Registered plugin: chef
 INFO manager: Registered plugin: ansible
 INFO manager: Registered plugin: salt
 INFO manager: Registered plugin: shell
 INFO manager: Registered plugin: kernel
 INFO global: Loading plugins!
 INFO manager: Registered plugin: vagrant-login
 INFO manager: Registered plugin: vagrant-share
 INFO manager: Registered plugin: libvirt
 INFO vagrant: `vagrant` invoked: ["ssh"]
 INFO environment: Environment initialized (#<Vagrant::Environment:0x00000001e83180>)
 INFO environment:   - cwd: /tmp/phusion-with-ssh
 INFO environment: Home path: /home/stefan/.vagrant.d
 INFO environment: Local data path: /tmp/phusion-with-ssh/.vagrant
 INFO environment: Running hook: environment_plugins_loaded
 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: #<Vagrant::Action::Builder:0x0000000277a1a8>
 INFO environment: Running hook: environment_load
 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: #<Vagrant::Action::Builder:0x00000002874310>
 INFO cli: CLI: [] "ssh" []
 INFO loader: Set :root = #<Pathname:/tmp/phusion-with-ssh/Vagrantfile>
 INFO loader: Loading configuration in order: [:home, :root]
 INFO command: Active machine found with name phusion. Using provider: docker
 INFO environment: Getting machine: phusion (docker)
 INFO environment: Uncached load of machine.
 INFO loader: Set "21644000_machine_phusion" = [["2", #<Proc:0x00000002992058@/tmp/phusion-with-ssh/Vagrantfile:2>]]
 INFO loader: Loading configuration in order: [:home, :root, "21644000_machine_phusion"]
 INFO machine: Initializing machine: phusion
 INFO machine:   - Provider: VagrantPlugins::DockerProvider::Provider
 INFO machine:   - Box: 
 INFO machine:   - Data dir: /tmp/phusion-with-ssh/.vagrant/machines/phusion/docker
 INFO command: With machine: phusion (#<VagrantPlugins::DockerProvider::Provider:0x0000000299a5c8 @logger=#<Log4r::Logger:0x0000000299a578 @fullname="vagrant::provider::docker", @outputters=[], @additive=true, @name="docker", @path="vagrant::provider", @parent=#<Log4r::Logger:0x00000001ac9f58 @fullname="vagrant", @outputters=[#<Log4r::StderrOutputter:0x00000001b4efc8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x00000001b4ef28>, @name="stderr", @level=0, @formatter=#<Log4r::DefaultFormatter:0x00000001b61df8 @depth=7>, @out=#<IO:<STDERR>>>], @additive=true, @name="vagrant", @path="", @parent=#<Log4r::RootLogger:0x00000001ac9d78 @level=0, @outputters=[]>, @level=2, @trace=false>, @level=2, @trace=false>, @machine=#<Vagrant::Machine: phusion (VagrantPlugins::DockerProvider::Provider)>, @cap_logger=#<Log4r::Logger:0x0000000198bf60 @fullname="vagrant::capability_host::vagrantplugins::dockerprovider::provider", @outputters=[], @additive=true, @name="provider", @path="vagrant::capability_host::vagrantplugins::dockerprovider", @parent=#<Log4r::Logger:0x00000001ac9f58 @fullname="vagrant", @outputters=[#<Log4r::StderrOutputter:0x00000001b4efc8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x00000001b4ef28>, @name="stderr", @level=0, @formatter=#<Log4r::DefaultFormatter:0x00000001b61df8 @depth=7>, @out=#<IO:<STDERR>>>], @additive=true, @name="vagrant", @path="", @parent=#<Log4r::RootLogger:0x00000001ac9d78 @level=0, @outputters=[]>, @level=2, @trace=false>, @level=2, @trace=false>, @cap_host_chain=[[:docker, #<#<Class:0x00000001981560>:0x00000001ae82a0>]], @cap_args=[#<Vagrant::Machine: phusion (VagrantPlugins::DockerProvider::Provider)>], @cap_caps={:docker=>#<Vagrant::Registry:0x000000019811c8 @items={:public_address=>#<Proc:0x00000001aa52e8@/opt/vagrant/embedded/gems/gems/vagrant-1.6.2/plugins/providers/docker/plugin.rb:54>}, @results_cache={}>, :virtualbox=>#<Vagrant::Registry:0x00000001980c78 @items={:forwarded_ports=>#<Proc:0x00000001adeb60@/opt/vagrant/embedded/gems/gems/vagrant-1.6.2/plugins/providers/virtualbox/plugin.rb:27>, :nic_mac_addresses=>#<Proc:0x00000001adeac0@/opt/vagrant/embedded/gems/gems/vagrant-1.6.2/plugins/providers/virtualbox/plugin.rb:32>, :public_address=>#<Proc:0x00000001e91eb0@/opt/vagrant/embedded/gems/gems/vagrant-share-1.0.1/lib/vagrant-share.rb:39>}, @results_cache={}>, :hyperv=>#<Vagrant::Registry:0x00000001980930 @items={:public_address=>#<Proc:0x00000001aed0e8@/opt/vagrant/embedded/gems/gems/vagrant-1.6.2/plugins/providers/hyperv/plugin.rb:25>}, @results_cache={}>}>)
 INFO machine: Calling action: ssh on provider Docker (b58b499b8c5ed12a3717e1ba07b91edb7272cbe648c9bf300ab5726560d72e6f)
 INFO host: Autodetecting host type for [#<Vagrant::Environment: /tmp/phusion-with-ssh>]
 INFO host: Detected: linux!
 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: #<Vagrant::Action::Builder:0x000000020848a8>
 INFO warden: Calling IN action: #<Vagrant::Action::Builtin::Call:0x00000002799288>
 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: #<Vagrant::Action::Builder:0x000000028340d0>
 INFO warden: Calling IN action: #<Vagrant::Action::Builtin::IsState:0x00000002839ad0>
 INFO subprocess: Starting process: ["/usr/bin/docker", "ps", "-a", "-q", "--no-trunc"]
 INFO subprocess: Starting process: ["/usr/bin/docker", "ps", "-q", "--no-trunc"]
 INFO warden: Calling OUT action: #<Vagrant::Action::Builtin::IsState:0x00000002839ad0>
 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: #<Vagrant::Action::Warden:0x0000000299abb8>
 INFO warden: Calling IN action: #<Proc:0x000000029b3028@/opt/vagrant/embedded/gems/gems/vagrant-1.6.2/lib/vagrant/action/warden.rb:94 (lambda)>
 INFO warden: Calling IN action: #<Vagrant::Action::Builtin::Call:0x0000000299ab40>
 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: #<Vagrant::Action::Builder:0x000000029d6118>
 INFO warden: Calling IN action: #<Vagrant::Action::Builtin::IsState:0x000000029dbe10>
 INFO subprocess: Starting process: ["/usr/bin/docker", "ps", "-a", "-q", "--no-trunc"]
 INFO subprocess: Starting process: ["/usr/bin/docker", "ps", "-q", "--no-trunc"]
 INFO warden: Calling OUT action: #<Vagrant::Action::Builtin::IsState:0x000000029dbe10>
 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: #<Vagrant::Action::Warden:0x00000001e7b778>
 INFO warden: Calling IN action: #<Proc:0x00000001f52a20@/opt/vagrant/embedded/gems/gems/vagrant-1.6.2/lib/vagrant/action/warden.rb:94 (lambda)>
 INFO warden: Calling IN action: #<VagrantPlugins::DockerProvider::Action::PrepareSSH:0x00000001e7b660>
 INFO warden: Calling IN action: #<Vagrant::Action::Builtin::SSHExec:0x00000001e7b638>
 INFO subprocess: Starting process: ["/usr/bin/docker", "ps", "-a", "-q", "--no-trunc"]
 INFO subprocess: Starting process: ["/usr/bin/docker", "ps", "-q", "--no-trunc"]
 INFO subprocess: Starting process: ["/usr/bin/docker", "inspect", "b58b499b8c5ed12a3717e1ba07b91edb7272cbe648c9bf300ab5726560d72e6f"]
 INFO ssh: Invoking SSH: ["root@172.17.0.2", "-p", "", "-o", "Compression=yes", "-o", "DSAAuthentication=yes", "-o", "LogLevel=FATAL", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-o", "IdentitiesOnly=yes", "-i", "/tmp/phusion-with-ssh/phusion.key"]
Bad port ''
Naycon commented 10 years ago

I can confirm I have the exact same problem with a similar setup. As stefancocora wrote, it seems like the port part of the ssh command isn't set to what it should be.

fgrehm commented 10 years ago

@mitchellh I'm guessing this might have been introduced on https://github.com/mitchellh/vagrant/commit/e714720052e7e1d00b752005760b12cac7b39cfa#diff-49c2ef2b4e9cf8571ff605b1b05d8362L139 as the provider no longer returns the :port hash value, shall we do some extra stuff over here to fix this bug or do you think it should be handled by the provider object itself?

hlj commented 10 years ago

A work around is assign the ssh.port in Vagrantfile:

config.ssh.port = "22"
stefancocora commented 10 years ago

I can confirm @hlj workaround works for me too, adding v.ssh.port = 22 to my original Vagrantfile makes it possible to ssh into the docker container. Vagrantfile

Vagrant.configure("2") do |config|
    config.vm.define "phusion" do |v|
        v.vm.provider "docker" do |d|
          d.cmd = ["/sbin/my_init", "--enable-insecure-key"]
          d.image = "phusion/baseimage"
          d.name = 'dockerizedvm'
          d.has_ssh = true
          #d.force_host_vm = true
        end
        v.ssh.port = 22
        v.ssh.username = 'root'
        v.ssh.private_key_path = 'phusion.key'
        v.vm.provision "shell", inline: "echo hello"
        #v.vm.synced_folder "./keys", "/vagrant"
    end
end

Vagrant up

§ vagrant up --provider=docker
Bringing machine 'phusion' up with 'docker' provider...
==> phusion: Creating the container...
    phusion:   Name: dockerizedvm
    phusion:  Image: phusion/baseimage
    phusion:    Cmd: /sbin/my_init --enable-insecure-key
    phusion: Volume: /media/truecrypt1/Docs/vagrant/vagrant-docker/phusion-with-ssh:/vagrant
    phusion:   Port: 2222:22
    phusion:  
    phusion: Container created: 80efb417520512d7
==> phusion: Starting container...
==> phusion: Waiting for machine to boot. This may take a few minutes...
    phusion: SSH address: 172.17.0.2:22
    phusion: SSH username: root
    phusion: SSH auth method: private key
    phusion: Warning: Connection refused. Retrying...
==> phusion: Machine booted and ready!
==> phusion: Running provisioner: shell...
    phusion: Running: inline script
==> phusion: stdin: is not a tty
==> phusion: hello
§ docker ps
CONTAINER ID        IMAGE                     COMMAND                CREATED             STATUS              PORTS                  NAMES
80efb4175205        phusion/baseimage:0.9.9   /sbin/my_init --enab   11 seconds ago      Up 10 seconds       0.0.0.0:2222->22/tcp   dockerizedvm

SSH works

§ vagrant ssh
Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.8.0-35-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
root@80efb4175205:~# 
Naycon commented 10 years ago

And I can confirm that @fgrehm's fix above works, at least when I manually introduce the same fixes directly in to the ruby file in my installation.

mitchellh commented 10 years ago

Fixed by #3807