This article introduces how to connect to a Docker container via SSH using Bash in the Linux command line.
Dockerfile
First, create a Dockerfile to set up SSH connection within the Docker container. Here’s the content:
# Dockerfile
FROM ubuntu:18.04
# Install and configure SSH server
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:pw' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
# Expose SSH port
EXPOSE 22
# Start SSH server
CMD ["/usr/sbin/sshd", "-D"]
This Dockerfile uses an Ubuntu 18.04-based Docker image, installs and configures an SSH server. It exposes SSH port 22, sets the root user password to pw, and configures to allow SSH connections.
Next, use this Dockerfile to build a Docker image and start a container:
docker image build . -t ssh_server:1.1
docker run -d -p 56022:22 ssh_server:1.1
ssh [email protected] -p 56022 # Password is pw (if using a different IP, check local IP with `ip addr` command)
Creating environment directly (ubuntu)
When setting up SSH connection directly within a Docker container, follow these steps:
: <<'NOTE'
Normally, on regular Ubuntu (when installed from VirtualBox, etc.), you can SSH connect by simply installing `openssh-server`. However, Docker images ideally have minimal configuration, and there may be differences from regular Ubuntu desktop versions. It's fine for general use, but worth checking.
Therefore, if you try to SSH connect with just `apt install openssh-server`, you may get an error like:
root@conoha:~# ssh [email protected] -p 57022
ssh_exchange_identification: Connection closed by remote host
NOTE
# Enter the container, install necessary packages and start SSH server
docker run -p 57022:22 -it -v /media/ownCloud/pg/docker/mnt/:/docker yuis/env:1.2
apt-get update && apt-get install -y openssh-server
echo 'root:pw' | chpasswd
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
service ssh restart # or `/usr/sbin/sshd -D` etc
# Exit container
ssh [email protected] -p 57022 # IP address is probably fixed at 127.0.0.1, but you can run `ip addr` command to confirm
SSH connection from remote
When using SSH connection through tunneling, there’s the following method:
ssh -t [email protected] ssh -p 55022 [email protected]
You can also tunnel port forwarding:
ssh -t -R 52698:localhost:52698 -L 4567:localhost:4567 [email protected] ssh -p 55022 -L 4567:localhost:4567 -R 52698:localhost:52698 [email protected]
# Example:
ASUS:/mnt/c/pg$ ssh -t -R 52698:localhost:52698 -L 4567:localhost:4567 [email protected] ssh -p 55022 -L 4567:localhost:4567 -R 52698:localhost:52698 [email protected]
root@541e5aed5dcf:~# ruby - <<EOS -o 0.0.0.0
require 'sinatra'
get '/' do
'Hello docker!'
end
EOS
ASUS:/mnt/c/pg$ curl localhost:4567
Hello docker!
This was about basic steps and explanations for SSH connection to Docker containers and tunneling. If you lack understanding of Docker, you might want to consider using VirtualBox as well.