Docker Compose: Your First Application in 10 Minutes
Install Docker Desktop and run a complete web application with database using Docker Compose
On this page
Let’s skip the theory and run something real. In about 10 minutes, you’ll have a web application with a database running on your computer.
Step 1: Install Docker Desktop
Docker Desktop includes everything you need, including Docker Compose.
Windows and Mac
- Visit docker.com/products/docker-desktop
- Download for your operating system
- Run the installer
- Start Docker Desktop
Wait for the Docker icon in your system tray to show it’s running.
Linux
# Install Docker and Docker Compose
sudo apt-get update
sudo apt-get install docker.io docker-compose-plugin
# Verify installation
docker compose version
Step 2: Verify Installation
Open your terminal:
docker compose version
You should see something like:
Docker Compose version v2.20.0
Step 3: Create a Project Directory
mkdir my-first-app
cd my-first-app
Step 4: Create docker-compose.yml
Create a file named docker-compose.yml with this content:
services:
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: postgres:15-alpine
environment:
POSTGRES_PASSWORD: secret
POSTGRES_DB: myapp
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Step 5: Create a Simple Web Page
Create an html directory with an index file:
mkdir html
Create html/index.html:
<!DOCTYPE html>
<html>
<head>
<title>My First Docker Compose App</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
}
h1 { color: #2563eb; }
.success { color: #16a34a; font-weight: bold; }
</style>
</head>
<body>
<h1>It Works!</h1>
<p class="success">Your Docker Compose application is running successfully.</p>
<p>This page is served by Nginx, and there's a PostgreSQL database running alongside it.</p>
<p>Both services started with a single command: <code>docker compose up</code></p>
</body>
</html>
Step 6: Start Everything
From your project directory:
docker compose up
You’ll see Docker downloading images and starting services. When you see lines like:
✔ Container my-first-app-db-1 Started
✔ Container my-first-app-web-1 Started
Your application is running!
Step 7: See It in Action
Open your browser and visit http://localhost:8080
You should see your web page. That’s a real web server running in a container, serving your HTML file.
What Just Happened?
When you ran docker compose up, Docker:
- Read your
docker-compose.ymlfile - Downloaded the Nginx and PostgreSQL images
- Started both services
- Connected them on a private network
- Mounted your HTML files into the web server
- Made the web server available on port 8080
All of this from one command.
Managing Your Application
Stop the Application
Press Ctrl+C in the terminal where it’s running, or in a new terminal:
docker compose down
Start in Background Mode
docker compose up -d
The -d flag means “detached”—it runs in the background.
View Logs
docker compose logs
Or follow logs in real-time:
docker compose logs -f
Stop and Remove Everything
docker compose down
This stops containers and removes them, but keeps volumes (your database data).
To remove everything including volumes:
docker compose down -v
Understanding the docker-compose.yml File
Let’s break down what you created:
services: # Define the services (applications) you want to run
web: # First service named "web"
image: nginx:alpine # Use the Nginx image
ports:
- "8080:80" # Make it accessible on port 8080
volumes:
- ./html:/usr/share/nginx/html # Share your HTML folder
db: # Second service named "db"
image: postgres:15-alpine # Use PostgreSQL 15
environment: # Set environment variables
POSTGRES_PASSWORD: secret
POSTGRES_DB: myapp
volumes:
- db_data:/var/lib/postgresql/data # Persist database data
volumes:
db_data: # Named volume for database persistence
Try This: Make a Change
- Edit
html/index.htmland change the text - Refresh your browser
- See your changes immediately
No restart needed. The volume connection keeps your files in sync.
Common Issues
Port already in use: If you see an error about port 8080, change it in docker-compose.yml:
ports:
- "8081:80" # Use 8081 instead
Docker Desktop not running: Make sure you see the Docker icon in your system tray/menu bar.
Permission denied (Linux): You may need to run with sudo or add your user to the docker group.
What You’ve Learned
In less than 10 minutes, you:
- Installed Docker Desktop
- Created a docker-compose.yml file
- Started a multi-service application
- Learned basic Docker Compose commands
More importantly, you experienced the core value: describing your entire application in one file and starting it with one command.
What’s Next?
In Part 3, we’ll build something more realistic—a Node.js application that actually connects to the database. You’ll learn how services communicate, how to customize configurations, and how to work with different types of applications.
You’re not just learning a tool. You’re learning a new way to think about running applications.