<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arda Suğur on Medium]]></title>
        <description><![CDATA[Stories by Arda Suğur on Medium]]></description>
        <link>https://medium.com/@ardasugur?source=rss-98de1d35368e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VIEvEwB2ULmqQHVSbIlxqQ.jpeg</url>
            <title>Stories by Arda Suğur on Medium</title>
            <link>https://medium.com/@ardasugur?source=rss-98de1d35368e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:23:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ardasugur/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular 17 SSR Dockerization]]></title>
            <link>https://medium.com/@ardasugur/angular-17-ssr-dockerization-fb653b4130c6?source=rss-98de1d35368e------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb653b4130c6</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[dockerfile-best-practice]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <dc:creator><![CDATA[Arda Suğur]]></dc:creator>
            <pubDate>Tue, 21 May 2024 09:22:37 GMT</pubDate>
            <atom:updated>2025-06-15T18:41:58.523Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Dockerfile for Angular 17 SSR application for production. And how it’s made.</p><p>tl;dr</p><pre>FROM node:20.13.1-alpine3.19 as build<br>WORKDIR /app/src<br>COPY package*.json ./<br>RUN npm ci<br>COPY . ./<br>RUN npm run build<br><br>FROM node:20.13.1-alpine3.19<br>RUN addgroup -S exampleusergroup &amp;&amp; adduser -S exampleuser -G exampleusergroup<br>USER exampleuser<br>WORKDIR /usr/app<br>COPY --from=build /app/src/dist/&lt;your-project-name&gt;/ ./<br>CMD node server/server.mjs<br>EXPOSE 4000</pre><h3>Intro</h3><p>We are going to make our Angular app with Server Side Rendering enabled run in docker container. Normal Angular applications generate static html, css and js files. SSR projects on the other hand creates server js files that needs node.js runtime.</p><p>We are going to create docker image with our publish files and runtime.</p><h3>Table of Contents</h3><ol><li><a href="#5afa">Constructing Multi Stage Dockerfile</a><br><a href="#0c22">1. Build Stage <br></a><a href="#7b29">2. Final Stage </a><br><a href="#7e1f">3. Build &amp; Run</a></li><li><a href="#693c">Fine Tuning<br></a><a href="#9d4e">1. Angular &amp; Node Versions</a><br><a href="#e8c4">2. Node Images</a></li><li><a href="#0df6">Security Tips</a></li></ol><h3>Constructing Multi Stage Dockerfile</h3><p>We are going to build our <em>Angular 17 SSR application</em> to run in a container.</p><p>We are going to make a multistage build. We’re going to build the app in Docker container and publish it on another container. <br>Since we don’t want any files other than our production ready files in our final image, first we are going to start a dockerfile.</p><pre>FROM node:20-alpine as build<br>WORKDIR /app/src<br>COPY package*.json ./<br>RUN npm ci<br>COPY . ./<br>RUN npm run build</pre><h4>In this part we create our build image:</h4><ul><li>Set base image.</li><li>Set a folder in it for our source code.</li><li>Copy only package.json and package.lock.json files to our source code folder.</li><li>Run npm ci for clean install.</li><li>Copy all other source code files.</li></ul><p><strong>Why did we first copy only package json files instead of copy all of the source code at third line?</strong></p><p>Because Docker caches all steps. If our package json files (or base image) haven’t changed since last build, then when we’d need to change our source code, Docker won’t need npm install again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*VtFLpsaUU6pnhY3zO3RH8w.png" /><figcaption>Cached subsequent build example. All steps are cached.</figcaption></figure><p>Isn’t it pretty nice.</p><h4>Final Image</h4><p>First image is temporary build image. It doesn’t publish with our production image. Second image has only runtime files. Much smaller packages means less surface for vulnerabilities, less time in CICD pipelines, faster deployment, less disk space etc…</p><p>After all commotion, we run build command.</p><p>Now the final image part:</p><pre>FROM node:20-alpine<br>WORKDIR /usr/app<br>COPY --from=build /app/src/dist/&lt;your-project-name&gt;/ ./<br>CMD node server/server.mjs<br>EXPOSE 4000</pre><p>We only copy our runtime files to final container.</p><p>When SSR enabled on Angular 17, on build it creates folder path like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*4q77pZCsAeOLgK2PGFbHDA.png" /></figure><p>dist/{OurProjectName} in this path there are 2 folder; <em>server </em>and <em>browser</em>.<br>Both required to Angular SSR app run correctly, so we copy all.<br>After that we run server.mjs with node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*CQl93_5oBtckHzr6zyt7mQ.png" /><figcaption>server.ts port</figcaption></figure><p>In server.ts file, default port is 4000, so I stick with it.<br>EXPOSE 4000 makes our docker be image able to listen port 4000.</p><h4>Ready to ship</h4><p>Our dockerfile is now ready. We can build our image with<br>docker build . <br>My advice is to give it a tag even if you aren’t going to push it to some docker repository<br>docker build . -t example-tag</p><p>You can run your application using Docker run command like:</p><p>docker run -p 4000:4000 example-tag <br>You can visit your site with <a href="http://localhost:4000/"><strong>http://localhost:4000</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jfEpL9lfaIdxFqtQ" /><figcaption>Photo by <a href="https://unsplash.com/@carrier_lost?utm_source=medium&amp;utm_medium=referral">Ian Taylor</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Fine Tuning &amp; Tips</h3><p>In this part why and how to select base image, how we can make it better.</p><h4>Angular &amp; Node versions</h4><p>Our Angular SSR applications are <strong>express.js</strong> applications, which are <strong>node.js</strong> applications.</p><p>We need node runtime. But which version do we choose?</p><p>As today latest Angular version is 17.3 I’m going to use this version in this example.<br>First, let&#39;s check <a href="https://angular.io/guide/versions"><strong><em>https://angular.io/guide/versions</em></strong></a> for which node version is supported.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FW8Z94H7hHTZ3fb5wBHGTg.png" /><figcaption>Angular version guide page</figcaption></figure><p>This may change at the time you read but this page is where you should pick node version for your base image.</p><p>For Angular 17.1 and later, node versions <em>^18.13.0</em> and <em>^20.9.0 </em>are supported<em><br></em>This means we should use</p><ul><li><em>18.13.0</em> and later minor, patch versions like <em>18.13.1</em> or <em>18.14.0</em>, <strong>not </strong><em>19.0.0</em></li><li><em>20.9.0 </em>and later minor, patch versions like <em>20.9.1</em> or <em>20.10.0</em>, <strong>not </strong><em>21.0.0</em></li></ul><p>So we want to use major version 20 and latest minor and patch versions of Node.js for less bugs and performance improvements.</p><h4>Node.js Docker Hub Images</h4><p>Node.js has official images in Docker Hub: <a href="https://hub.docker.com/_/node">https://hub.docker.com/_/node</a></p><p>In this page there are tags listed with “S<em>upported tags and respective `dockerfile` links</em>”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/1*j3I5Q6BKHpOMfwvBq11B6w.png" /><figcaption>node.js docker hub images for version 20</figcaption></figure><p>In this screenshot above, all node version 20 with latest minor/patch images are shown.</p><p>You can use any one of these tags. Differences are; different base images (operating system) of the node images.</p><p>I’m not going to compare operating systems.</p><p>But if you search tags in this page<br><a href="https://hub.docker.com/_/node/tags?page=&amp;page_size=&amp;ordering=&amp;name=20-">https://hub.docker.com/_/node/tags?page=&amp;page_size=&amp;ordering=&amp;name=20-</a></p><p>you’ll see both package sizes and security vulnerabilities:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nZ0233eV2NFQCt1TyvAidg.png" /><figcaption>Image vulnerabilities and package sizes</figcaption></figure><p>What I’m looking for is minimum package size and minimum vulnerabilities.</p><p>Alpine Linux is best operating system in these matters. (IMO) <br>From Alpine Linux website: <em>Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gOrrDr1K-n1yMd0Xpk4NkA.png" /><figcaption>Alpine Linux Node 20 image</figcaption></figure><p>In Node Docker Hub page you can see, these tags are<strong> “for now”</strong> same image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*OsXcB2rMmC9KvAi1weCtVA.png" /></figure><p>For example</p><ul><li><strong>20-alpine</strong> means Node v20 latest minor/patch version and Alpine Linux latest version</li><li><strong><em>20-alpine3.19</em> </strong>means Node v20 latest minor/patch version and Alpine Linux 3.19 latest patch version</li><li><strong><em>20.13.1-alpine3.19</em> </strong>means Node v20.13.1 version and Alpine Linux 3.19 latest patch version</li></ul><p>When there is node version 20.14.0:</p><p><em>20-alpine </em>and <em>20-alpine3.19</em> will be updated to that version. But<em> 20.13.1-alpine3.19 </em>will stay with same Node version. This is called <strong>pinned tag. </strong>It wouldn’t have a breaking change.</p><p>When you’re making an image for production, you’ll want your base image doesn’t change automatically so you don’t have side effects. It’s better to check new versions and update base images manually.</p><pre>FROM node:20-alpine as build<br>WORKDIR /app/src<br>COPY package*.json ./<br>RUN npm ci<br>COPY . ./<br>RUN npm run build<br><br><br>FROM node:20-alpine<br>WORKDIR /usr/app<br>COPY --from=build /app/src/dist/&lt;your-project-name&gt;/ ./<br>CMD node server/server.mjs<br>EXPOSE 4000</pre><p><strong>node:20-alpine</strong> version is fine for general purpose, for example a year later Angular will have 1 or 2 major updates like Angular 18-19<br>But Angular 17 will stay node version 20.<br>Alpine OS latest version with this tag probably be fine.</p><p><strong>But </strong>if you’re going to publish some app to production,</p><p><strong>Use</strong> a base image with versions <strong>pinned</strong> like this:</p><pre>FROM node:20.13.1-alpine3.19 as build<br>WORKDIR /app/src<br>COPY package*.json ./<br>RUN npm ci<br>COPY . ./<br>RUN npm run build<br><br><br>FROM node:20.13.1-alpine3.19<br>WORKDIR /usr/app<br>COPY --from=build /app/src/dist/&lt;your-project-name&gt;/ ./<br>CMD node server/server.mjs<br>EXPOSE 4000</pre><h3>Security extras</h3><p>We build our image as small as possible. We tried to minimize attack surface. (less packages on final image, less vulnerability potency from needless packages for our app.)</p><p>There are other steps for security like running app as user.</p><pre>FROM node:20.13.1-alpine3.19 as build<br>WORKDIR /app/src<br>COPY package*.json ./<br>RUN npm ci<br>COPY . ./<br>RUN npm run build<br><br><br>FROM node:20.13.1-alpine3.19<br>RUN addgroup -S exampleusergroup &amp;&amp; adduser -S exampleuser -G exampleusergroup<br>USER exampleuser<br>WORKDIR /usr/app<br>COPY --from=build /app/src/dist/&lt;your-project-name&gt;/ ./<br>CMD node server/server.mjs<br>EXPOSE 4000</pre><p>We’ve added two new lines</p><pre>RUN addgroup -S exampleusergroup &amp;&amp; adduser -S exampleuser -G exampleusergroup<br>USER exampleuser</pre><p>As default Docker containers run processes as <em>root user. </em>We change this behavior with those two lines. First line adds new user and user group. (standard linux commands.) Second line sets this new user as active user for our image.</p><p>You can find more (and updated) tips for security on: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html">https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html</a></p><p>Thanks for reading, if you have any suggestions. Please comment or you can find me on <a href="https://x.com/ArdaSugur">https://x.com/ArdaSugur</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb653b4130c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>