SSH directly into Docker Instance - docker

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?

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.

Related

Connect remotely via SSH on docker container

I am trying to create an ssh connection to a docker container I built.
I can successfully access the docker container from my remote machine using:
docker exec -it <container_name> /bin/bash
In the docker container, I modified the file /etc/ssh/sshd_config by changing:
Allowroot: yes
Setting a password in my docker container I can create a ssh connection from my remote server by using:
ssh root#localhost -p 2200
(I mapped 2200:22).
This works.
However from my local machine I am trying to connect to the docker container using:
ssh root#192.168.1.33 -p 2200 and I got permission denied.
where 192.168.1.33 is the ip adress of my remote machine where my docker container is running.
Would you have any clue?
thanks :)
ssh root#192.168.1.33 -p 2200 and I got permission denied.
where 192.168.1.33 is the ip adress of my remote machine where my docker container is running.
Would you have any clue?
thanks :)

when i ssh into a docker, the things are different from I docker run from its host?

I have a docker image, mapped the host 8888 to docker 22.
when i use another computer to ssh to host 8888, it goes into docker directly, but it's weird.
if i use 'sudo docker exec -u 0 -it xxxx /bin/bash' goes into it, when i input 'pip list', i can get the results like below
but if i ssh to docker via host 8888 port by root, it says command not found!
and if i input python from docker exec
well by ssh directly into docker, it is like this
totally different, what should i do to ssh into docker like docker exec from host, much appreciate!

how to ssh to docker container created from one machine (centos) from another machine(centos or mac)

I want to create a docker container from one machine (suppose having centos) machine and then access that container from another machine(may be centos or mac). How can we do that? Is it possible with macvlan networking? If yes , what are steps? If not, what is the way?
Depends from what is your final goal. Following are some approaches (depending on what you want to achieve as final goal):
Manage container and execute bash in the container on a remote host:
Easiest way is to use the environment variable DOCKER_HOST
export DOCKER_HOST=ssh://vagrant#192.168.5.178
docker exec -ti centos_remote /bin/bash
You can find more information in this answer https://stackoverflow.com/a/51897942/2816703
Use the container as a form of virtual machine on which user can ssh:
First you will need a container that is running the sshd. You will expose the port 22 on another port on the host network. Finally you will use the ssh with -p to connect that port. Here is a working example:
$ sudo docker run -d -P --name test_sshd rastasheep/ubuntu-sshd:14.04
$ sudo docker port test_sshd 22
0.0.0.0:49154
$ ssh root#localhost -p 49154
# The password is `root`
root#test_sshd $
or if you are on a remote machine, use the host IP address xxx.xxx.xxx.xxx, to connect to the container use:
$ ssh root#xxx.xxx.xxx.xxx -p 49154
# The password is `root`
Also you can pre-select a port (in this case port 22000) and test from the host.
~# docker run -d -p 22000:22 --name test_sshd rastasheep/ubuntu-sshd:14.04
~# ssh root#<ipaddress> -p 22000
Setup a network layer (L2/L3) between the hosts:
Using macvlan is one approach. Another approach is the ipvlan. In both cases, you are converting the host network adapter to a virtual router, after which you need to setup the routes. You can find detailed explanation on this link http://networkstatic.net/configuring-macvlan-ipvlan-linux-networking/

SSH from host machine into docker container

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?
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.
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?

Tomcat with Docker

I am testing out running a tomcat8 on my Mac. I have the following Dockerfile:
FROM tomcat:8-jre7
MAINTAINER "Sonam Lastname <sonam#mymail.com>"
When I run the Docker container with the following command:
docker run -d -P sonam/docker-webapp
I check for the docker process by
docker ps -l
and see the port mapped at:
0.0.0.0:32769->8080/tcp
I am not able to access the tomcat page with localhost:32769 (and even tried to 8080 port).
thanks
-Sonam
Docker runs in a virtual machine when on Mac. Tomcat will listen to that network interface.
You can run docker-machine ip <name of your docker machine> and access it via that IP instead of localhost.
If on boot2docker, it is similar: boot2docker ip.

Resources