Dockerfiles for Web Applications: A Simple Guide to Containerizing Your Apps

Deploying web applications smoothly is super important in today’s DevOps world. Docker helps a lot by packaging apps into lightweight containers that run the same way everywhere.
In this blog, we’ll learn how to write Dockerfiles to deploy web applications using popular tools like Nginx, Tomcat, Node.js, and even MySQL database.
1. Nginx for Static Websites
Nginx is a fast and reliable web server used to serve static files like HTML, CSS, and images.
Here’s a simple Dockerfile to deploy a static website using Nginx:
FROM nginx
COPY . /usr/share/nginx/html/
EXPOSE 80
We start with the official Nginx image.
Copy your website files into Nginx’s default folder.
Expose port 80 so the app is accessible on the web.
2. Tomcat for Java Web Applications
Tomcat is a popular server to run Java web apps, like those using servlets or JSP.
Sample Dockerfile for Tomcat:
FROM tomcat:8.0.20-jre8
COPY tomcat-users.xml /usr/local/tomcat/conf/
COPY target/*.war /usr/local/tomcat/webapps/
EXPOSE 8080
Use an official Tomcat image with Java 8.
Add your Tomcat user configuration.
Copy your
.war
file (Java web app) to Tomcat’s webapps folder.Expose port 8080 for access.
3. Node.js for Dynamic Web Apps
Node.js lets you build real-time, scalable apps using JavaScript.
Here’s how to write a Dockerfile for a Node.js app:
FROM node:19-alpine AS builder
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
FROM builder AS final
RUN npm install --production
COPY . .
CMD ["node", "index.js"]
Use a lightweight Node.js image.
Install dependencies.
Copy your app code.
Run the app with
node index.js
.
4. MySQL Database Dockerfile
You can also containerize databases like MySQL to run alongside your apps.
Example Dockerfile for MySQL:
FROM mysql/mysql-server:5.7
ENV MYSQL_ROOT_PASSWORD=admin@123
ENV MYSQL_USER=root
EXPOSE 3306
Use MySQL official server image.
Set root password and user via environment variables.
Expose MySQL default port 3306.
Wrap Up
Docker makes deploying web apps easy and consistent. Whether you’re serving static pages with Nginx, running Java apps on Tomcat, or building dynamic sites with Node.js — Dockerfiles help you package your app with everything it needs.
Start writing your Dockerfiles today and enjoy fast, reliable deployments everywhere!
Show Some Love! 💖
If you found this helpful for your Docker learning or interviews, please hit the heart button 10 times and leave a comment! Your support inspires me to create more DevOps content. ❤️
Subscribe to my newsletter
Read articles from Anusha Kotha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
