|
| 1 | +# Setting Up Docker Environment for Angular Application with Nginx |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | + |
| 5 | +1. [Introduction](#1-introduction) |
| 6 | + - [Purpose](#purpose) |
| 7 | + - [Scope](#scope) |
| 8 | +2. [Prerequisites](#2-prerequisites) |
| 9 | +3. [Set up for Angular project](#3-set-up-for-angular-project) |
| 10 | +4. [Setting up the Docker Environment](#4-setting-up-the-docker-environment) |
| 11 | + - [Dockerfile](#41-dockerfile) |
| 12 | + - [Angular version-specific file path for Nginx](#set-the-correct-file-path-for-nginx) |
| 13 | + - [Docker Compose](#42-docker-compose) |
| 14 | + - [nginx.conf](#43-nginxconf) |
| 15 | + - [.dockerignore](#44-dockerignore) |
| 16 | +5. [Testing the Docker Environment](#5-testing-the-docker-environment) |
| 17 | + - [Using docker-compose](#build-and-run-using-docker-compose-file) |
| 18 | + - [Step by step build](#or-alternatively-build-step-by-step) |
| 19 | +6. [Access Angular Application](#6-access-angular-application) |
| 20 | +7. [Sample Repository](#7-sample-repository) |
| 21 | +8. [Conclusion](#8-conclusion) |
| 22 | + - [Benefits of Docker Setup](#benefits-of-docker-setup) |
| 23 | + - [Future Enhancements](#future-enhancements) |
| 24 | + |
| 25 | +Each section is linked to its corresponding content in the document for easy navigation. |
| 26 | + |
| 27 | +## 1. Introduction |
| 28 | + |
| 29 | +### Purpose |
| 30 | + |
| 31 | +The purpose of this document is to provide a guide for setting up a Docker environment for an Angular 17 application and to host it on nginx server. |
| 32 | + |
| 33 | +### Scope |
| 34 | + |
| 35 | +- This document covers the basic setup of a Docker environment for an Angular application, including Dockerfile creation, Docker Compose setup, and database configuration. |
| 36 | + |
| 37 | +- Dockerization is done on an app built on Angular version 17.1.0 |
| 38 | + |
| 39 | +- This document also covers Dockerization for apps built on Angular version 16 and below. |
| 40 | + |
| 41 | +[Back to top](#table-of-contents) |
| 42 | + |
| 43 | +## 2. Prerequisites |
| 44 | + |
| 45 | +Before setting up the Docker environment, ensure you have the following prerequisites: |
| 46 | + |
| 47 | +- Docker installed on your local machine. |
| 48 | +- An Angular application set up with necessary dependencies. |
| 49 | +- Basic knowledge of Docker and Docker Compose. |
| 50 | + |
| 51 | +Download links: |
| 52 | + |
| 53 | +- [Docker](https://docs.docker.com/get-docker/) |
| 54 | +- [Node.js](https://nodejs.org/en/download/) |
| 55 | +- [Angular CLI](https://angular.io/guide/setup-local) |
| 56 | +- [Visual Studio Code (Optional)](https://code.visualstudio.com/) |
| 57 | + |
| 58 | +[Back to top](#table-of-contents) |
| 59 | + |
| 60 | +## 3. Set up for Angular project |
| 61 | + |
| 62 | +In `package.json`, set the command to build the application with production configuration: |
| 63 | + |
| 64 | +```jsonc |
| 65 | +// package.json |
| 66 | +{ |
| 67 | + // ... |
| 68 | + "scripts": { |
| 69 | + // Other Commands |
| 70 | + // Add the following line under "scripts" |
| 71 | + "build:prod": "ng build --configuration production" |
| 72 | + } |
| 73 | + // ... |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +[Back to top](#table-of-contents) |
| 78 | + |
| 79 | +## 4. Setting up the Docker Environment |
| 80 | + |
| 81 | +### 4.1 Dockerfile |
| 82 | + |
| 83 | +Create a Dockerfile in the root directory of your Angular application. |
| 84 | + |
| 85 | +```Dockerfile |
| 86 | +### Stage 0: Build ### |
| 87 | +#-------------------------------------- |
| 88 | +# Use an official Node runtime as a parent image |
| 89 | +FROM node:20 AS builder |
| 90 | + |
| 91 | +# Set the working directory in the container |
| 92 | +WORKDIR /usr/src/app |
| 93 | + |
| 94 | +# Copy package.json and package-lock.json to the working directory |
| 95 | +COPY package*.json ./ |
| 96 | + |
| 97 | +# Install Angular CLI Globally |
| 98 | +RUN npm install -g @angular/cli |
| 99 | + |
| 100 | +# Install app dependencies |
| 101 | +RUN npm install |
| 102 | + |
| 103 | +# Copy the rest of the application code |
| 104 | +COPY . . |
| 105 | + |
| 106 | +# Build the Angular app for production |
| 107 | +RUN npm run build:prod |
| 108 | + |
| 109 | +### Stage 1: Run ### |
| 110 | +#-------------------------------------- |
| 111 | +# Defining nginx image to be used |
| 112 | +FROM nginx:latest AS ngi |
| 113 | + |
| 114 | +# Fetch APP_NAME from docker-compose |
| 115 | +ARG APP_NAME |
| 116 | + |
| 117 | +# Create App Path for build files for Angular 17 and above projects |
| 118 | +# For versions older than 17, replace APP_PATH with /usr/src/app/dist/* |
| 119 | +ENV APP_PATH=/usr/src/app/dist/${APP_NAME}/browser |
| 120 | + |
| 121 | +# Copy the built Angular app to the default Nginx public folder |
| 122 | +COPY --from=builder $APP_PATH /usr/share/nginx/html/ |
| 123 | + |
| 124 | +# Need to make nginx config file |
| 125 | +COPY nginx.conf /etc/nginx/conf.d/default.conf |
| 126 | + |
| 127 | +# Expose port 80 |
| 128 | +EXPOSE 80 |
| 129 | + |
| 130 | +# Start Nginx when the container starts |
| 131 | +CMD ["nginx", "-g", "daemon off;"] |
| 132 | +``` |
| 133 | + |
| 134 | +### Set the Correct File Path for Nginx |
| 135 | + |
| 136 | +#### Why? |
| 137 | + |
| 138 | +- When you run the command `ng build`, a `dist` folder is created which contains all angular build files. |
| 139 | +- Depending on the CLI version, different folder structures are created. |
| 140 | +- We need to give the path of `index.html` for Nginx to work. |
| 141 | + |
| 142 | +#### How to find folder name? |
| 143 | + |
| 144 | +- Folder name is present in `package.json` as `"name": "your-app-name"` |
| 145 | +- Verify the folder name by running `ng build` and checking in `dist` folder. |
| 146 | + |
| 147 | +#### 1. For angular version 17 |
| 148 | + |
| 149 | +``` |
| 150 | +build file folder structure |
| 151 | +
|
| 152 | +dist |
| 153 | +| your-app-name |
| 154 | +| | browser |
| 155 | +| | | index.html |
| 156 | +| | | <...other build files> |
| 157 | +| | license |
| 158 | +``` |
| 159 | +**Define APP_NAME in `docker-compose.yml`:** |
| 160 | + |
| 161 | +- Define `${APP_NAME}` in [docker compose file](#42-docker-compose) and skip this section. |
| 162 | + |
| 163 | +**OR ALTERNATIVELY** |
| 164 | + |
| 165 | +**Update `Dockerfile`:** |
| 166 | + |
| 167 | +- Update `ENV APP_PATH` by directly replacing `${APP_NAME}` with your application name like so: |
| 168 | +```Dockerfile |
| 169 | +ENV APP_PATH=/usr/src/app/dist/your-app-name/browser |
| 170 | +``` |
| 171 | + |
| 172 | +#### 2. For angular versions 16 and below: |
| 173 | + |
| 174 | +``` |
| 175 | +Build file folder structure |
| 176 | +
|
| 177 | +dist |
| 178 | +| your-app-name |
| 179 | +| | index.html |
| 180 | +| | <...other build files> |
| 181 | +``` |
| 182 | + |
| 183 | +**Update `Dockerfile`:** |
| 184 | + |
| 185 | +- Remove the line `ARG APP_NAME` |
| 186 | +- Set `ENV APP_PATH` as `/usr/src/app/dist/*`: |
| 187 | +```Dockerfile |
| 188 | +ENV APP_PATH=/usr/src/app/dist/* |
| 189 | +``` |
| 190 | + |
| 191 | +[Back to top](#table-of-contents) |
| 192 | + |
| 193 | +### 4.2 Docker Compose |
| 194 | + |
| 195 | +Create a `docker-compose.yml` file in the root directory of your project. Set the arg `APP_NAME` as the name of your application if [not updated in Dockerfile](#1-for-angular-version-17). |
| 196 | + |
| 197 | +```yml |
| 198 | +services: |
| 199 | + angular-docker: |
| 200 | + build: |
| 201 | + context: . |
| 202 | + dockerfile: Dockerfile |
| 203 | + args: |
| 204 | + APP_NAME: your-app-name |
| 205 | + image: angular-sample-image |
| 206 | + container_name: angular-sample-container |
| 207 | + ports: |
| 208 | + - '127.0.0.1:5000:80' |
| 209 | + |
| 210 | +``` |
| 211 | + |
| 212 | +[Back to top](#table-of-contents) |
| 213 | + |
| 214 | +### 4.3 nginx.conf |
| 215 | + |
| 216 | +Create a `nginx.conf` file in your root directory folder (where the `package.json` is located) and copy the below code into it. |
| 217 | + |
| 218 | +```plaintext |
| 219 | +server { |
| 220 | + listen 80; |
| 221 | +
|
| 222 | + location / { |
| 223 | + root /usr/share/nginx/html; |
| 224 | + index index.html; |
| 225 | + try_files $uri $uri/ /index.html; |
| 226 | + } |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +[Back to top](#table-of-contents) |
| 231 | + |
| 232 | +### 4.4 Dockerignore |
| 233 | + |
| 234 | +Create a `.dockerignore` file to exclude unnecessary files and directories from being copied into the Docker image. This helps in reducing the image size. |
| 235 | + |
| 236 | +```plaintext |
| 237 | +.git |
| 238 | +.gitignore |
| 239 | +.editorconfig |
| 240 | +/.vscode/* |
| 241 | +/node_modules |
| 242 | +/npm-debug.log |
| 243 | +/e2e |
| 244 | +/docs |
| 245 | +*.zip |
| 246 | +``` |
| 247 | + |
| 248 | +[Back to top](#table-of-contents) |
| 249 | + |
| 250 | +## 5. Testing the Docker Environment |
| 251 | + |
| 252 | +### Build and run using docker-compose file |
| 253 | + |
| 254 | +Run the following command to build and compose your docker image using docker-compose. |
| 255 | + |
| 256 | +```shell |
| 257 | +docker-compose -f docker-compose.yml up |
| 258 | +``` |
| 259 | +### OR Alternatively build step by step |
| 260 | + |
| 261 | +#### Build Docker Image |
| 262 | + |
| 263 | +Run the following command in the root directory of your project to build the Docker images. Replace `your-app-name` with a suitable value as an argument: |
| 264 | + |
| 265 | +```shell |
| 266 | +docker build --build-arg APP_NAME=your-app-name -t ng-docker-app:v1.0.0 -f ./Dockerfile . |
| 267 | +``` |
| 268 | + |
| 269 | +#### Create Docker Containers |
| 270 | + |
| 271 | +Start the Docker containers using the following command: |
| 272 | + |
| 273 | +```shell |
| 274 | +docker run -p 5000:80 -d ng-docker-app:v1.0.0 |
| 275 | +``` |
| 276 | + |
| 277 | +#### Optional: To get the list of currently running containers |
| 278 | + |
| 279 | +```shell |
| 280 | +docker container ls |
| 281 | +``` |
| 282 | + |
| 283 | +[Back to top](#table-of-contents) |
| 284 | + |
| 285 | +## 6. Access Angular Application |
| 286 | + |
| 287 | +You can access your Angular application at http://localhost:5000 |
| 288 | + |
| 289 | +[Back to top](#table-of-contents) |
| 290 | + |
| 291 | +## 7. Sample Repository |
| 292 | + |
| 293 | +Explore the following practical demonstration for dockerizarion of angular app: [Repository Link](https://github.com/OsmosysSoftware/angular-docker-sample) |
| 294 | + |
| 295 | +[Back to top](#table-of-contents) |
| 296 | + |
| 297 | +## 8. Conclusion |
| 298 | + |
| 299 | +### Benefits of Docker Setup |
| 300 | + |
| 301 | +Setting up a Docker environment for your Angular application with Nginx offers several benefits: |
| 302 | + |
| 303 | +- Easy deployment and scalability. |
| 304 | +- Consistent development environment across different machines. |
| 305 | +- Simplified dependency management. |
| 306 | + |
| 307 | +[Back to top](#table-of-contents) |
| 308 | + |
| 309 | +### Future Enhancements |
| 310 | + |
| 311 | +Consider enhancing your Docker setup by adding features such as environment-specific configurations, health checks, and Docker Swarm/Kubernetes integration for production deployments. |
| 312 | + |
| 313 | +[Back to top](#table-of-contents) |
0 commit comments