Deploying a Web App with Docker – In-Depth Guide
Introduction
Deploying web applications using Docker allows developers to package code, dependencies, and configurations into a container. This ensures the app runs consistently across different environments—whether on a local machine, a server, or the cloud.
This guide covers:
✅ Building a simple Node.js web app
✅ Creating a Dockerfile to containerize the app
✅ Running the container locally
✅ Using Docker Compose for multi-container deployment
✅ Deploying the app on a cloud server
1️. Creating a Web App (Node.js + Express Example)
To demonstrate Docker deployment, we’ll use a simple Node.js + Express application.
Step 1: Create a Project Directory
mkdir my-web-app && cd my-web-app
Step 2: Create a server.js File
Create a basic web server that listens on port 3000.
Javascript
const express = require(“express”);
const app = express();
const PORT = 3000;
// Define a simple route
app.get(“/”, (req, res) => {
res.send(“Hello from Dockerized Web App!”);
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Step 3: Create package.json
This file manages dependencies. Run:
npm init -y
Then edit package.json to include Express.js dependency:
json
CopyEdit
{
“name”: “docker-web-app”,
“version”: “1.0.0”,
“main”: “server.js”,
“dependencies”: {
“express”: “^4.17.1”
}
}
✅ Install dependencies
npm install
2️. Writing a Dockerfile
A Dockerfile is a blueprint for building an image that runs your application.
Create a Dockerfile in the project directory:
Dockerfile
# Step 1: Use an official Node.js base image
FROM node:16
# Step 2: Set the working directory inside the container
WORKDIR /app
# Step 3: Copy package.json and install dependencies
COPY package.json .
RUN npm install
# Step 4: Copy the entire project into the container
COPY . .
# Step 5: Expose the port the app will run on
EXPOSE 3000
# Step 6: Command to start the application
CMD [“node”, “server.js”]
🔹 Understanding the Dockerfile:
1️. FROM node:16 → Uses Node.js 16 as the base image.
2️. WORKDIR /app → Sets /app as the working directory inside the container.
3️. COPY package.json . → Copies package.json first for better caching.
4️. RUN npm install → Installs dependencies inside the container.
5️. COPY . . → Copies all app files into the container.
6️. EXPOSE 3000 → Opens port 3000 for incoming traffic.
7️. CMD [“node”, “server.js”] → Starts the app inside the container.
3️. Building and Running the Docker Container Locally
✅ Step 1: Build the Docker Image
docker build -t my-web-app.
This command:
• Reads the Dockerfile.
• Packages everything into a Docker image named my-web-app.
✅ Step 2: Run the Container
docker run -d -p 3000:3000 my-web-app
🔹 Explanation:
• -d → Runs the container in detached mode (background).
• -p 3000:3000 → Maps port 3000 on the container to 3000 on the host.
✅ Step 3: Verify the Container is Running
docker ps
✅ Step 4: Test the Web App
Open a browser and go to:
You should see:
🚀 Hello from Dockerized Web App!
4️. Using Docker Compose for Deployment
If your app has multiple services (e.g., database + backend), Docker Compose simplifies deployment.
Create a docker-compose.yml File
Yaml
version: “3.8”
services:
web:
build: .
ports:
– “3000:3000”
volumes:
– .:/app
environment:
– NODE_ENV=production
✅ Run the Application Using Docker Compose:
docker-compose up -d
✅ Check Running Services:
docker-compose ps
5️. Deploying to a Cloud Server (AWS, Digital Ocean, etc.)
Step 1: Push the Image to Docker Hub
✅ Login to Docker Hub:
docker login
✅ Tag the Image:
docker tag my-web-app username/my-web-app:v1
✅ Push the Image:
docker push username/my-web-app:v1
Step 2: Deploy on a Cloud Server
✅ SSH into your server:
ssh user@your-server-ip
✅ Pull the Image from Docker Hub:
docker pull username/my-web-app:v1
✅ Run the Container on the Server:
docker run -d -p 80:3000 username/my-web-app:v1
✅ Access the App in a Browser:
🚀 Your app is now live on the internet!
6️⃣ Scaling the Web App with Docker Swarm
For high availability and scalability, use Docker Swarm to run multiple instances of your web app.
✅ Initialize a Swarm:
docker swarm init
✅ Deploy a Scalable Web App:
docker service create –name web-app –replicas 3 -p 80:3000 username/my-web-app:v1
✅ Check Running Services:
docker service ls
7️. Best Practices for Deploying Web Apps with Docker
🔹 Use Multi-Stage Builds to keep images lightweight.
🔹 Leverage Docker Compose for managing multi-container setups.
🔹 Push images to Docker Hub or a private registry for easy deployment.
🔹 Monitor containers using tools like Docker Logs and Prometheus.
🔹 Use Volumes for persistent data storage.
Conclusion
• Docker simplifies web app deployment by containerizing code and dependencies.
• Docker Compose makes managing multi-container apps easy.
• Cloud deployment can be done using Docker Hub and cloud providers.
• Scaling with Docker Swarm ensures high availability.