<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ritesh4u</title>
    <description>The latest articles on DEV Community by ritesh4u (@ritesh4u).</description>
    <link>https://dev.to/ritesh4u</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F441104%2F2f78644e-433e-4774-8da9-c28e79ed55eb.jpeg</url>
      <title>DEV Community: ritesh4u</title>
      <link>https://dev.to/ritesh4u</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ritesh4u"/>
    <language>en</language>
    <item>
      <title>Deploy Angular Application With NGINX and Docker</title>
      <dc:creator>ritesh4u</dc:creator>
      <pubDate>Fri, 21 Jan 2022 06:04:27 +0000</pubDate>
      <link>https://dev.to/ritesh4u/deploy-angular-application-with-nginx-and-docker-3jf6</link>
      <guid>https://dev.to/ritesh4u/deploy-angular-application-with-nginx-and-docker-3jf6</guid>
      <description>&lt;p&gt;Hello folks, from past few months i was reading about the docker and deployment stuffs, so i thought it will be useful to share the steps which i usually follow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I presumed that u already know about the docker and how angular build takes place&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you didn't know much about docker you can go through link below&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://docs.docker.com/get-started/overview/" rel="noopener noreferrer"&gt;https://docs.docker.com/get-started/overview/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you want to know more about angular you can go through link below&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://angular.io/cli/build" rel="noopener noreferrer"&gt;https://angular.io/cli/build&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you want to know more about nginx you can go through link below&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://nginx.org/en/docs/" rel="noopener noreferrer"&gt;https://nginx.org/en/docs/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before start we need few things to be setup correctly&lt;br&gt;
1) Nodejs&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;https://nodejs.org/en/download/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;2) Angular CLI&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://angular.io/cli" rel="noopener noreferrer"&gt;https://angular.io/cli&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;3)Docker&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://docs.docker.com/get-docker/" rel="noopener noreferrer"&gt;https://docs.docker.com/get-docker/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, Lets create simple angular application for this blog You can skip this step 1 if you have app with you&lt;/p&gt;

&lt;h2&gt;
  
  
  1) On Terminal run below command to create angular application
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ng new angular-docker-blog&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2) Create 2 file with name Dockerfile , .dockerignore and nginx.conf in project root folder
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8qhsibdo4x2bqyrxhtba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8qhsibdo4x2bqyrxhtba.png" alt="file location in root folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dockerfile will consist of commands which needs to execute when we are building docker image&lt;br&gt;
.dockerignore contains which file/folder we need to ignore while docker build takes place&lt;/p&gt;

&lt;h3&gt;
  
  
  Dockerfile
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# STEP-1 BUILD
# Defining node image and giving alias as node-helper
# It's better to define version otherwise me might face issue in future build

FROM node:14-alpine3.15 as node-helper

#Accepting build-arg to create environment specific build
#it is useful when we have multiple environment (e.g: dev, tst, staging, prod)
#default value is development
ARG build_env=development

#Creating virtual directory inside docker image
WORKDIR /app

RUN npm cache clean --force

#Copying file from local machine to virtual docker image directory
COPY . .

#installing deps for project
RUN npm install

#creating angular build
RUN ./node_modules/@angular/cli/bin/ng build --configuration=$build_env

#STEP-2 RUN
#Defining nginx img 
FROM nginx:1.20 as ngx

#copying compiled code from dist to nginx folder for serving
COPY --from=node-helper /app/dist/angular-docker-blog /usr/share/nginx/html

#copying nginx config from local to image
COPY /nginx.conf /etc/nginx/conf.d/default.conf

#exposing internal port
EXPOSE 80


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  .dockerignore
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

.git
.gitignore
/node_modules


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  nginx.conf
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

server{
    listen 80;
    sendfile on;
    default_type application/octet-stream;

    gzip on;
    gzip_http_version 1.1;
    gzip_disable      "MSIE [1-6]\.";
    gzip_min_length   256;
    gzip_vary         on;
    gzip_proxied      expired no-cache no-store private auth;
    gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_comp_level   9;

    root /usr/share/nginx/html;

    location / {
      try_files $uri $uri/ /index.html =404;
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  3)Docker build command
&lt;/h2&gt;

&lt;p&gt;for creating docker image open terminal and run command&lt;/p&gt;
&lt;h5&gt;
  
  
  For creating development build
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker build -t ad-blog:development .


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h5&gt;
  
  
  For creating tst build
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker build -t ad-blog:tst --build-arg build_env=tst .


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h5&gt;
  
  
  For creating production build
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker build -t ad-blog:production --build-arg build_env=production .


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;-t: Tag (if not specified, docker will take "latest" by default)&lt;br&gt;
--build-arg : for passing build argument, in our case we are passing 'build_env' to tell angular which environment to pick while creating build.&lt;/p&gt;

&lt;h2&gt;
  
  
  4) for creating docker container
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker run -p 8080:80 -d ad-blog:tst


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;-p for defining port&lt;br&gt;
Syntex:-&amp;gt; [host-port]:[docker-port]&lt;br&gt;
80 port is exposed from container and we are mapping that with 8080&lt;/p&gt;

&lt;p&gt;-d for running running container in Detach mode&lt;br&gt;
 the docker will keep the console running&lt;/p&gt;

&lt;h3&gt;
  
  
  Finally
&lt;/h3&gt;

&lt;p&gt;If you followed steps correctly you will have docker container running on port 8080 and you will able to access you application on &lt;a href="http://localhost:8080/" rel="noopener noreferrer"&gt;http://localhost:8080/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Extras
&lt;/h3&gt;

&lt;p&gt;if you want to see running docker containers you can run this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker ps


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;for stoping docker container&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker stop CONTAINER_ID


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;CONTAINER_ID you will get from docker ps command&lt;/p&gt;

&lt;h3&gt;
  
  
  GITHUB Repo
&lt;/h3&gt;

&lt;p&gt;If you want to see how i configured for different environment&lt;br&gt;
consider checking angular.json, environment folder here  &lt;a href="https://github.com/ritesh4u/angular-docker-blog" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>docker</category>
      <category>nginx</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
