How to Deploy a Web App with Docker: A Step-by-Step Guide

Docker has revolutionized web application deployment by packaging your app and its dependencies into a portable container. This ensures your application runs identically in any environment—from a developer’s laptop to a production server. In this guide, I’ll walk you through a practical, end-to-end workflow for deploying a web app using Docker.

The core idea is simple: you create a Docker image (a blueprint), run it as a container (a lightweight, isolated process), and then push that image to a registry so it can be pulled and run on any server. Let’s break this down into actionable steps.

Article illustration

1. Write a Dockerfile

The Dockerfile defines your application’s environment. Here’s a minimal example for a Node.js app:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

2. Build and Test Locally

  • Build the image: docker build -t my-web-app .
  • Run the container: docker run -p 3000:3000 my-web-app
  • Verify: Open http://localhost:3000 to confirm it works.

3. Push to a Registry

Once tested, push your image to Docker Hub or Amazon ECR so it’s accessible on your production server.

  • Tag the image: docker tag my-web-app yourusername/my-web-app:latest
  • Push it: docker push yourusername/my-web-app:latest

4. Deploy on Your Server

SSH into your server and pull your image:

docker pull yourusername/my-web-app:latest
docker run -d -p 80:3000 --restart unless-stopped yourusername/my-web-app:latest

The -d flag runs it in the background, and --restart ensures it auto-starts on reboot.

Conclusion

Docker simplifies deployment by eliminating “works on my machine” issues. By containerizing once and running anywhere, you gain consistency, scalability, and easy rollbacks. Start with these basics, and explore Docker Compose for multi-service apps, and you’ll have a robust deployment pipeline in no time.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *