当前位置: 首页 > 知识库问答 >
问题:

Vagrant ssh到Docker容器

澹台逸明
2023-03-14

我将Drupal作为Docker容器运行在Vagrant box boot2docker(在Windows 8.1上)中:

Vagrantfile(我的Docker容器)

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.provider "docker" do |docker|
    docker.vagrant_vagrantfile = "host/Vagrantfile"
    docker.image = "drupal"
    docker.ports = ['80:80']
    docker.name = 'drupal-container'
  end
  config.vm.synced_folder ".", "/vagrant", type: "smb", disabled: true
end

主机/VagrantFile(主机)

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.hostname = "docker-host"
  config.vm.box = "hashicorp/boot2docker"
  config.vm.network "forwarded_port", guest: 80, host: 8080
end

我只需在Docker容器的目录中调用vagrant up来运行它(和主机):

$ vagrant up
Bringing machine 'default' up with 'docker' provider...
==> default: Docker host is required. One will be created if necessary...
    default: Vagrant will now create or start a local VM to act as the Docker
    default: host. You'll see the output of the `vagrant up` for this VM below.
    default:
    default: Importing base box 'hashicorp/boot2docker'...
    default: Matching MAC address for NAT networking...
    default: Checking if box 'hashicorp/boot2docker' is up to date...
    default: Setting the name of the VM: boot2docker_default_1463064065066_29287
    default: Clearing any previously set network interfaces...
    default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Forwarding ports...
    default: 2375 (guest) => 2375 (host) (adapter 1)
    default: 80 (guest) => 8080 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
    default: Running 'pre-boot' VM customizations...
    default: Booting VM...
    default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: docker
    default: SSH auth method: password
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Machine booted and ready!
GuestAdditions versions on your host (5.0.20) and guest (4.3.28 r100309) do not match.
The guest's platform ("tinycore") is currently not supported, will try generic Linux method...
Copy iso file C:\Program Files/Oracle/VirtualBox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.0.20 - guest version is 4.3.28 r100309
mkdir: can't create directory '/tmp/selfgz98932600': No such file or directory
Cannot create target directory /tmp/selfgz98932600
You should try option --target OtherDirectory
An error occurred during installation of VirtualBox Guest Additions 5.0.20. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
    default: Setting hostname...
==> default: Warning: When using a remote Docker host, forwarded ports will NOT be
==> default: immediately available on your machine. They will still be forwarded on
==> default: the remote machine, however, so if you have a way to access the remote
==> default: machine, then you should be able to access those ports there. This is
==> default: not an error, it is only an informational message.
==> default: Creating the container...
    default:   Name: drupal-container
    default:  Image: drupal
    default:   Port: 80:80
    default:
    default: Container created: d12499a52f3d3f27
==> default: Starting container...
==> default: Provisioners will not be run since container doesn't support SSH.

现在我喜欢从同一个目录连接到容器:

$ vagrant ssh
==> default: SSH will be proxied through the Docker virtual machine since we're
==> default: not running Docker natively. This is just a notice, and not an error.
==> default: The machine you're attempting to SSH into is configured to use
==> default: password-based authentication. Vagrant can't script entering the
==> default: password for you. If you're prompted for a password, please enter
==> default: the same password you have configured in the Vagrantfile.
docker@127.0.0.1's password: tcuser
ssh: connect to host 172.17.0.1 port 22: Connection refused
Connection to 127.0.0.1 closed.
$ vagrant global-status
id       name    provider   state   directory
----------------------------------------------------------------------------------------------
e4da5ae  default virtualbox running c:/my-project-path/host/boot2docker
98ef037  default docker     running c:/my-project-path

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
$ vagrant ssh 98ef037
==> default: SSH will be proxied through the Docker virtual machine since we're
==> default: not running Docker natively. This is just a notice, and not an error.
==> default: The machine you're attempting to SSH into is configured to use
==> default: password-based authentication. Vagrant can't script entering the
==> default: password for you. If you're prompted for a password, please enter
==> default: the same password you have configured in the Vagrantfile.
docker@127.0.0.1's password: tcuser
ssh: connect to host 172.17.0.3 port 22: Connection refused
Connection to 127.0.0.1 closed.

