How to connect wsl2 docker containers via ssh

How to connect wsl2 docker containers via ssh

In brief, I'm learning ansible and for that I need some docker machines to test playbooks. I've some raspberry and hyper-v machines which I've already configured and connected and test some playbooks. But I need to test some other machines and I've created in docker some of this new machines with this commands:

docker network create ansible --subnet=172.18.0.0/16

docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.2 --cgroupns=host --name=debian1 --network=ansible apasoft/debian11-ansible 
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.3 --cgroupns=host --name=debian2 --network=ansible apasoft/debian11-ansible 
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.5 --cgroupns=host --name=rocky1 --network=ansible apasoft/rocky9-ansible 
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.6 --cgroupns=host --name=rocky2 --network=ansible apasoft/rocky9-ansible 
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.8 --cgroupns=host --name=ubuntu1 --network=ansible apasoft/ubuntu22-ansible 
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.10 --cgroupns=host --name=mysql1 --network=ansible apasoft/debian11-ansible 
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.11 --cgroupns=host --name=mysql2 --network=ansible apasoft/debian11-ansible
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.12 --cgroupns=host --name=tomcat1 --network=ansible apasoft/debian11-ansible 
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --ip 172.18.0.13 --cgroupns=host --name=tomcat2 --network=ansible apasoft/debian11-ansible

I can connect it using docker container exec -it [machine_name] /bin/bash but can't connect them via ssh, even can't ping them.

Container's sshd_config have this configuration enabled:

PermitRootLogin yes
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem       sftp    /usr/lib/openssh/sftp-server

I kindly request some knowledge about this, I've struggled almost for two days with this.

Thank you in advanced.

答案1

You need to check some parameters :

  • is ssh service running inside docker

ss -laputen | grep ssh #this inside docker

  • How network and firewall are configured in your host/docker context :

https://dockerlabs.collabnix.com/networking/A1-network-basics.html

https://forums.docker.com/t/docker-firewall-rules-how-to-change-or-disable/30398

Then you should be able to connect it.

相关内容