π Deploying Your Angular App with Docker β The Fun Way!
Prerequisite: Installation of
- Docker Desk top
- Visual Studio Code, (with or w/o your angular project)
- Node.js,
- Docker Extension for VS.
Letβs break it down into two easy stages. No boring explanationsβjust simple, fun, and straight to the point! π
π¨ Step 1: Create Your Angular Project
First things first, letβs create our Angular project and get things rolling. Open your terminal and type:
ng new front-end # This creates a new Angular app Skip this if u have already project
cd front-end # Move into the project folder
ng serve # Start the app on localhost:4200
Now, check if your app is running in the browser. If yes, youβre awesome. If not, wellβ¦ double-check the steps above. π
π³ Step 2: Create the Dockerfile
Docker loves instructions, so weβll create a Dockerfile
to tell it what to do. Create a file named Dockerfile
in your project root and add the following:
π¨ Stage 1: Build Angular Code
# Use the latest Node.js for building
FROM node:latest AS build
WORKDIR /usr/local/app
COPY ./ /usr/local/app/
RUN npm install # Install dependencies
RUN npm run build # Build the Angular app
Whatβs happening here? We:
- Pulled the latest Node.js image
- Set our working directory
- Copied all our project files into the container
- Installed dependencies & built the project (Boom! π)
π Stage 2: Serve with Nginx
# Use the latest Nginx image for serving the app
FROM nginx:latest
COPY --from=build /usr/local/app/dist/front-end /usr/share/nginx/html # Change 'front-end' if needed
EXPOSE 80 # Open port 80 for the webserver
Now, we:
- Pulled the latest Nginx image
- Copied the compiled Angular app to the Nginx web root
- Exposed port 80 so the world can see our masterpiece π
π’ Step 3: Build & Run the Docker Image
Build the Angular project before creating the image:
ng build # This will generate the 'dist' folder
Now, letβs build the Docker image and run it:
docker build -t my-angular-app . # Replace 'my-angular-app' with your preferred name
docker run -p 80:80 my-angular-app # Runs your app on port 80
π Open your browser and visit http://localhost:80
βyour Angular app is now running inside a Docker container! π
π Step 4: Push Your Project to GitHub (With Git hub account for immbizsoft)
Letβs store our project in a GitHub repository. First, initialize Git:
git init # Initialize a new Git repository
git add . # Stage all files
git commit -m "Initial commit" # Commit the files
Now, connect your project to GitHub:
git remote add origin https://github.com/immbizsoft/front-end.git # Replace with your actual repository URL
git branch -M main # Set the default branch to main
git push -u origin main # Push your code to GitHub
Boom! π Your Angular project is now safely stored on GitHub.
π Thatβs It!
Now youβve officially dockerized your Angular app AND pushed it to GitHub! You can share it, deploy it, or just feel cool knowing youβre a DevOps pro. π
Got any questions? Drop them in the comments below! Happy coding! π»π