DEV Community

Cover image for ๐Ÿšข How to Dockerize Your Existing Angular Application
Suliman Munawar khan
Suliman Munawar khan

Posted on

๐Ÿšข How to Dockerize Your Existing Angular Application

Tired of hearing โ€œIt works on my machineโ€?

Let's fix that.

Dockerizing your Angular app helps you:

  • Package it with all dependencies
  • Run it consistently on any machine
  • Simplify deployment

In this guide, you'll learn how to containerize your Angular app using Docker, step-by-step โ€” even if you're new to Docker.


๐Ÿงฐ Prerequisites

Make sure you have:

  • โœ… Node.js and Angular CLI installed
  • โœ… Docker installed and running Download Docker
  • โœ… An existing Angular app (any version works)

๐Ÿ— Step 1: Build Your Angular App for Production

Angular apps need to be built before being served in production.

ng build --configuration production
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Step 2: Create Your Dockerfile

Inside the root of your Angular project, create a file named: Dockerfile

Paste the following content:

# Stage 1: Build the Angular app
FROM node:18-alpine AS build

WORKDIR /app

COPY package.json package-lock.json ./
RUN npm install

COPY . .
RUN npm run build -- --configuration production

# Stage 2: Serve the app using NGINX
FROM nginx:1.23-alpine

COPY --from=build /app/dist/* /usr/share/nginx/html

# Copy custom nginx config (optional)
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Notes:

  • First stage builds the Angular app using Node.js
  • Second stage serves the build with a lightweight NGINX server
  • We expose port 80 so Docker knows where the app runs

๐ŸŒ Step 3 (Optional): Add a Custom NGINX Config

You can skip this if the default is fine. But for SPAs (like Angular), custom routing helps avoid 404 errors on refresh.

Create a file: nginx.conf
Add:

server {
  listen 80;
  server_name localhost;

  root /usr/share/nginx/html;
  index index.html;

  location / {
    try_files $uri $uri/ /index.html;
  }

  error_page 404 /index.html;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ Step 4: Add .dockerignore

To speed up the build and avoid copying unnecessary files, create a .dockerignore file:

node_modules
dist
.git
.gitignore
Dockerfile
README.md
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช Step 5: Build Your Docker Image

From the root folder of your project, run:

docker build -t my-angular-app .
Enter fullscreen mode Exit fullscreen mode

It will:

  • Install dependencies
  • Build the Angular app
  • Create a lightweight image ready to run

๐Ÿš€ Step 6: Run Your Container

After the build completes:

docker run -d -p 8080:80 my-angular-app
Enter fullscreen mode Exit fullscreen mode

Visit:
๐Ÿ‘‰ http://localhost:8080

You should see your Angular app running in a Docker container!

๐Ÿงผ Bonus: Clean Up

To stop the container:

docker ps  # Find the container ID
docker stop <container-id>
Enter fullscreen mode Exit fullscreen mode

๐ŸŒ Deploying to the Cloud?

Now that your Angular app is dockerized, you can:

  • Push it to Docker Hub or GitHub Container Registry
  • Deploy it to Azure App Service, AWS ECS, Google Cloud Run, etc.

๐Ÿ‘‹ About Me

Iโ€™m a frontend developer working with Angular, React, and AI-assisted tools. I love writing clean, scalable code and sharing what I learn along the way.

Letโ€™s connect in the comments โ€” and donโ€™t forget to โค๏ธ the post if you found it useful!

Happy Dockering!

Top comments (0)