Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy files between two docker containers using scp

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:

  1. Attach the same volume to both the containers.
  2. Using docker cp from server container to host and then host to client container.
  3. Using 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?

like image 414
Harshal Gunda Avatar asked Sep 13 '19 07:09

Harshal Gunda


People also ask

Can you scp to Docker?

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.

Can I use scp to copy a directory?

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

Can 2 Docker containers talk to each other?

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.


1 Answers

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:

  • Run container with privileged access: docker exec --privileged -ti container_name bash
  • Inside container, open port 22 with UFW firewall: sudo apt-get install -y ufw && sudo ufw allow 22
like image 127
4ndt3s Avatar answered Oct 23 '22 04:10

4ndt3s