I am running my Docker Container in local machine, to Use SSH utility I have perform following steps
Step 1: Login to Docker Container using docker command
$ docker exec -it <container_id or container_name> /bin/bash
Step 2: Install SSH Server
$ apt-get update && apt-get install -y openssh-server
**Step 3 Create SSH-keypair in your local machine **
You can Skip this step if you have SSH-keypair in your ~/.ssh directory
$ ssh-keygen
- Keep Default Path for storing Key Pair.
- and for this Enter passphrase (empty for no passphrase): Just press Enter
Step 4: Copy the Content of Public key file to Docker ~/.ssh/authorized_keys file
$ cat ~/.ssh/id_rsa.pub
Now Copy that Public Key
$ mkdir -p ~/.ssh && chmod 700 ~/.ssh
$ echo "<Public-Key content>" > ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
Step 5: Update SSH Configuration and restart SSH Service Inside Docker Container
Open file /etc/ssh/sshd_config and remove comment from following line
PubkeyAuthentication yes
This command is use to restart SSH Service to affect the changes.
$ service ssh restart
and Check the status of SSH Service to check it's running or not
$ service ssh status
Step 6: Get Docker Container I.P.
From Local machine run following Command
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name or container_id>
Step 7:Perform SSH
$ ssh root@<Container_IP>
You can also use SCP.
$ scp <file_name> root@<container_ip>: