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
๐ 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;"]
๐ 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;
}
๐ฆ 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
๐งช Step 5: Build Your Docker Image
From the root folder of your project, run:
docker build -t my-angular-app .
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
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>
๐ 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)