如果我将docker.has_ssh=true添加到Vagrantfile(我不知道是否需要它,因为我可以在没有它的情况下调用Vagrant ssh):

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.provider "docker" do |docker|
    docker.vagrant_vagrantfile = "host/Vagrantfile"
    docker.image = "drupal"
    docker.ports = ['80:80']
    docker.name = 'drupal-container'
    docker.has_ssh = true
  end
  config.vm.synced_folder ".", "/vagrant", type: "smb", disabled: true
end

那么我无法运行/重新加载我的容器,因为它会无限期地等待机器启动:

$ vagrant reload
==> default: Stopping container...
==> default: Starting container...
==> default: Waiting for machine to boot. This may take a few minutes...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

那么如何使用vagrant ssh连接到Docker容器呢?

注意:我可以先连接到主机,然后调用它上的docker

$ cd host
$ vagrant ssh
==> default: The machine you're attempting to SSH into is configured to use
==> default: password-based authentication. Vagrant can't script entering the
==> default: password for you. If you're prompted for a password, please enter
==> default: the same password you have configured in the Vagrantfile.
docker@127.0.0.1's password: tcuser
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.7.0, build master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015
Docker version 1.7.0, build 0baf609
docker@docker-host:~$ sudo docker exec -t -i drupal-container /bin/bash
root@d12499a52f3d:/var/www/html#

但这是一个不切实际的变通办法。我只是喜欢在容器的目录中调用Vagrant ssh来连接到容器(常见的Vagrant工作流)。

共有1个答案

陈宏胜
2023-03-14

您运行的是docker容器,而不是VM作为您的提供者,因此,由于无法ssh到docker容器中,所以vagrant工作流中断。

使用docker提供程序时,可以使用vagrant docker-run运行命令;参见文档

如果您直接运行docker,您将不能直接使用ssh,有一些方法可以解决这一问题,但是vagrant作为提供者的抽象不能使它成为一个简单的ssh命令,而且它不是官方支持。

 类似资料:
  • 我拉出了mysql docker映像,并用命令运行容器: 此时,springBoot在本地工作。它与MySQL连接: 现在,我希望springBoot应用程序也在单独的docker容器上,在同一服务器上。为此,我使用了DockerFile: 同样,它无法连接到数据库:

  • 我的数据库连接是用下面的类处理的: 这是我的spring boot控制器 下面是我的dockerfile: 我读了这里和这里提到的解决方案。也阅读了一些教程,但我无法将这些解决方案中的任何一个适合我的代码。在我的代码中,我应该在哪里更改?

  • 我是docker的新手。我在windows 10上使用docker for windows。我创建了一个docker容器应用程序(spring boot),该应用程序需要与rabbitmq通信-这也是在一个容器中。他们都使用相同的docker网络(默认网桥网络),但当我尝试连接到rabbitmq(端口5672)时,连接被拒绝。我使用的是容器IP(rabbitmq为172.17.0.2)。如果我在d

  • 问题内容: 我已经在Docker上进行了几天的实验,并逐渐喜欢上它。但是,有些事情仍然使我难以理解。到目前为止,这是我所拥有的 创建低占用空间的Ubuntu 14.04映像 为Apache映像创建Dockerfile 为PHP5创建Dockerfile 为memcached创建Dockerfile并构建映像 用memcached启动Docker容器 用Apache启动Docker容器并将其链接到之

  • 我的服务器上有一个运行neo4j的docker容器,并且设置了自动备份来备份数据库。我试图测试从备份中还原数据库,但我不知道如何在docker容器中还原数据库。 我正在尝试使用 我从neo4j文档中读到,您必须关闭neo4j才能恢复备份,但我非常肯定在容器中关闭neo4j将关闭我的容器(容器是使用启动的)。是否有一种方法可以在不关闭neo4j的情况下还原备份,或者可以在不关闭容器的情况下关闭neo

  • 目前情况: 我通过执行以下命令创建了一个新的Cassandra映像“Cassandra”和一个名为“container-node”的容器: Docker pull Cassandra Docker images grep Cassandra Docker run-d--name cassandra-node--publish 9042:9042 cassandra 连接到容器:docker exe