I have created two docker containers named as server and client using alpine image and I am running both containers.
Then I installed apk add openssh
and apk add openrc
in both the containers.
Using rc-service sshd start
I have started the ssh service.
Now, I want to copy a file using scp
.
From server container I typed:
scp myfile.txt [email protected]:/location_of_the_folder
It is asking a password for the client container. What can I do? What is the default password for docker container(s)?
I have tried 3 options as follows:
docker cp
from server container to host and then host to
client container.ssh-keygen
in the server container and copied the id_rsa.pub
key manually to client containers /root/.ssh
directory and it works.I don't want to use options 1 and 2. What should I do for option 3 using shell script? I want to automate this thing. I can do it manually, but can we do it by automation using a shell script, to copy some text from one container to another container?
Copy files from your local host to a machine, from machine to machine, or from a machine to your local host using scp . The notation is machinename:/path/to/files for the arguments; in the host machine's case, you don't need to specify the name, just the path.
To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents. You'll be prompted for your password on the source system ( deathstar.com ).
If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.
If it's a local docker container, use docker cp as explained here:
docker cp {container_name}:{file_path} {target_file_path OR target_dir_ended_with_slash}
But if you really need ssh
(e.g. when container runs in a remote host), try these steps:
1. Make sure you run your container with ssh port 22 redirection from host, e.g. docker run -p 8022:22 ...
Then inside the container:
2. Install sshd: sudo apt update && sudo apt install -y openssh-server
3. Create sshd directory: mkdir /var/run/sshd
4. Add password to current user ("root" user doesn't have password by default): passwd
5. Set PermitRootLogin yes
in sshd_config: sudo sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
6. You might also need: sudo sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
7. And: sudo sh -c 'echo "export VISIBLE=now" >> /etc/profile'
8. Restart sshd service: sudo service ssh restart
Then you should be able to connect with SSH, and transfer files with SCP.
If you get "port 22: Connection refused", try any of these workarounds:
docker exec --privileged -ti container_name bash
sudo apt-get install -y ufw && sudo ufw allow 22
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With