如何从Windows连接到Docker容器内的SSHD?

时间:2017-03-06 18:38:11

标签: windows docker sshd

我有一个Ruby on Rails环境,我正在将其转换为在Docker中运行。这主要是因为开发机器是Windows笔记本电脑而服务器不是。我主要启动并运行Docker容器,现在我想连接RubyMine调试器。为此,建议在容器中设置SSH服务器。

https://intellij-support.jetbrains.com/hc/en-us/community/posts/207649545-Use-RubyMine-and-Docker-for-development-run-and-debug-before-deployment-for-testing-

我使用https://docs.docker.com/engine/examples/running_ssh_service/#build-an-egsshd-image中的dockerfile行减去EXPOSE 22(因为它不能使用docker-compose.yml中的端口映射)成功地将SSHD添加到容器中。但是本地机器上无法访问该端口

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                          NAMES
6652389d248c        civilservice_web    "bundle exec rails..."   16 minutes ago      Up 16 minutes       0.0.0.0:3000->3000/tcp, 0.0.0.0:3022->22/tcp   civilservice_web_1

当我尝试将PUTTY指向localhost和3022时,它表示服务器意外关闭了连接。

我在这里缺少什么?

这是我的dockerfile

FROM ruby:2.2
RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    nodejs \
    openssh-server

RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

CMD ["/usr/sbin/sshd", "-D"]

RUN mkdir /MyApp
WORKDIR /MyApp
ADD Gemfile /MyApp/Gemfile
ADD Gemfile.lock /MyApp/Gemfile.lock
RUN bundle install
ADD . /MyApp

这是我的docker-compose.yml

version: '2'
services:
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/CivilService
    ports:
      - "3000:3000"
      - "3022:22"

DOCKER_HOST似乎不是环境变量

docker version输出以下内容

Client:
 Version:      17.03.0-ce
 API version:  1.26
 Go version:   go1.7.5
 Git commit:   60ccb22
 Built:        Thu Feb 23 10:40:59 2017
 OS/Arch:      windows/amd64

Server:
 Version:      17.03.0-ce
 API version:  1.26 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   3a232c8
 Built:        Tue Feb 28 07:52:04 2017
 OS/Arch:      linux/amd64
 Experimental: true

docker run -it --rm --net container:civilservice_web_1 busybox netstat -lnt输出

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.11:35455        0.0.0.0:*               LISTEN

SSHD现在与Rails应用程序一起运行,但我正在设置服务的配方对于我的基本映像附带的Linux的风格是不正确的https://docs.docker.com/engine/examples/running_ssh_service/#build-an-egsshd-image

我正在使用的图像基于Debian 8.有人能指出我示例发生故障的地方吗?

1 个答案:

答案 0 :(得分:0)

您的sshd进程尚未运行。这在netstat输出中可见:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.11:35455        0.0.0.0:*               LISTEN

但正如user2105103指出的那样,我应该意识到,如果我将docker-compose.yml与Dockerfile进行比较。您可以使用Dockerfile行在图像中定义sshd命令:

CMD ["/usr/sbin/sshd", "-D"]

但是在使用docker-compose命令运行容器时,您将覆盖图像设置:

command: bundle exec rails s -p 3000 -b '0.0.0.0'

因此,正如您在netstat中看到的那样,唯一运行的是rails应用程序监听3000.如果您需要运行多个命令,那么您可以docker exec启动第二个命令(不是推荐用于这样的第二个服务),使用在后台启动sshd的命令和前台的rails(相当丑陋),或者你可以考虑像supervisord这样的东西。

就个人而言,我跳过sshd并在需要时使用docker exec -it civilservice_web_1 /bin/bash在容器内得到提示。