Some handy Linux commands for files transfer and updates
Copy file from local machine to docker.
This is a very common scenario in our day to day work routine to copy a file from a local machine to a VM which is running your docker image. Let’s make it into simple steps.
Most of the time, the VM is not directly accessible, so we need to access it via a jump host or bastion server.
Let’s first copy the file to your jump host. You can use WinSCP to directly transfer a file to jump host or use the below commands.
scp fileName userID@hostIP:/home/userID
We need to provide userID and host ip to access the machine and then the path of the folder where the file will be copied.
A dummy example will be like, Let’s say, we are sending a zip file name mydata.zip
scp mydata.zip [email protected]:/home/arun
After scp, we need to ssh to the machine and then copy the file from machine to the actual VM
ssh userID@hostIP
eg: ssh [email protected]
It will ask for the password and enter your password.
Now, we need to copy the file from bastion to actual VM
scp fileName userID@VMhostIP:/home/userID
eg:
scp mydata.zip arun@10.0.0.11:/home/arun
Now ssh to the VM and get the docker image ID
ssh userID@VMhostIP
eg:
It will again ask for a password and enter the password.
Now let’s gets docker image ID
sudo docker ps
It will print the docker id of the image.
We need to use the same docker image ID to copy the file from VM to docker.
tar -c filename | sudo docker exec -i imageID /bin/tar -C /folderpath -x
say, the folder path is /opt/tomcat/webapps/ and id is 12345
tar -c mydata.zip | sudo docker exec -i 12345 /bin/tar -C /opt/tomcat/webapps/ -x
Now verify if the file is copied. Let’s get into bash/sh and get into the docker image
sudo docker exec -it imageID bash
eg sudo docker exec -it 12345 bash
It will get into the bash of your docker image
Let’s go to the path and see if the file is copied.
cd /opt/tomcat/webapps/
ls –lrt
And we can see our file is copied.
Copy from docker to local
So first get into the docker image and say we want to copy a file from docker image to our local machine.
Lets say , we want to copy back the mydata.zip back to our machine based on above references.
Copy the file from docker to the VM
sudo docker cp imageID:filePathInDocker filePathInVM
eg:
sudo docker cp 12345:/opt/tomcat/webapps/mydata.zip /home/arun/mydata.zip
Exit from the VM and then you will be in the bastion server host. Now copy the file from VM to the bastion
exit
scp user@VMHost:filePath .
eg
exit
scp [email protected]:/home/arun/mydata.zip .
Now exit from bastion and then we can copy to the local machine.
exit
scp user@jumpHost:filePath .
eg.
exit
scp [email protected]:/home/arun/mydata.zip .
Change a file in the zip/jar folder
We can use the below steps to modify a zip file.
Go to the folder path and create a new directory, say tempDir and extract the zip folder content into that
cd /opt/tomcat/webapps/
mkdir tempDir
cd tempDir
jar -xvf /opt/tomcat/webapps/mydata.jar
extract and go to the temp folder
Now the files are extracted in the temp folder and we can make the required changes.
After the changes are done, then we have to jar it again
jar cvf testJar.jar .
Copy the jar to the same folder
cp /opt/tomcat/webapps/tempDir/testJar.jar /opt/tomcat/webapps/testJar.jar
Now replace the existing jar with the testJar
rm mydata.jar
mv testjar.jar mydata.jar
Clean up / delete the tempDir and you may want to restart the server based on the loader.