Skip to content
This repository has been archived by the owner. It is now read-only.

alexxxnf/spa-docker-example

Repository files navigation

Docker SPA Example

This project shows how to use Docker to build and serve a single page application.

As an example of SPA, I use Angular, but my approach with minor modifications can be applied to any framework.

If you use CDN or something else to serve your app, then this repo is probably not for you.

Goals

Create a Docker image that can be easily run on Kubernetes or Docker-compose in both production and development environments. The image should be

  • simple to use
  • small (in terms of RAM and HDD)
  • configurable via environment variables
  • effective in serving static files.

High level overview

In order to keep the final image as small as possible, I use multistage build process.

The first stage installs dependencies and builds the app. I created a custom base image for that purpose. It contains NPM, yarn, node-gyp. It also has gzip and Brotli to produce pre-compressed static files.

The second stage copies the application into custom-made nginx image. That concludes the building process.

When a container based on the image runs for the first time, a special script replaces configuration variables with the values from environmental variables. Ii also compresses the files and deals with hashes.

For security reasons, consider using your own base images.

How to use it

If you have an Angular-based SPA, using this approach takes two simple steps.

  1. Replace actual values in environment.prod.ts with placeholders that looks like this: ${TITLE}.
  2. Copy the docker directory from this project to your repo.

Then you can build your image and create a container based on it to run anywhere you like.

Building Docker images

Client Side Rendering

In the root directory run docker build -t <TAG_NAME> -f docker/csr/Dockerfile ..

Server Side Rendering

In the root directory run docker build -t <TAG_NAME> -f docker/ssr/Dockerfile ..

Running Docker container

Client Side Rendering

Run docker run -p 80:80 -e TITLE="Title passed via env vars" <TAG_NAME> or use docker/csr/docker-compose.yaml.

Server Side Rendering

Server Side Rendering requires simultaneous run of two containers. See docker/ssr/docker-compose.yaml for example.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published