<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>angular &#8211; Other Things</title>
	<atom:link href="https://blog.adamzolo.com/category/angular/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.adamzolo.com</link>
	<description>Blog about Things by Adam Zolotarev</description>
	<lastBuildDate>Tue, 25 May 2021 13:39:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>docker-compose build and deployment for Angular</title>
		<link>https://blog.adamzolo.com/docker-compose-build-and-deployment-for-angular/</link>
					<comments>https://blog.adamzolo.com/docker-compose-build-and-deployment-for-angular/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Fri, 02 Apr 2021 19:24:50 +0000</pubDate>
				<category><![CDATA[angular]]></category>
		<category><![CDATA[docker]]></category>
		<guid isPermaLink="false">http://blog.adamzolo.com/?p=959</guid>

					<description><![CDATA[In this tutorial we&#8217;ll make docker-compose files for angular and write a simple deploy script to build and deploy the images from your local machine. Development Let&#8217;s start with the dev environment. First, add .dockerignore file in the root of your project: Create .docker directory in the root of your project. Add dev.dockerfile: We are&#8230;<p><a class="more-link" href="https://blog.adamzolo.com/docker-compose-build-and-deployment-for-angular/" title="Continue reading &#8216;docker-compose build and deployment for Angular&#8217;">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
										<content:encoded><![CDATA[
<p>In this tutorial we&#8217;ll make docker-compose files for angular and write a simple deploy script to build and deploy the images from your local machine.</p>



<h2 class="wp-block-heading">Development</h2>



<p>Let&#8217;s start with the dev environment. First, add <code>.dockerignore</code> file in the root of your project:</p>



<pre class="wp-block-code"><code>.git
.gitignore
.vscode
docker-compose*.yml
Dockerfile
node_modules
</code></pre>



<p>Create <code>.docker</code> directory in the root of your project. Add <code>dev.dockerfile</code>:</p>



<pre class="wp-block-code"><code>FROM node:10

RUN mkdir /home/node/app &amp;&amp; chown node:node /home/node/app
RUN mkdir /home/node/app/node_modules &amp;&amp; chown node:node /home/node/app/node_modules
WORKDIR  /home/node/app
USER node
COPY --chown=node:node package.json package-lock.json ./
RUN npm ci --quiet
COPY --chown=node:node . .</code></pre>



<p>We are using node 10 image and using a less privileged <code>node</code> user. <code>npm ci</code> &#8220;is similar to&nbsp;<a href="https://docs.npmjs.com/cli-commands/install"><code>npm install</code></a>, except it&#8217;s meant to be used in automated environments such as test platforms, continuous integration, and deployment &#8212; or any situation where you want to make sure you&#8217;re doing a clean install of your dependencies.&#8221; &#8211; <a href="https://docs.npmjs.com/cli/v7/commands/npm-ci#description" target="_blank" rel="noreferrer noopener">npm-ci | npm Docs (npmjs.com)</a></p>



<p>Create <code>docker-compose.yml</code> file in the root of your project:</p>



<pre class="wp-block-code"><code># docker-compose
version: '3.7'
services:
services:
  app:
    container_name: 'your-container-name'
    build:
      context: .
      dockerfile: .docker/dev.dockerfile
    command: sh -c "npm start"
    ports:
      - 4200:4200
    working_dir: /home/node/app
    volumes:
      - ./:/home/node/app
      - node_modules:/home/node/app/node_modules
volumes:
  node_modules:


</code></pre>



<p>With this setup, the node_modules will be overridden when we build a new container. Basically, this means you may have to run <code>docker-compose run app npm install</code> when you need to update your packages. Rebuilding the image is not going to do it for you.</p>



<p>For alternative setups, check out this <a href="https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds" data-type="URL" data-id="https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds" target="_blank" rel="noreferrer noopener">stackoverflow answer</a>.</p>



<p>In you <code>package.json</code> you should have the definition of the npm start command:</p>



<pre class="wp-block-code"><code>"scripts": {
    "ng": "ng",
    "start": "ng serve --host 0.0.0.0",
    "build": "ng build"
  },</code></pre>



<p>Run <code>docker-compose build</code> and <code>docker-compose up</code>.</p>



<h2 class="wp-block-heading">Deployment</h2>



<h3 class="wp-block-heading">Docker Setup</h3>



<p>Let&#8217;s add <code>production.dockerfile</code> to <code>.docker</code> directory:</p>



<pre class="wp-block-code"><code># Stage 1
FROM node:10 as node

RUN mkdir /home/node/app &amp;&amp; chown node:node /home/node/app
RUN mkdir /home/node/app/node_modules &amp;&amp; chown node:node /home/node/app/node_modules
WORKDIR  /home/node/app
USER node
COPY --chown=node:node package.json package-lock.json ./
RUN npm ci --quiet
COPY --chown=node:node . .

# max_old_space_size is optional but can help when you have a lot of modules
RUN node --max_old_space_size=4096 node_modules/.bin/ng build --prod

# Stage 2
# Using a light-weight nginx image
FROM nginx:alpine

COPY --from=node /home/node/app/dist /usr/share/nginx/html
COPY --from=node /home/node/app/.docker/nginx.conf /etc/nginx/conf.d/default.conf
</code></pre>



<p>Add <code>docker-compose.production.yml</code> file:</p>



<pre class="wp-block-code"><code>version: '3.7'
services:
services:
  app:
    build:
      context: .
      dockerfile: .docker/production.dockerfile
    image: production-image
    container_name: production-container
    ports:
      - 80:80</code></pre>



<h3 class="wp-block-heading">Deploy script</h3>



<p>We are going to ssh into our destination server and copy the updated image directly. Using a repository has a lot of advantages over this approach, but if you need something simple this will work:</p>



<pre class="wp-block-code"><code>#!/bin/sh

# Build the image locally, upload to your production box and start the new container based on the latest image

{
    echo "Create an image"
    docker-compose -f docker-compose.yml -f docker-compose.production.yml build

    echo "Upload the latest image"
    echo $(date +"%T")
    docker save production-image:latest | ssh -C user@your_server_ip docker load

    echo "Stop and restart containers"
    ssh -C user@your_server_ip "echo Stopping container at $(date +'%T'); \
        docker stop production-container || true; \
        docker rm production-container || true; \
        docker container run -d --restart unless-stopped -p 80:80 --name production-container production-image:latest; \
        echo Restarted container at $(date +'%T'); \
        docker image prune -f || true"

    echo "Finished"
    echo $(date +"%T")
} || {
    # catch
    echo "Something went wrong"
}

</code></pre>



<p>We are starting a new container based on the latest uploaded image on our destination host and mapping the host port 80 to the container port 80.</p>



<p></p>



<p><strong>Helpful resources:</strong></p>



<ul class="wp-block-list"><li><a href="https://codinglatte.com/posts/angular/docker-compose-angular-multi-environment-deployments/">Docker Compose &#8211; Angular Multi Environment Deployments (codinglatte.com)</a></li><li><a href="https://nodejs.org/en/docs/guides/nodejs-docker-webapp/">Dockerizing a Node.js web app | Node.js</a></li><li><a href="https://markfknight.dev/posts/angular-in-docker/">Angular Development in Docker &#8211; Part 1 | Electric Sheep (markfknight.dev)</a></li></ul>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/docker-compose-build-and-deployment-for-angular/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
