Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何在OSX(boot2docker)上获得与docker容器的ssh连接_Macos_Ssh_Docker_Boot2docker_Linux Containers - Fatal编程技术网

Macos 如何在OSX(boot2docker)上获得与docker容器的ssh连接

Macos 如何在OSX(boot2docker)上获得与docker容器的ssh连接,macos,ssh,docker,boot2docker,linux-containers,Macos,Ssh,Docker,Boot2docker,Linux Containers,我在OSX上使用docker 我想从终端获得一个Ssh连接到一个正在运行的容器中 但我不能这么做:( 我认为这是因为Docker正在虚拟机中运行。要启用ssh到VM中运行的容器,必须执行以下几项操作: 在容器()中安装并运行sshd。sshd默认情况下不存在,因为容器通常只运行一个进程,尽管它们可以运行任意多个进程 EXPOSE作为创建映像的一部分的端口,通常为22,以便在运行容器时,守护进程连接到容器内部的EXPOSE'd端口,并且可以在容器外部显示某些内容 运行容器时,需要决定如何映射该端口

我在OSX上使用docker

我想从终端获得一个Ssh连接到一个正在运行的容器中

但我不能这么做:(


我认为这是因为Docker正在虚拟机中运行。

要启用ssh到VM中运行的容器,必须执行以下几项操作:

  • 在容器()中安装并运行
    sshd
    sshd
    默认情况下不存在,因为容器通常只运行一个进程,尽管它们可以运行任意多个进程
  • EXPOSE
    作为创建映像的一部分的端口,通常为22,以便在运行容器时,守护进程连接到容器内部的
    EXPOSE
    'd端口,并且可以在容器外部显示某些内容
  • 运行容器时,需要决定如何映射该端口。可以让Docker自动映射,也可以显式映射。我建议显式地执行:
    Docker run-p 42222:22…
    将VM上的端口42222映射到容器中的端口22
  • 向虚拟机添加端口映射以向主机公开端口。例如,当虚拟机未运行时,可以添加如下映射:
    VBoxManage-modifyvm“boot2docker-VM”--natpf1“containerssh,tcp,,42222,,42222”
  • 然后从主机上,您应该能够通过ssh连接到主机上的端口42222,以到达容器的ssh守护进程

    以下是我执行上述步骤时发生的情况:

    $ VBoxManage modifyvm "boot2docker-vm" --natpf1 "containerssh,tcp,,42222,,42222"
    $ ./boot2docker start
    [2014-04-11 12:07:35] Starting boot2docker-vm...
    [2014-04-11 12:07:55] Started.
    $ docker run -d -p 42222:22 dhrp/sshd
    Unable to find image 'dhrp/sshd' (tag: latest) locally
    Pulling repository dhrp/sshd
    2bbfe079a942: Download complete 
    c8a2228805bc: Download complete 
    8dbd9e392a96: Download complete 
    11d214c1b26a: Download complete 
    27cf78414709: Download complete 
    b750fe79269d: Download complete 
    cf7e766468fc: Download complete 
    082189640622: Download complete 
    fa822d12ee30: Download complete 
    1522e919ec9f: Download complete 
    fa594d99163a: Download complete 
    1bd442970c79: Download complete 
    0fda9de88c63: Download complete 
    86e22a5fdce6: Download complete 
    79d05cb13124: Download complete 
    ac72e4b531bc: Download complete 
    26e4b94e5a13b4bb924ef57548bb17ba03444ca003128092b5fbe344110f2e4c
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                   NAMES
    26e4b94e5a13        dhrp/sshd:latest    /usr/sbin/sshd -D      6 seconds ago       Up 3 seconds        0.0.0.0:42222->22/tcp   loving_einstein     
    $ ssh root@localhost -p 42222
    The authenticity of host '[localhost]:42222 ([127.0.0.1]:42222)' can't be established.
    RSA key fingerprint is ....
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '[localhost]:42222' (RSA) to the list of known hosts.
    root@localhost's password: screencast
    Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.12.1-tinycore64 x86_64)
    
     * Documentation:  https://help.ubuntu.com/
    
    The programs included with the Ubuntu system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
    applicable law.
    
    root@26e4b94e5a13:~# exit
    logout
    

    这样显示SSH->本地主机42222 > VM端口42222 >容器端口22。

    < P>如果您只想进入运行容器,您可以考虑使用。这里是一个简单的BASH脚本(),您可以使用它进入坞箱。将它保存在您的代码> $PATH < /代码>中,作为DOCSKER输入和<代码> CHMOD+X

    !/bin/bash
    set-e
    #检查N中心。如果找不到,请安装它
    boot2docker ssh'[-f/var/lib/boot2docker/nsenter]| | docker run--rm-v/var/lib/boot2docker/:/target jpetazzo/nsenter'
    #如果未指定命令,请使用bash
    args=$@
    如果[$#=1]];则
    args+=(/bin/bash)
    fi
    boot2docker ssh-tsudo/var/lib/boot2docker/docker输入“${args[@]}”
    

    然后,您可以运行
    docker enter 89af3d
    (或您想要输入的任何配置)

    这是Michael答案的一个稍加修改的变体,只需要将您想要输入的容器命名(APPNAME):


    Docker已将
    Docker exec
    命令添加到Docker 1.3.0。您可以使用以下命令连接到正在运行的容器:

    docker exec -it <container id> /bin/bash
    
    docker exec-it/bin/bash
    

    这将连接到正在运行的容器上的bash提示符。

    我已经对运行在相同操作系统Docker 18.09.2主机上的Ubuntu 16.04映像进行了测试,它还应该适用于boot2Docker,只需稍作修改

    塑造形象。 在后台容器中运行它(您的用户可能是root):

    $docker运行-ditu

    用外壳将其连接到:

    $docker exec-it/bin/bash

    安装openssh服务器(
    sudo
    仅当用户不是root用户时才需要,boot2Docker的命令可能不同):

    $sudo apt get安装-y openssh服务器

    运行它:

    $sudo服务ssh启动

    (以下步骤是可选的,如果您的用户有密码,您可以跳过它并在每个ssh连接上提供密码)

    在客户端主机上创建RSA密钥:

    $ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/youruser/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/youruser/.ssh/id_rsa.
    Your public key has been saved in /home/youruser/.ssh/id_rsa.pub.
    
    在docker映像上,创建一个目录
    $HOME/.ssh

    $ cd
    $ mkdir .ssh && cd .ssh
    $ vi authorized_keys
    
    将客户端计算机上的
    $HOME/.ssh/id_rsa.pub
    内容复制并粘贴到docker映像上的
    授权密钥
    ,然后保存文件

    (可选步骤结束)

    记下图像的IP地址:

    $ cat /etc/hosts
    127.0.0.1   localhost
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    172.17.0.2  63448863ac39
    ^^^^^^^^^^ this
    
    现在,来自客户端主机的连接应该是有效的:

    $ ssh 172.17.0.2
    Enter passphrase for key '/home/youruser/.ssh/id_rsa': 
    Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-46-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    Last login: Fri Apr  5 09:50:30 2019 from 172.17.0.1
    

    当然,您可以在docker文件中以非交互方式应用上述过程。

    也可以使用docker compose作为
    docker compose运行/bin/bash
    $ ssh 172.17.0.2
    Enter passphrase for key '/home/youruser/.ssh/id_rsa': 
    Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-46-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    Last login: Fri Apr  5 09:50:30 2019 from 172.17.0.1