7 Steps to Dockerize Your Angular 9 App With Nginx
Set up your Angular 9 app in a Docker environment and deploy it in no time

In this tutorial, I want to show you how you can dockerize your Angular 9 application and put it online in no time!
It will be useful to have some basic knowledge of Angular and Docker, since I won’t go into the nitty-gritty details of Docker and Angular 9.
I will show you a practical way to dockerize your Angular 9 application so you can deploy it on your web hosting of choice.
Originally published on: ByRayRay.dev
Tooling
In this tutorial, we are going to use the following tools. If you haven’t installed them, I will walk you through it, but it will be faster if you have them already on your machine.
- Node.js
- npm or Yarn
- Angular CLI
- Code editor of your choice. (I favor Visual Studio Code.)
- Docker
1. Install Angular CLI
If you haven’t installed the Angular CLI, I would recommend doing so.
Open your terminal and run npm install -g @angular/cli or if you use Yarn, run yarn global add @angular/cli.
2. Create a New Angular Project With CLI
If you’ve already created an Angular project, you can skip this step.
Run the ng new command to create a new Angular 9 project. Make sure you answer all the questions you get in the terminal because this is going to make the setup very easy.
Commands
- npm:
ng new project-name - Yarn:
ng new project-name --packageManager=yarn
To test if everything works, navigate in the terminal to your project-name folder and run ng serve. When everything works as expected, you can open your browser with localhost:4200.
Of course, you’re going to build an awesome Angular application that is very useful for its users. But you can do that later.
3. Install Docker
When we want to use Docker to build our Docker image and run our container, we have to install Docker.
If you have Docker already installed, you can continue to the next step.
Download the installer for your platform and run the installation. It should be straightforward.
To test if the installation succeeds, run the command docker -version. If you see a version number, everything is good to go to the next step.
4. Create a Multi-Stage Docker Image
If you have a bit of experience with Docker, you know what a Docker image is and what you can do with it. For those who don’t know it, let me explain it in short.
What is a Docker image?
A Docker image is the blueprint for the application you want to run in a Docker container. In the blueprint, you have to decide what kind of steps Docker needs to take to build your application, get dependencies, and copy the needed files.
With the Docker image, you can spin up a Docker container to run it and make it visible in the browser, for example.
What is a Docker container?
A Docker container is a sort of virtual (mini) server that you can start, stop, or remove with a single command. A Docker container makes it easy to spin up multiple instances of that container so you can divide the traffic between them.
Let’s build the first part of the Docker image
Create a file, Dockerfile, notice there is no file extension. (That's correct.)
First, we are going to tell it on which image we want our image to be based on. This image will install all the dependencies for Node.js automatically to build our Angular 9 application on it.
FROM node:13.3.0 AS compile-imageWe are using node:13.3.0, if you want to use another version or smaller base image, check the Node.js Docker page. We call that image compile-image, this will become handy later on.
RUN npm install -g yarnNext step, we install Yarn for the people who are using Yarn. If you want to use npm, you can skip this step.
WORKDIR /opt/ng COPY .npmrc package.json yarn.lock ./We set our WORKDIR to /opt/ng so we know the directory where everything will be copied to with the COPY line.
For Yarn, we run the yarn command as you would do on your local computer.
RUN npm installWhen using npm, you run the npm install command to install all the dependencies.
COPY . ./ RUN ng build --prodCopy all the other Angular files to be able to run the ng build --prod. It is recommended to run this command on your computer before creating a Docker image, this will save you from constantly building a Docker image that is failing.
Most Docker images will stop here. But this will make your Docker image bigger than needed.
Build the second part of the Docker image
To run our Angular app, we don’t need to run it with Node.js, we can choose a more lightweight variant to run it.
The second phase of the Docker image starts with the FROM statement where we specify we want to use nginx.
COPY docker/nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=compile-image /opt/ng/dist/app-name /usr/share/nginx/htmlThe first COPY statement copies the default.conf file from the nginx base image to the Docker image we are building.
The second COPY statement tells it that we want some files from the compile-image. So, underwater, it has built the first part of the image in a separate Docker image.
We copy the built Angular app from the /dist folder in the compile-image to the html folder of NGINX.
The reason for using NGINX is that it will make it possible to run the Angular app without the need for the big node_modules folder. We only use the files of our application. This will save some bytes on the Docker image.







