Docker has revolutionized the way modern applications are developed and deployed, offering a flexible and efficient way to package and run applications in containers. In this blog, we will delve into the reasons why Angular developers and DevOps engineers should consider dockerizing their applications and the advantages that come with it. We will also cover the basics of dockerizing an Angular application, from building a Docker image to deploying it in a production environment. Whether you are an experienced Angular developer or just starting out, this blog is designed to provide you with a comprehensive introduction to the world of Docker and its impact on Angular application development.
There are several benefits of "dockerizing" an Angular application, including:
Overall, dockerizing an Angular application can provide improved consistency, portability, and ease of deployment, making it a useful tool for modern web development.
The prerequisites for simple dockerizing an Angular application include:
A Dockerfile is a script used to automate the creation of a Docker image for an application. It contains the necessary instructions for building an image that can be used to deploy the application. At this step, it is assumed that the reader has an Angular application and docker installed locally.
Create a Dockerfile on the root of Angular application with the following content:
FROM node:16-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 4200
CMD ["npm", "start"]
This Dockerfile uses the official Node.js 16 image as the base image. It sets the working directory to /app, copies the package.json and package-lock.json files, and installs the application's dependencies using npm install. The rest of the application files are then copied to the image, and the Angular application is built using npm run build. The default port of the Angular application is exposed, and the application is started using the npm start command.
If you wish to run specific environment defined in angular.json, change the following line in the previously defined Dockerfile:
RUN npm run build --configuration env_name
Once the Dockerfile has been defined, the next step is creating a docker image that can be run as docker container. Since the previous Dockerfile example is used primarily for development and local usage, we are going to build the image with a local tag (without docker registry specified).
Position yourself in the root directory where Dockerfile is created, and run the following command in terminal:
docker build –t angular-frontend .
After you have successfully finished building the docker image, you can run the corresponding docker container using the previously defined image tag. To launch the angular application, simply execute the following command:
docker run –d –p 4200:4200 angular-frontend
The docker run command with the specified options -d -p 4200:4200 is used to run a Docker container in the background (-d option) and map a host port to a container port (-p option).
In this case, the options -p 4200:4200 maps the host port 4200 to the container port 4200. This means that the application running in the container, which is specified by the image angular-frontend, will be accessible at http://localhost:4200 on the host.
Dockerizing an Angular application in combination with Nginx offers several advantages over ”regular” dockerization:
In summary, dockerizing an Angular application in combination with Nginx provides improved performance, scalability, security, and organization compared to a regular dockerization approach.
FROM node:16-alpine AS build
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
# Serve Application using Nginx Server
FROM nginx:alpine
COPY --from=build /app/dist/project-name/ /usr/share/nginx/html
EXPOSE 80
This Dockerfile leverages the power of multi-stage builds to build and serve your Angular application in an optimized and efficient manner. Unlike the traditional approach of serving the application using CMD, this Dockerfile utilizes Nginx to serve the compiled application as static content, which results in improved performance and scalability.
The following screenshot showcases a remarkable difference in RAM usage between the Docker container served via Nginx and the one built using conventional means. This highlights the superiority of utilizing Nginx to serve the application as static content, resulting in a more optimized resource utilization and superior performance.
In conclusion, dockerizing an Angular application offers a range of benefits, including increased portability, isolation, reproducibility, scalability, and ease of deployment. To get started, the prerequisites include having Docker installed and a basic understanding of Angular and containerization. The process involves creating a Dockerfile, building the Docker image, and running the Angular Docker container. Additionally, using Nginx in combination with Docker can result in improved performance and other benefits. With this guide, developers can easily start dockerizing their Angular applications and reap the advantages it offers.