Skip to content

Serving static with nginx #168

@Gleonett

Description

@Gleonett

For static you can use nginx instead of yarn to reduce the amount of resources consumption

For template React application

  • nginx uses ~8 mb RAM
  • yarn uses >500 mb RAM

Example Dockerfile:

FROM node:lts-alpine as builder

WORKDIR /app
ARG CEREBRUM_API_CLIENT_ENDPOINT
ENV CEREBRUM_API_CLIENT_ENDPOINT=${CEREBRUM_API_CLIENT_ENDPOINT}

COPY package.json tsconfig.json ./
COPY public/ public/
COPY src/ src/

RUN yarn && yarn build

FROM nginx:1.25.1-alpine

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build /usr/share/nginx/html

RUN touch /var/run/nginx.pid &&  \
    chown -R nginx:nginx /var/run/nginx.pid /usr/share/nginx/html /var/cache/nginx /var/log/nginx /etc/nginx/conf.d
USER nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Example nginx.conf:

server_tokens off;
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri /index.html;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions