Loading…
Warp is now open-source Learn more
Loading…
Before getting started, if you want to inspect or execute commands in a running Docker container, you should read our other article on how to run a Bash shell in Docker.
To create a containerized SSH server running in Ubuntu with minimal configuration to which you can connect using a username/password pair, you can use the following Dockerfile:
FROM ubuntu
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo "root:password" | chpasswd
RUN echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
CMD ["/usr/sbin/sshd", "-D"]Where:
The second way to connect to an SSH server is to use a pair of public/private keys.
To generate a new pair of SSH keys, you can use the ssh-keygen command as follows:
$ ssh-keygen -b 4096 -t rsa -f dockerkeyWhere:
Upon execution, this command will generate two files: dockerkey containing the private key and dockerkey.pub containing the public key.
To create a containerized SSH server running in Ubuntu with minimal configuration to which you can connect using a pair of SSH keys, you can use the following Dockerfile:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
COPY ./dockerkey.pub /root/.ssh/authorized_keys
CMD ["/usr/sbin/sshd", "-D"]Where:
To build the Dockerfile into a runnable Docker image, you can use the following docker build command:
$ docker build . -t ssh-hostWhere:
Once built, you can launch a new container from this image using the following docker run command:
$ docker run -p 2222:22 ssh-hostWhere:
To connect to the SSH server running in the container using a username/password pair, you can use the ssh command as follows:
$ ssh <username>@<host> -p <port>Where:
Once executed, you will be prompted to enter the password.
For example, the following command will connect to the SSH server running on your local machine through the port 2222 with the root user:
$ ssh root@localhost -p 2222To connect to the SSH server running in the container using a private key file, you can use the ssh command with the -i flag as follows:
$ ssh -i <key> <username>@<host> -p <port>Where:
For example, the following command will connect to the SSH server running on your local machine through the port 2222 with the root user account using the dockerkey private key file:
$ ssh -i dockerkey root@localhost -p 2222If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp AI feature:

Entering docker image with ssh and keys in the AI question input will prompt a human-readable step by step guide including code snippets.
As Linux tends to disregard the signals sent to the primary process, thus preventing it from being manually stopped using for instance CTRL + C, you have to manually handle these signals by wrapping the main process in a script as follows:
#!/bin/bash
/usr/sbin/sshd -D &
pid="$!"
trap "kill -SIGTERM $pid" SIGINT SIGTERM
wait $pidWhere:
In the server's Dockerfile, you can then replace this instruction:
CMD ["/usr/sbin/sshd", "-D"]By the following one:
COPY ./init.sh /init.sh
RUN chmod +x /init.sh
CMD ["/init.sh"]Where the new lines:
When working with OpenSSH, you can mitigate potential exposure and enhance overall security measures by creating a configuration file named sshd\_config containing the following properties:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-serverWhere:
And add the following instruction to your Dockerfile:
COPY ./sshd_config /etc/ssh/sshd_configWhich will copy the sshd\_config file into the /etc/ssh/ directory of the image at build time.
ssh-agent is a command-line tool used to simplify the management of SSH keys by keeping them encrypted in memory and automatically providing them to SSH when needed.
This allows users to avoid repeatedly typing passphrases when using SSH keys, and in the context of Docker, to avoid copying SSH keys into containers to connect to third-party services like Git.
To create a containerized SSH client that allows you to connect to an SSH server from within a Docker container, you can use the following Dockerfile:
FROM ubuntu
RUN apt-get update && apt-get install -y openssh-client
CMD ["sleep", "infinity"]Where:
And build the Dockerfile into a Docker image named ssh-client using the following docker build command:
$ docker build . -t ssh-clientssh-agent works by creating a socket file where tools can proxy requests to communicate commands and authenticate, so to forward the agent into a container you basically have to mount this socket into the container and set an environment variable with the path to the socket. This will make all requests inside the container for ssh-agent essentially be forwarded to the host session.
In a standard CLI installation of Docker you can create a new ssh-agent session by running:
To start the ssh-agent tool in the background and set the value of the SSH\_AUTH\_SOCK environment variable to the path of the SSH agent socket, you can use the following command:
$ eval $(ssh-agent)Next, you can add SSH keys to the SSH agent session using the ssh-add command as follows:
$ ssh-add <path>Where:
Finally, you can run the ssh-client container in interactive mode using the following docker run command:
$ docker run -it -v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK ssh-client bashWhere:
A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.
Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.
Learn how to override and customize the entrypoint of a Docker container using the docker run command.
Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.
Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.
How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.
Learn how to set, change and match a docker container hostname.
Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.
Start an interactive shell in Docker container
Learn how to launch a MySQL container in Docker Compose.
FROM ubuntu
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo "root:password" | chpasswd
RUN echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
CMD ["/usr/sbin/sshd", "-D"]$ ssh-keygen -b 4096 -t rsa -f dockerkeyFROM ubuntu:latest
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
COPY ./dockerkey.pub /root/.ssh/authorized_keys
CMD ["/usr/sbin/sshd", "-D"]$ docker build . -t ssh-host$ docker run -p 2222:22 ssh-host$ ssh <username>@<host> -p <port>$ ssh root@localhost -p 2222$ ssh -i <key> <username>@<host> -p <port>$ ssh -i dockerkey root@localhost -p 2222#!/bin/bash
/usr/sbin/sshd -D &
pid="$!"
trap "kill -SIGTERM $pid" SIGINT SIGTERM
wait $pidCMD ["/usr/sbin/sshd", "-D"]COPY ./init.sh /init.sh
RUN chmod +x /init.sh
CMD ["/init.sh"]PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-serverCOPY ./sshd_config /etc/ssh/sshd_configFROM ubuntu
RUN apt-get update && apt-get install -y openssh-client
CMD ["sleep", "infinity"]$ docker build . -t ssh-client$ eval $(ssh-agent)$ ssh-add <path>$ docker run -it -v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK ssh-client bash