Copying Files from Docker Container on ECS: Quick Guide
# 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
# 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
# List all Docker containers on EC2 host
docker ps
# 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
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