有几个问题需要解决:
- 由于您的主机使用端口 22,您必须使用另一个端口。你可以通过
appPort做到这一点:
"appPort": "2222:22",
此表示法将主机的端口 2222 映射到容器的 22。
-
runArgs 和 forwardPorts 是多余的。
-
您需要添加 "overrideCommand": false 以防止 VSCode 覆盖 Dockerfile 中声明的 CMD。
-
您在 Dockerfile 中的 sed 不正确,默认配置不包含行 PermitRootLogin prohibit-password 但包含 #PermitRootLogin <some-other-value。将sed 命令更改为:
RUN sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
为了方便,这里是修改过的文件:
Dockerfile:
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y --no-install-recommends net-tools iputils-ping openssh-client openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:test' | chpasswd
RUN sed -i 's/.*PermitRootLogin.*/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
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
devcontainer.json:
{
"name": "Ubuntu",
"build": {
"dockerfile": "Dockerfile",
},
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": [],
"appPort": "2222:22",
"overrideCommand": false
}
当您运行容器时,您可以使用ssh root@localhost -p 2222 和密码“test”连接到它。
另外,我不知道您为什么决定使用 VSCode 特定的方式来使用 Docker,也许这样做有充分的理由,但有更好的方法。您可以使用docker-compose 创建测试环境。它是:
- 有更好的记录;
- 广泛使用;
- 受许多 IDE(包括 VSCode)支持。
看看这个docker-compose.yml:
# Check out this reference https://docs.docker.com/compose/compose-file/
# for list of available versions, their differences, and the file format in general.
version: "3.0"
# This is where you declare containers you want to run.
services:
# This is the name of the service. One cool thing about it is that is will be a DNS name
# in the networks where this service will be present. So when you need to connect this
# service from another container you can simply do 'ssh username@ssh-server'.
ssh-server:
# This is the name of the image to use. In this case I intentionally used a nonexistent name.
# Because of that when Docker will build the image from the Dockerfile, it will assign this
# name to the image. This is not required since I've added 'build' property but giving the
# right name could come handy.
image: myssh
# This is equivalent to 'build an image from the Dockerfile in current working directory' or
# 'docker build .'
build:
context: .
dockerfile: Dockerfile
# This maps host's port 2222 to container's 22. This isn't necessary unless you want to connect
# to this container from outside (e.g. from host or another machine). Containers do not
# require 'exposure' or any other step to reach one another within one network - they have all
# ports open. That is why it is called port forwarding or mapping.
ports:
- "2222:22"
# Same image as the server but with a different command to execute.
ssh-client:
image: myssh
build:
context: .
# Just a loop to run a command every second. Won't work with password, you need a key or some hacks.
command: bash -c 'while sleep 1; do ssh root@ssh-server ls /; done'
如果将其保存到上面带有Dockerfile 的目录中,则可以使用docker-compose up 运行它。或者你可以将它与VSCode集成:当没有.devcontainer目录并且你点击Reopen in container,你可以选择From 'docker-compose.yml',然后选择你想要的服务之一,它会构建并启动一个容器。它还将创建.devcontainer 目录,其中包含devcontainer.json。