SSH into a Docker Container on AWS Fargate

Introduction
Setting up an SSH-accessible Docker container can be useful for debugging, remote management, and infrastructure prototyping. In this post, we’ll walk through how to:
Build a Docker image with SSH and Python
Push it to Amazon ECR
Deploy it serverlessly on AWS Fargate
Connect to it via SSH
🏢 Step 1: Build the Docker Image
We start by creating a Dockerfile that installs Python and an SSH server on an Ubuntu base image:
Build it locally:
🍎 Note for Apple Silicon Users
If you’re building the image on an ARM-based machine (like a Mac with Apple Silicon), you can choose ARM architecture when deploying to ECS.
However, if you want to deploy your image to an x86 (amd64) environment, you must explicitly specify the platform during the build process:
⚠️ Be aware that images built for amd64 may not run locally on ARM machines unless you’re using emulation. You might encounter errors like exec format error if you try to run them directly.
📤 Step 2: Pushing the Docker Image to Amazon ECR
Now that your Docker image is built and working locally, let’s upload it to Amazon Elastic Container Registry (ECR). This will allow us to later deploy it on AWS Fargate.
🛠 Create a Private Repository
First, create a private repository in the AWS Console.
Go to Amazon ECR → Private Registry → Create repository, give it a name (e.g., ssh-demo-repo
), keep the default settings, and hit Create.

🔐 Authenticate Docker with ECR
Use the following command to authenticate your Docker client with ECR:
Replace
<your-account-id>
with your actual AWS Account ID.
🏷️ Tag the Existing Image
Since your Docker image is already built, you just need to tag it with your ECR repository URI:
🚀 Push the Image to ECR
Push the image using:
Once pushed, your image will appear in the ECR console.

🚀 Step 3: Create an ECS ClusterCreate a new cluster in the Amazon ECS Console:
Name:
ssh-demo-cluster
Infrastructure: AWS Fargate (serverless)

📊 Step 4: Define the Task DefinitionIn the ECS Console:
Go to Task Definitions
Click Create new task definition with JSON
Replace <your-account-id> and the SSH_PUBLIC_KEY value with your own.

🔐 For production environments, replace the environment block with secrets to inject sensitive variables securely via AWS Secrets Manager.
🌐 Step 5: Create a Fargate Service
Go to your ECS Cluster, click Services → Create, then:
Launch type: Fargate
Task definition:
ssh-demo-task
Service name:
ssh-demo-container
Number of tasks: 1


Configure networking:
Select a public subnet
Enable public IP assignment
Use a security group allowing ports 80 and 22 (SSH only from your IP)
🛡️ Step 6: Connect via SSH
Once the task is running:Go to Tasks → ENI → Public IPSSH into your container:You should be inside your Fargate-hosted Ubuntu container!

🔒 Bonus: Use AWS Secrets Manager for Key Injection
Instead of passing your key as an environment variable, use a more secure method:
Store it in AWS Secrets Manager:
Reference it in your Task Definition JSON:
This keeps your secrets encrypted and never exposed in plaintext.
🌟 Summary
✅ Built a Docker image with SSH and Python
✅ Pushed it to Amazon ECR
✅ Deployed it using ECS Fargate
✅ Connected to it securely via SSH
✅ Explored secure ways to inject credentials
Check out our medium page: Clerion Medium