0

We're learning about docker and for practice we have to SSH from the host machine into a container. I'm running Ubuntu server on VMWare Workstation. I have successfully installed SSH and the service is running. The container I've created is running on an Ubuntu image. When I try to SSH into the container by using #ssh root@ContainerIP, I get the error "Connection refused". How can I fix this?

bmal
  • 1
  • 5
  • 1
    Usually Docker containers don't run ssh daemons, and usually it's not productive to look up the container IP address. Think of a container as a wrapper around a single process and not a full-blown system you need to manually manage. (Imagine you're running `nginx` directly on that host: what is that process's IP address, and how would you ssh into it?) – David Maze Jul 01 '20 at 17:13
  • 2
    Rather than SSHing into the docker container, do you perhaps instead mean you just want to access a bash shell inside the container from your host machine? If so you can find the running container ID with `docker ps`, then use the docker exec command to run a bash shell in the container - `docker exec -ti CONTAINER_ID bash`. – foxsoup Jul 01 '20 at 17:27

2 Answers2

2

Try the following commands.

docker ps

It will give you a list of all the working containers. Select the appropriate container in which you want to log in and pass to below command

docker exec -it container bash

It will log you in the container.

Sandhu
  • 348
  • 5
  • 23
1

Firstly you need to install a SSH server in the images you wish to ssh-into. You can use a base image for all your container with the ssh server installed. Then you only have to run each container mapping the ssh port (default 22) to one to the host's ports (Remote Server in your image), using -p :. i.e:

docker run -p 52022:22 container1    
docker run -p 53022:22 container2

Then, if ports 52022 and 53022 of hosts are accessible from outside, you can directly ssh to the containers using the IP of the host (Remote Server) specifying the port in ssh with -p . I.e.:

ssh -p 52022 myuser@RemoteServer --> SSH to container1
ssh -p 53022 myuser@RemoteServer --> SSH to container2

I think this post would help a lot: How to SSH into Docker?

Mahmud
  • 119
  • 1
  • 7