<?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: Prince Kumar Singh</title>
    <description>The latest articles on DEV Community by Prince Kumar Singh (@prince_kumarsingh_e5fdba).</description>
    <link>https://dev.to/prince_kumarsingh_e5fdba</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3752998%2F3eb2128a-dd54-4a4a-9d54-e8fb4ad3914e.png</url>
      <title>DEV Community: Prince Kumar Singh</title>
      <link>https://dev.to/prince_kumarsingh_e5fdba</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prince_kumarsingh_e5fdba"/>
    <language>en</language>
    <item>
      <title>Docker for Frontend Developers: From Vite Build to Nginx and Runtime Configuration</title>
      <dc:creator>Prince Kumar Singh</dc:creator>
      <pubDate>Mon, 23 Nov 2026 13:47:22 +0000</pubDate>
      <link>https://dev.to/prince_kumarsingh_e5fdba/docker-for-frontend-developers-from-vite-build-to-nginx-and-runtime-configuration-3jk5</link>
      <guid>https://dev.to/prince_kumarsingh_e5fdba/docker-for-frontend-developers-from-vite-build-to-nginx-and-runtime-configuration-3jk5</guid>
      <description>&lt;h2&gt;
  
  
  1. Introduction: Where Does Frontend Code Run?
&lt;/h2&gt;

&lt;p&gt;Unlike backend applications, which typically run on servers, frontend applications run inside the user's browser.&lt;/p&gt;

&lt;p&gt;For example, when we build a React application, the browser executes the JavaScript code, renders the UI, and handles user interactions.&lt;/p&gt;

&lt;p&gt;So, if frontend code runs in the browser, why do we need Docker?&lt;/p&gt;

&lt;p&gt;Docker helps us maintain a consistent environment for building and serving frontend applications. It ensures that the required dependencies, build tools, and configurations are managed consistently across different environments.&lt;/p&gt;

&lt;p&gt;Before understanding how Docker works with frontend applications, let's understand some basic Docker concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Understanding Docker Fundamentals
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Docker?
&lt;/h3&gt;

&lt;p&gt;Docker is a platform that allows us to package applications and their dependencies into containers.&lt;/p&gt;

&lt;p&gt;It helps avoid issues caused by differences in software environments, such as different dependency versions, libraries, and runtime configurations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Docker Image vs. Docker Container
&lt;/h3&gt;

&lt;p&gt;These are two fundamental concepts in Docker.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Docker Image:&lt;/strong&gt; An image is an immutable package containing everything required to run an application, such as application code, dependencies, and necessary runtime components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docker Container:&lt;/strong&gt; A container is a running instance of a Docker image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In simple terms, an image is the blueprint, while a container is the running instance created from that blueprint.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dockerfile vs. Docker Compose
&lt;/h3&gt;

&lt;p&gt;Docker provides two important components for defining and managing applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dockerfile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Dockerfile defines the instructions required to build a Docker image.&lt;/p&gt;

&lt;p&gt;It specifies things such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The base image.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dependencies to install.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Files to copy.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commands to execute.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How the application should be prepared for execution.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Docker Compose&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Docker Compose defines how different services should run and communicate with each other.&lt;/p&gt;

&lt;p&gt;It allows us to configure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Services and containers.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Port mappings.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Environment variables.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Networks.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Volumes.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, we can use Docker Compose to run our frontend application alongside a backend application.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Dockerizing a Frontend Application Using Multi-Stage Builds
&lt;/h2&gt;

&lt;p&gt;When Dockerizing a frontend application, we commonly use a multi-stage build process.&lt;/p&gt;

&lt;p&gt;The idea is simple: first, build the frontend application, and then serve the generated static files using an Nginx server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 1: Building the Frontend
&lt;/h3&gt;

&lt;p&gt;Let's assume we have a React application built using Vite.&lt;/p&gt;

&lt;p&gt;We first install the required dependencies and execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Vite processes our React application and generates optimized static files.&lt;/p&gt;

&lt;p&gt;These files generally include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HTML files.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JavaScript bundles.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CSS files.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Other static assets.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The generated output is stored inside the &lt;code&gt;dist/&lt;/code&gt; directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 2: Serving the Build Using Nginx
&lt;/h3&gt;

&lt;p&gt;Once the build is generated, we copy the contents of the &lt;code&gt;dist/&lt;/code&gt; directory into the Nginx server's web root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/usr/share/nginx/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nginx then serves these static files whenever a user requests the frontend application.&lt;/p&gt;

&lt;p&gt;It can also handle compression, such as Gzip, to reduce the size of transferred files.&lt;/p&gt;

&lt;p&gt;The main advantage of this multi-stage approach is that we don't need Node.js, &lt;code&gt;node_modules&lt;/code&gt;, or build tools in our final image. We only need Nginx and the generated static files.&lt;/p&gt;

&lt;p&gt;This keeps the final Docker image lightweight.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Understanding How React Actually Runs in the Browser
&lt;/h2&gt;

&lt;p&gt;One important thing to understand is that Nginx does not execute React or provide a separate React execution engine.&lt;/p&gt;

&lt;p&gt;Let's understand what actually happens.&lt;/p&gt;

&lt;p&gt;When we build a React application using Vite, Vite bundles our application code along with the required React and React DOM library code into JavaScript files.&lt;/p&gt;

