Skip to content

Commit

Permalink
feat: support for setting env variable when running container
Browse files Browse the repository at this point in the history
  • Loading branch information
birme authored and martinstark committed May 10, 2024
1 parent eb4f3cc commit 9bfc503
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
42 changes: 16 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
FROM node:21-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
# RUN apk add --no-cache libc6-compat g++ cmake tar make
FROM nginx:1.19.0
ARG PORT=8080
ARG MANAGER_URL
EXPOSE $PORT
EXPOSE $MANAGER_URL

RUN apt-get update
RUN apt-get install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_21.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g yarn
RUN mkdir /app
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN yarn --immutable

# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Set backed url to empty string to use window.location.origin fallback
RUN VITE_BACKEND_URL="" yarn build

# Production image, copy all the files and run next
FROM nginx:1.19.0 AS runner
WORKDIR /usr/share/nginx/html/app
RUN rm -rf ./*

ENV NODE_ENV production
RUN yarn --immutable

COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/dist .
RUN chmod +x /app/scripts/entrypoint.sh

ENTRYPOINT [ "nginx", "-g", "daemon off;" ]
ENV NODE_ENV production
ENTRYPOINT [ "/app/scripts/entrypoint.sh" ]
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ To use a local manager instance, set `VITE_BACKEND_URL=http://0.0.0.0:8000/`

`yarn dev` to start a dev server

## Docker Container

Build local Docker image

```
docker build -t intercom-frontend:dev
```

Run container on port 8080 and with intercom manager on https://intercom-manager.dev.eyevinn.technology/

```
docker run --rm -d -p 8080:8080 \
-e PORT=8080 \
-e MANAGER_URL=https://intercom-manager.dev.eyevinn.technology/ \
--name=frontend \
intercom-frontend:dev
```

Then the app is available at http://localhost:8080/

Stop container

```
docker stop frontend
```

## Contributing

Contributions to are welcome.
Expand Down
2 changes: 1 addition & 1 deletion nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ server {
listen 8080;
server_name frontend;
location / {
root /usr/share/nginx/html/app;
root /usr/share/nginx/html;
index index.html;
}
}
7 changes: 7 additions & 0 deletions scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

sed -i "s/listen\s*8080;/listen $PORT;/" /etc/nginx/conf.d/default.conf

VITE_BACKEND_URL=$MANAGER_URL yarn build && \
cp -r /app/dist/* /usr/share/nginx/html/ && \
nginx -g 'daemon off;'

0 comments on commit 9bfc503

Please sign in to comment.