Skip to content

Commit

Permalink
docker: Reduced image size
Browse files Browse the repository at this point in the history
  • Loading branch information
CSantosM committed Sep 23, 2024
1 parent 23859d6 commit b26bff5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
55 changes: 37 additions & 18 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
# Build OpenVidu Call for production
FROM --platform=linux/amd64 node:20 as openvidu-call-build
### Stage 1: Build the frontend
FROM --platform=linux/amd64 node:20 as build-frontend

WORKDIR /openvidu-call
USER node

WORKDIR /app/frontend

ARG BASE_HREF=/

COPY . .
COPY --chown=node:node openvidu-call-front/package*.json ./

RUN npm install

COPY --chown=node:node openvidu-call-front/ ./

RUN npm run build

### Stage 2: Build the backend
FROM node:20 as build-backend

USER node

# Install openvidu-call-back dependencies
RUN cd openvidu-call-back && npm install && \
npm run build
WORKDIR /app/backend

# Build openvidu-call-back frontend
RUN cd openvidu-call-front && npm install && \
npm run prod:build ${BASE_HREF} && \
cd ../ && rm -rf openvidu-call-front
COPY --chown=node:node openvidu-call-back/package*.json ./

FROM node:20-alpine3.19
RUN npm install

COPY --chown=node:node openvidu-call-back/ ./

RUN npm run build

# Copy static files from the frontend build
COPY --from=build-frontend /app/frontend/dist/openvidu-call /app/backend/dist/public


### Stage 3: Final production image
FROM node:20-alpine as production

WORKDIR /opt/openvidu-call

COPY --from=openvidu-call-build /openvidu-call/openvidu-call-back .
COPY --from=build-backend /app/backend/dist ./dist
COPY --from=build-backend /app/backend/package*.json ./

RUN npm install --production && npm cache clean --force

# Entrypoint
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN apk add --no-cache curl && \
chmod +x /usr/local/bin/entrypoint.sh && \
chown -R node:node /opt/openvidu-call

USER node
RUN chmod +x /usr/local/bin/entrypoint.sh && \
chown -R node:node /opt/openvidu-call

EXPOSE $SERVER_PORT

Expand Down
1 change: 1 addition & 0 deletions docker/create_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ IMAGE=${1:-?echo "Error: You need to specify an image name as first argument"?}
if [[ -n $IMAGE ]]; then
cd ..
export BUILDKIT_PROGRESS=plain && docker build --pull --no-cache --rm=true -f docker/Dockerfile -t "$IMAGE" --build-arg BASE_HREF=/ .
echo "Docker image '$IMAGE' built successfully."
else
echo "Error: You need to specify an image name as first argument"
fi
17 changes: 17 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ terminate_process() {
# Trap termination signals
trap terminate_process TERM INT

if [ -z "${LIVEKIT_URL}" ]; then
echo "LIVEKIT_URL is required"
echo "example: docker run -e LIVEKIT_URL=https://livekit-server:7880 -e LIVEKIT_API_KEY=api_key -e LIVEKIT_API_SECRET=api_secret -p 6080:6080 openvidu-call"
exit 1
fi
if [ -z "${LIVEKIT_API_KEY}" ]; then
echo "LIVEKIT_API_KEY is required"
echo "example: docker run -e LIVEKIT_URL=https://livekit-server:7880 -e LIVEKIT_API_KEY=api_key -e LIVEKIT_API_SECRET=api_secret -p 6080:6080 openvidu-call"
exit 1
fi
if [ -z "${LIVEKIT_API_SECRET}" ]; then
echo "LIVEKIT_API_SECRET is required"
echo "example: docker run -e LIVEKIT_URL=https://livekit-server:7880 -e LIVEKIT_API_KEY=api_key -e LIVEKIT_API_SECRET=api_secret -p 6080:6080 openvidu-call"
exit 1
fi


cd /opt/openvidu-call || { echo "Can't cd into /opt/openvidu-call"; exit 1; }
node dist/src/server.js &

Expand Down

0 comments on commit b26bff5

Please sign in to comment.