Deploy Angular application with Docker

Gleb Kletskov

--

So, a lot of developers face the problem when the project is ready, but they don’t know how to deploy project to production. In this article we will learn how to deploy Angular application with Docker and NGINX.

Prepare NGINX config ⚙️

First of all, you will need to setup NGINX config for serving static files from Angular build.

  1. Create nginx.conf file in the root directory of your project.
touch nginx.conf
NGINX config file is placed in root dir of project
Config file in Root dir

2. Write down this code into the file

Thats it! NGINX will serve our static files from Docker container.

Docker 🐳

Docker is one of the important nowadays technologies. It helps us with build, run, and share applications with containers.

  1. Create Dockerfile

Dockerfile is needed to build docker images which we will use on our virtual machine. Create it in root directory of your project.

touch Dockerfile

Write down this code to file

Deploy 📦

  1. Build Docker image

Make sure that you are in root directory of your project.

docker build . -t yournickname/yournameofcontainer:latest
docker push yournickname/yournameofcontainer

All the are images is pushed to https://hub.docker.com by default . So, your image will be public and anyone will able to pull it. If you want your images to be private you should create private repository on DockerHub or host your own registry.

2. Pull image from DockerHub

Connect via SSH to your Virtual Machine. And then pull image:

docker pull yournickname/yournameofcontainer:latest

After that you have 2 options: Run container with Docker CLI or with Docker compose:

Docker CLI way:

docker run -p 80:80 -d yournickname/yoyouryournameofcontainer:latest

Note: Flag -p 80:80 means all requests from localhost:80 will redirect to container:80 port. So you can choose the first port, but the second one should be 80 because NGINX container runs on this port by default.

Docker Compose way:

Docker Compose is the tool that helps you to automate common Docker tasks and build microservices. It is much more useful for production than common Docker CLI. First of all, create docker-compose.yml anywhere at your virtual machine.

touch docker-compose.yml

Paste this code to file with Nano, Vim or cat command.

Run this commands:

docker-compose pull angularapp
docker-compose up -d angularapp

Thats all. Now you can go to 80 port of your Virtual Machine and test your app.

Hope this article was useful for you 👨‍💻

Written by Kletskov Gleb, frontend software engineer in Mail.ru Group.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Gleb Kletskov
Gleb Kletskov

Written by Gleb Kletskov

Front-end 🕸 /DevOps 📦 /Raves ⚡️

No responses yet

Write a response