&lt;p&gt;Now, when a user opens our application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The browser sends an HTTP request to the Nginx server.&lt;/li&gt;
&lt;li&gt;Nginx responds with &lt;code&gt;index.html&lt;/code&gt; and the required JavaScript and CSS files.&lt;/li&gt;
&lt;li&gt;The browser downloads these files.&lt;/li&gt;
&lt;li&gt;The browser's JavaScript engine executes the downloaded JavaScript.&lt;/li&gt;
&lt;li&gt;The bundled React and React DOM libraries execute within the browser.&lt;/li&gt;
&lt;li&gt;React creates and maintains its element tree and handles UI updates by updating the actual browser DOM.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, the important distinction is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nginx:&lt;/strong&gt; Delivers the HTML, CSS, and JavaScript files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Browser JavaScript Engine:&lt;/strong&gt; Executes the JavaScript code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React and React DOM:&lt;/strong&gt; Handle the React component tree and rendering updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nginx only serves the files. React actually runs inside the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Important Nginx Configuration for Single-Page Applications
&lt;/h2&gt;

&lt;p&gt;When serving a React application using Nginx, we need to handle client-side routing properly.&lt;/p&gt;

&lt;p&gt;For example, suppose our application has a route:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;When a user navigates to this route through React Router, everything works because React handles the routing inside the browser.&lt;/p&gt;

&lt;p&gt;However, if the user refreshes the page, the browser sends a request for &lt;code&gt;/dashboard&lt;/code&gt; directly to Nginx.&lt;/p&gt;

&lt;p&gt;Nginx may look for a physical file or directory named &lt;code&gt;dashboard&lt;/code&gt;. Since it doesn't exist, it can return a 404 error.&lt;/p&gt;

&lt;p&gt;To solve this, we configure Nginx to fall back to &lt;code&gt;index.html&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;try_files&lt;/span&gt; $&lt;span class="n"&gt;uri&lt;/span&gt; $&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that requests for frontend routes are handled by the React application.&lt;/p&gt;

&lt;p&gt;We can also configure caching for static assets and compression to improve performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Runtime Configuration: Changing the Backend URL Without Rebuilding the Image
&lt;/h2&gt;

&lt;p&gt;Now let's understand an important problem that occurs when deploying frontend applications across different environments.&lt;/p&gt;

&lt;p&gt;Suppose our frontend application communicates with a backend API.&lt;/p&gt;

&lt;p&gt;We might have different backend URLs for different environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Development:&lt;/strong&gt; &lt;code&gt;http://localhost:5000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Staging:&lt;/strong&gt; &lt;code&gt;https://staging-api.example.com&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production:&lt;/strong&gt; &lt;code&gt;https://api.example.com&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How do we manage these URLs?&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem With Build-Time Environment Variables
&lt;/h3&gt;

&lt;p&gt;In Vite, we commonly define environment variables using the &lt;code&gt;VITE_&lt;/code&gt; prefix.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VITE_API_URL=https://api.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And access them inside our application using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VITE_API_URL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem is that Vite replaces these environment variable references with their actual values during the build process.&lt;/p&gt;

&lt;p&gt;This means the API URL becomes part of the generated JavaScript bundle.&lt;/p&gt;

&lt;p&gt;So, if we want to change the backend URL after building the Docker image, we need to rebuild the image.&lt;/p&gt;

&lt;p&gt;This is inconvenient because the same frontend image cannot easily be reused across different environments with different backend URLs.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Solution: Runtime Configuration
&lt;/h3&gt;

&lt;p&gt;Instead of injecting the backend URL during the build process, we can inject it when the Docker container starts.&lt;/p&gt;

&lt;p&gt;We can achieve this using a runtime configuration file.&lt;/p&gt;

&lt;p&gt;The basic idea is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create a runtime configuration template&lt;/strong&gt;, such as &lt;code&gt;env-config.js.template&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Define a placeholder&lt;/strong&gt; for the backend URL inside it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pass the actual backend URL as an environment variable&lt;/strong&gt; when starting the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;envsubst&lt;/code&gt;&lt;/strong&gt; to replace the placeholder with the actual URL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generate the final &lt;code&gt;env-config.js&lt;/code&gt; file&lt;/strong&gt; inside the Nginx web root.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, our template could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__ENV__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="no"&gt;API_URL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"${API_URL}"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the container starts, the environment variable is substituted, generating a configuration file containing the actual backend URL.&lt;/p&gt;

&lt;p&gt;We can then load this file through &lt;code&gt;index.html&lt;/code&gt; before loading our React application.&lt;/p&gt;

&lt;p&gt;Inside React, we can access the URL using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;API_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__ENV__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;API_URL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, whenever we start the container, we can provide a different backend URL without rebuilding the Docker image.&lt;/p&gt;

&lt;p&gt;For example, the same image can be used in both staging and production by passing different environment variables during container startup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Runtime configuration is accessible to the browser. Therefore, it should only contain public configuration values, such as API URLs and feature flags. Never expose passwords, private keys, or other secrets through frontend configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Conclusion
&lt;/h2&gt;

&lt;p&gt;Docker helps us create a consistent and manageable environment for building and serving frontend applications.&lt;/p&gt;

&lt;p&gt;The important concepts to remember are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Docker images are immutable packages, while containers are running instances of those images.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dockerfiles define how images are built, while Docker Compose defines how services run and communicate.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-stage builds separate the frontend build process from the final serving environment.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nginx serves static files, while the browser executes JavaScript and React handles UI rendering.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nginx needs proper SPA routing configuration to handle frontend routes.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Runtime configuration allows us to change backend URLs without rebuilding the Docker image.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these concepts gives us a clear picture of how a frontend application is built, containerized, and served in a production environment.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>webdev</category>
      <category>devops</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
