Retrieve Docker-deployed files/source code on EC2

gw151226
3 min readDec 11, 2021

--

In case you lost everything on your computer, including the source code of your website you have just finished up for your client…

Due to a series of unfortunate event, my laptop was completely dead. Since mine is a Macbook Pro (A1708), there simply ain’t a way to recover the data without spending a ridiculous amount of money for repair. What made it worse was I had just completed some major changes on my client’s website without pushing to anywhere. So the only place containing the source code is on the Docker container deployed on AWS EC2 Linux 2 before my laptop crashed.

If you are in the same shoe as I was, here is the solution that has worked perfectly for me:

Pre-requisite:

You need your private key (.pem file), so ssh into your EC2 instance is still possible. If you have lost it, you can refer to the official guide. I find it to be quite clear. Here is the link.

6 STEPS to get your files back

  1. ssh into your EC2 instance

2. Get the name of your Docker container with the following command: docker ps

3. Now there are 2 ways to export your container.

3.1 docker export container_name > file_name.tar

  • docker export dreamy_uranium > output.tar

3.2* docker cp container_name:/path/to/files /local/path

docker export container_name > file_name.tar

e.g. docker export dreamy_uranium > output.tar

  • For this command to work, you need to know exactly where your file is placed first.
  • Please also note the /local/path stated above is referring to the path on the EC2 instance (NOT the path to a location on your local computer).

4. Get the full path of output.tar by realpath filename

  • realpath /home/ec2-user/output.tar

5. Open a new terminal window and cd into the Directory with your EC2 private key

6. Depending on which method you choose in Step 3, here is what you need to do:

6.1 (For .tar)

If you go with 3.1, use the following command:

scp -i <private key> <user>@<host>:<server file path> <local path to download file>
  • Replace <private key> with e.g. mykey.pem
  • Replace <user>@<host> with e.g. ec2-user@ec2–52–201–115–3.ap-southeast-1.compute.amazonaws.com
  • Replace <server file path> with e.g. /home/ec2-user/output.tar
  • Replace <local path to download file> with e.g. /Users/Abc/Desktop *this is the path to a location on your local computer
Putting the example together:scp -i mykey.pem ec2-user@ec2–52–201–115–3.ap-southeast-1.compute.amazonaws.com:/home/ec2-user/output.tar /Users/Abc/Desktop

Now the .tar file is being downloaded. Your website source code should be inside /usr/src; if not, you can always search for it.

6.2 (for directory)

If you go with 3.2, use the following command:

scp -r -i <private key> <user>@<host>:<server file path> <local path to download file>
  • This is mostly the same with 6.1, but with the newly added “-r” for downloading a directory. Refer to details in 6.1 for examples. With this, your source code should be directly downloaded.

Now your code is back. One less thing to worry about.

Give me a clap if this has been helpful to you.

--

--

No responses yet