Running a Docker Compose App on One EC2 Instance: Launch the Server, Start the Stack, and the Three Settings That Decide If It Works
Put the subtitle app on one EC2 server: Ubuntu 24.04 on a t3.medium with 30 GB, a security group and a .pem key pair, SSH from PowerShell, Docker, the .env, docker compose up. Then the three settings that decide whether it works: open ports, the two addresses in .env, and the S3 CORS rule.
What you will learn
- Launch an Ubuntu EC2 instance with the right size, storage, security group and key pair
- Connect with SSH and fix the unprotected private key error on Windows
- Install Docker, clone the code, create the .env and start the stack
- Make the security group, the .env addresses and the S3 CORS rule agree on the app's address
Before you start
- Wiring Whisper, Celery and S3 into a FastAPI app
- The S3 CORS rule
The subtitle app runs on your own machine. Now it goes onto an EC2 instance, a server in AWS, so anyone with the link can open it and sign in. It's a small server with no GPU, so Whisper runs on the CPU: slower, but it keeps the cost down.
Step 1: launch the instance
In the AWS console, open EC2, go to instances and click launch instances.
| Setting | Pick | Why |
|---|---|---|
| Name | subly-02 | Any name you'll recognise |
| Image | Ubuntu Server 24.04 LTS | The default offered was 26.04; 24.04 is the tried and tested version |
| Instance type | t3.medium: 2 vCPUs, 4 GB | The default t3.micro has 1 GB, too small for a Celery worker, Redis and Whisper. Not free tier, so stop it when you're done |
| Security group | Create one: allow SSH, HTTP and HTTPS | Ports 22, 80 and 443 |
| Storage | 30 GB instead of 8 | The packages and the Whisper model need room |
| Key pair | Create new: RSA, .pem | The format OpenSSH uses |
A security group is the firewall of the instance. AWS warns that SSH from 0.0.0.0/0 means any address; choosing "my IP" is safer. And read the key pair note: the private key downloads only once, AWS keeps no copy, and without it you can't connect. Store it somewhere safe. Launch, and the new instance shows t3.medium with its status check initializing, then checks passed.
Step 2: connect and start the app
The connect page shows the public IP. The browser option sometimes fails, so use PowerShell:
ssh -i <your-key>.pem ubuntu@<public-ip>
Type yes for the fingerprint. The first try fails: "unprotected private key file", then permission denied. The key file is open to other users, so SSH refuses it. Two icacls commands fix it on Windows: the first removes inherited access, the second gives read access to your user only. An AI assistant gives you these from the error. Run SSH again, and Ubuntu welcomes you.
cd /home
sudo mkdir subly
cd subly
sudo git clone <your-repo-url>
Plain git clone is denied because the folder belongs to root, so it runs with sudo. Then Docker: update the packages, run the install script from get.docker.com, add the ubuntu user to the docker group and run newgrp docker. docker --version printed 29.5.3, and Compose comes with it.
The repository has three folders, Django, FastAPI and Flask; this server runs FastAPI. In it, sudo cp sample.env .env (plain cp is denied too). ls doesn't list the new file because a name starting with a dot is hidden; ls -a shows it. The values are still placeholders: the stack starts with them, but sign in needs your own Cognito values, which stay private.
docker compose up -d --build
The logs show Redis ready, Next.js ready on port 3000, the worker with the Whisper model loaded, and the backend on port 8000. But in the browser, the public IP on port 3000 says the site can't be reached. The stack runs on the server; the page isn't reachable from outside yet.
Step 3: three settings that must agree
1. The security group
Open the instance, the Security tab, edit inbound rules. HTTP and HTTPS are there from the launch. The apps listen on their own ports, so add custom TCP rules for 8000 (backend) and 3000 (frontend). Open to all is fine for a quick check, but narrow it for real use. Save, and the address responds.
2. The two addresses in .env
NEXT_PUBLIC_API_URL is where the frontend finds the backend: the server address with port 8000. CORS_ORIGINS holds the frontend address, port 3000; the backend only answers pages from an address in this list. The login form talks to Cognito through the backend, so there's no Cognito callback address to change. After any change here, run docker compose up -d --build again, because the frontend's address is built into it.
3. The S3 CORS rule
Open the bucket, Permissions, Cross-origin resource sharing, and put the same frontend address in AllowedOrigins. The browser reads the subtitle file straight from S3, so without it View SRT stays empty. (Why, in the S3 CORS rule.)
Run it end to end
Open the server address on port 3000: the Subly app. Log in and upload a video the same way as on your machine. When the pipeline finishes, the project shows completed. View SRT loads the cues from S3, and Download SRT works too. The whole pipeline runs on AWS.
When an address doesn't match
Open the app by the server's public DNS name instead of its IP, log in, and you get "failed to fetch". In the browser's network tab both login requests are red. The new address isn't in CORS_ORIGINS, so the backend doesn't accept the page and the browser blocks the login. S3 needs the same address too, or the subtitles won't load. Add it to both, rebuild, log in again. And when a call reaches the backend but still fails, docker compose logs on the server shows what the backend printed.
Common mistakes
- A t3.micro. It runs out of memory with the worker and Whisper.
- Losing the .pem file. AWS can't give it back.
- Opening ports 3000 and 8000 to the world and leaving them that way.
- Changing
NEXT_PUBLIC_API_URLwithout rebuilding. - Updating CORS in .env and forgetting the bucket, or the other way round.
- Leaving the instance running. t3.medium isn't free tier.
Quick recap
- Ubuntu 24.04, t3.medium, 30 GB, a security group for SSH, HTTP and HTTPS, and a key pair kept safe.
- SSH in, install Docker, clone, create
.env,docker compose up -d --build. - Open 3000 and 8000, and write the same frontend address in
.envand the S3 CORS rule. - The network tab and
docker compose logstell you where a failure is.
A hand-built server works until you need a second one or a busy hour. What comes next is in from one EC2 box to containers, Terraform and autoscaling.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Python Virtual Environments: One Project, One Toolbox
Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.
Testing a FastAPI App with pytest: Three Small Tests
A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.
Config and Logging Instead of Hard-Coded Values in ML Training
Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.
