How to SSH into a Running Docker container under windows

Docker May 13, 2020

If you are familar with docker, then you know you can use docker exec to login into docker container, but you can not use ssh to login into then container, because the network is not reachable. Today i will teach you how to login into docker with ssh client.

Install docker desktop

Download docker from https://download.docker.com/win/stable/Docker Desktop Installer.exe, install docker is very easy.

After docker installation, let's use centos7 as a example, but we need run centos7 container as detached mode, so it can running in background. But we need first create a docker network first, because i want to give cnetos7 container a fix ip address,

docker network create --driver=bridge --subnet=172.18.0.0/16 --gateway=172.18.0.1 ideras

I created a network names ideras, then start a centos7 container with docker run command.

docker run --name centos -itd --network ideras --restart always --privileged=true --ip 172.18.0.2 centos:7 /usr/sbin/init

Next we need install openssh server in centos7 container, it's very easy can be done with one command.

sudo yum –y install openssh-server openssh-clients

Then we make ssh service run when container start

sudo systemctl enable sshd

Normally systemctl is not working in centos7 container, that's why i add /usr/sbin/init in the docker run command, with init started as pid 1, then we can use systemctl command as usual.

Config network

Now we still cannot connect with centos7 container with ssh, you can try `ping 172.18.0.2`,  we need to config windows network so that the network of host and docker container can visit each other.

route -p add 172.18.0.0 MASK 255.255.255.0 10.0.75.2

It's done, now we can use ssh [email protected] to login into centos7 container, and every time you restart docker, centos7 container will restart, and the ssh service in centos7 container will start automatically, and you can just ssh into docker container directly.

What's Next

Next i will write post how to use docker build hadoop cluster, hbase cluster, flink cluster, zookeeper cluster, elasticsearch cluster, if you find my blog useful, please subscribe it, thanks.

Shaoyang Liu

Founder of https://ideras.com and https://blog.ideras.com, write blog about java and node.js technology.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.