0

I want to SSH into a directly into the bash of a docker image running on a Ubuntu VM. VM has public ip.

I want to SSH into this docker image from a remote machine. I have added the auth keys from my remote machine to my Ubuntu VM. And then started the docker image with bash on the VM with this command:

docker exec -it CONTAINER_ID bash

When I run SSH command from the remote machine as follows:

ssh -i path/to/private/key username_VM@ip_VM

I directly ssh into the Ubuntu VM but not into docker image. How to ssh directly into running docker image on the VM?

gapc311
  • 463
  • 2
  • 6
  • 15
  • Does this answer your question? [How to SSH into Docker?](https://stackoverflow.com/questions/28134239/how-to-ssh-into-docker) – tripleee Jan 19 '20 at 11:45
  • If you are really running Docker on a virtual machine, perhaps you need additional routing to pass traffic from the host system into the virtual machine. Maybe you should clarify this in your question. What's the OS underneath the VM and which virtualization platform are you using? Why are you running Docker inside that? – tripleee Jan 19 '20 at 11:48
  • How do you directly ssh into your browser – not your host system, but the browser itself? A Docker image usually runs a single process and it's almost never an ssh daemon (and the credentials are almost impossible to set up correctly). I wouldn't worry about this particular case. – David Maze Jan 19 '20 at 12:05
  • Also consider https://stackoverflow.com/questions/30172605/how-do-i-get-into-a-docker-containers-shell/30173220#30173220 – larsks Jan 19 '20 at 13:29
  • What do you mean by - "How to ssh directly into running docker". Why not ssh to remote machine and then ```docker exec -it container-id /bin/bash``` to docker? – GintsGints Jan 19 '20 at 18:50
  • @GintsGints I am not trying to ssh into docker from the host. I am trying to ssh into host that's running docker container from a remote machine. So that I directly get inside the docker container on doing ssh from remote machine. And I don't want to use any port in the ssh command. I want the docker ssh to be present on available on port 22 of host. So that I simply run the command ssh user_host@ip_host and it gets me inside the docker container.. – gapc311 Jan 21 '20 at 06:29

1 Answers1

0

You need to expose the Docker instance's SSH port to the outside world with -p. If the host where you run this image is already using port 22 for its SSH server, you need to use a different port number.

Try e.g.

docker run -p 2222:22 yourimage

and then

ssh -i path/to/private/key -p 2222 username_VM@ip_VM

to log in to the instance over SSH.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Is it possible that instead of port 2222 I simply do -p 22:22 so that I don't have to enter the port number in ssh command? I tried running docker command for forwarding port 22 but it exits with this docker error: docker: Error response from daemon: driver failed programming external connectivity on endpoint condescending_easley. Error starting userland proxy: listen tcp 0.0.0.0:22: bind: address already in use. – gapc311 Jan 19 '20 at 12:56
  • That's exactly what I am explaining in this answer. You already have an SSH daemon using that port so that's why you need to use a different port number. Alternatively, shut down the hosting system's SSH daemon (but binding port 22 requires privileged access; not sure if Docker can do that even then without `sudo`). – tripleee Jan 19 '20 at 13:12
  • And of course, specyfing the port number in your `.ssh/config` lets you get away with not specifying it explicitly every time you connect, if that's what you are trying to avoid. – tripleee Jan 19 '20 at 13:13