1. Introduction: Where Does Frontend Code Run?

Unlike backend applications, which typically run on servers, frontend applications run inside the user's browser.

For example, when we build a React application, the browser executes the JavaScript code, renders the UI, and handles user interactions.

So, if frontend code runs in the browser, why do we need Docker?

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.

Before understanding how Docker works with frontend applications, let's understand some basic Docker concepts.

2. Understanding Docker Fundamentals

What is Docker?

Docker is a platform that allows us to package applications and their dependencies into containers.

It helps avoid issues caused by differences in software environments, such as different dependency versions, libraries, and runtime configurations.

Docker Image vs. Docker Container

These are two fundamental concepts in Docker.

  • Docker Image: An image is an immutable package containing everything required to run an application, such as application code, dependencies, and necessary runtime components.
  • Docker Container: A container is a running instance of a Docker image.

In simple terms, an image is the blueprint, while a container is the running instance created from that blueprint.

Dockerfile vs. Docker Compose

Docker provides two important components for defining and managing applications.

Dockerfile

A Dockerfile defines the instructions required to build a Docker image.

It specifies things such as:

  • The base image.
  • Dependencies to install.
  • Files to copy.
  • Commands to execute.
  • How the application should be prepared for execution.

Docker Compose

Docker Compose defines how different services should run and communicate with each other.

It allows us to configure:

  • Services and containers.
  • Port mappings.
  • Environment variables.
  • Networks.
  • Volumes.

For example, we can use Docker Compose to run our frontend application alongside a backend application.

3. Dockerizing a Frontend Application Using Multi-Stage Builds

When Dockerizing a frontend application, we commonly use a multi-stage build process.

The idea is simple: first, build the frontend application, and then serve the generated static files using an Nginx server.

Stage 1: Building the Frontend

Let's assume we have a React application built using Vite.

We first install the required dependencies and execute:

npm run build 

Vite processes our React application and generates optimized static files.

These files generally include:

  • HTML files.
  • JavaScript bundles.
  • CSS files.
  • Other static assets.

The generated output is stored inside the dist/ directory.

Stage 2: Serving the Build Using Nginx

Once the build is generated, we copy the contents of the dist/ directory into the Nginx server's web root:

/usr/share/nginx/html 

Nginx then serves these static files whenever a user requests the frontend application.

It can also handle compression, such as Gzip, to reduce the size of transferred files.

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

This keeps the final Docker image lightweight.

4. Understanding How React Actually Runs in the Browser

One important thing to understand is that Nginx does not execute React or provide a separate React execution engine.

Let's understand what actually happens.

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.

Now, when a user opens our application:

  1. The browser sends an HTTP request to the Nginx server.
  2. Nginx responds with index.html and the required JavaScript and CSS files.
  3. The browser downloads these files.
  4. The browser's JavaScript engine executes the downloaded JavaScript.
  5. The bundled React and React DOM libraries execute within the browser.
  6. React creates and maintains its element tree and handles UI updates by updating the actual browser DOM.

So, the important distinction is:

  • Nginx: Delivers the HTML, CSS, and JavaScript files.
  • Browser JavaScript Engine: Executes the JavaScript code.
  • React and React DOM: Handle the React component tree and rendering updates.

Nginx only serves the files. React actually runs inside the browser.

5. Important Nginx Configuration for Single-Page Applications

When serving a React application using Nginx, we need to handle client-side routing properly.

For example, suppose our application has a route:

/dashboard 

When a user navigates to this route through React Router, everything works because React handles the routing inside the browser.

However, if the user refreshes the page, the browser sends a request for /dashboard directly to Nginx.

Nginx may look for a physical file or directory named dashboard. Since it doesn't exist, it can return a 404 error.

To solve this, we configure Nginx to fall back to index.html:

try_files $uri $uri/ /index.html; 

This ensures that requests for frontend routes are handled by the React application.

We can also configure caching for static assets and compression to improve performance.

6. Runtime Configuration: Changing the Backend URL Without Rebuilding the Image

Now let's understand an important problem that occurs when deploying frontend applications across different environments.

Suppose our frontend application communicates with a backend API.

We might have different backend URLs for different environments:

  • Development: http://localhost:5000
  • Staging: https://staging-api.example.com
  • Production: https://api.example.com

How do we manage these URLs?

The Problem With Build-Time Environment Variables

In Vite, we commonly define environment variables using the VITE_ prefix.

For example:

VITE_API_URL=https://api.example.com 

And access them inside our application using:

import.meta.env.VITE_API_URL 

The problem is that Vite replaces these environment variable references with their actual values during the build process.

This means the API URL becomes part of the generated JavaScript bundle.

So, if we want to change the backend URL after building the Docker image, we need to rebuild the image.

This is inconvenient because the same frontend image cannot easily be reused across different environments with different backend URLs.

The Solution: Runtime Configuration

Instead of injecting the backend URL during the build process, we can inject it when the Docker container starts.

We can achieve this using a runtime configuration file.

The basic idea is:

  1. Create a runtime configuration template, such as env-config.js.template.
  2. Define a placeholder for the backend URL inside it.
  3. Pass the actual backend URL as an environment variable when starting the container.
  4. Use envsubst to replace the placeholder with the actual URL.
  5. Generate the final env-config.js file inside the Nginx web root.

For example, our template could look like this:

window.__ENV__ = { API_URL: "${API_URL}" }; 

When the container starts, the environment variable is substituted, generating a configuration file containing the actual backend URL.

We can then load this file through index.html before loading our React application.

Inside React, we can access the URL using:

const API_URL = window.__ENV__.API_URL; 

Now, whenever we start the container, we can provide a different backend URL without rebuilding the Docker image.

For example, the same image can be used in both staging and production by passing different environment variables during container startup.

Important: 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.

7. Conclusion

Docker helps us create a consistent and manageable environment for building and serving frontend applications.

The important concepts to remember are:

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

Understanding these concepts gives us a clear picture of how a frontend application is built, containerized, and served in a production environment.