Skip to content

Instantly share code, notes, and snippets.

@knzcx
Created April 28, 2025 14:22
Show Gist options
  • Save knzcx/ef95f9c85ed3a4ee62613ee3779ff89c to your computer and use it in GitHub Desktop.
Save knzcx/ef95f9c85ed3a4ee62613ee3779ff89c to your computer and use it in GitHub Desktop.
Quick guide for copying files from Docker containers running on ECS/EC2 to local machine using command line only.

Copying Files from Docker Container on ECS: Quick Guide

Generic Commands

1. Identify Container

# List ECS tasks
aws ecs list-tasks --cluster YOUR_CLUSTER_NAME

# Show task details
aws ecs describe-tasks --cluster YOUR_CLUSTER_NAME --tasks TASK_ARN

# List Docker containers on EC2 instance
docker ps

2. Access Container

# Start shell in container
docker exec -it CONTAINER_ID_OR_NAME sh

3. Copy Files from Container to EC2 Host

# Exit the container (if logged in)
exit

# Copy files/folders from container to EC2 host
docker cp CONTAINER_ID_OR_NAME:/path/in/container /tmp/destination

# Adjust permissions
sudo chmod -R 755 /tmp/destination

4. Copy Files from EC2 Host to Local Machine

# Run from local terminal
scp -i PRIVATE_KEY.pem -r ec2-user@EC2_IP_ADDRESS:/tmp/destination /local/path

5. Alternative: Transfer via S3

# On EC2 host: Upload to S3
aws s3 cp /tmp/destination s3://BUCKET_NAME/path/ --recursive

# On local machine: Download from S3
aws s3 cp s3://BUCKET_NAME/path/ /local/path --recursive

Example for Next.js Project

1. Identify Container

# List all Docker containers on EC2 host
docker ps

2. Access Container

# Start shell in Next.js container
docker exec -it f903f2929dfc sh

# Check contents
/app $ ls -lah

3. Copy Files from Container to EC2 Host

# Exit the container
/app $ exit

# Copy .next directory to the EC2 host
docker cp f903f2929dfc:/app/.next /tmp/.next

# Adjust permissions
sudo chmod -R 755 /tmp/.next

4. Copy Files from EC2 Host to Local Machine

# Run from local terminal
scp -i ~/.ssh/my-aws-key.pem -r [email protected]:/tmp/.next ~/projects/nextjs-dev/.next

5. Alternative: Transfer via S3

# On EC2 host: Upload to S3
aws s3 cp /tmp/.next s3://my-deployment-bucket/backups/next/ --recursive

# On local machine: Download from S3
aws s3 cp s3://my-deployment-bucket/backups/next/ ~/projects/nextjs-dev/.next --recursive

Additional Useful Commands

View Container Logs

docker logs CONTAINER_ID_OR_NAME

Check Other Files in Container

docker exec -it CONTAINER_ID_OR_NAME find /app -type f -name "*.js" | grep -v "node_modules"

Check File Size of .next Directory

docker exec -it CONTAINER_ID_OR_NAME du -sh /app/.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment