From 4db1154ecdc0bcf61c141be91bf54e7ca6c06c30 Mon Sep 17 00:00:00 2001 From: aronmal Date: Fri, 5 Jan 2024 01:29:42 +0100 Subject: [PATCH] Add docker --- .dockerignore | 15 ++++---- Dockerfile | 53 ++++++++++++++++++++++---- docker.sh | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 14 deletions(-) create mode 100755 docker.sh diff --git a/.dockerignore b/.dockerignore index 5fd749a..c46cdac 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,8 @@ -.fleet/ -.idea/ -dist/ -node_modules/ -dist/ -.DS_Store/ - +Dockerfile +.dockerignore +node_modules +npm-debug.log +README.md +.output +.vinxi +.git diff --git a/Dockerfile b/Dockerfile index 3616810..4bc1d1f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,50 @@ -FROM node:lts AS build +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 WORKDIR /app -COPY package*.json ./ -RUN npm install + +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +RUN \ + if [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then npm install --global pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules COPY . . + RUN npm run build -FROM nginx:alpine AS runtime -COPY ./nginx/nginx.conf /etc/nginx/nginx.conf -COPY --from=build /app/dist /usr/share/nginx/html -EXPOSE 8080 +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV production + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 solidjs + +COPY --from=builder /app/public ./public + +COPY --from=builder --chown=solidjs:nodejs /app/.output ./.output +COPY --from=builder --chown=solidjs:nodejs /app/.vinxi ./.vinxi + +RUN npm install vinxi + +USER solidjs + +EXPOSE 3000 + +ENV PORT 3000 +# set hostname to localhost +ENV HOSTNAME "0.0.0.0" + +CMD ["npx", "vinxi", "start"] diff --git a/docker.sh b/docker.sh new file mode 100755 index 0000000..1076045 --- /dev/null +++ b/docker.sh @@ -0,0 +1,100 @@ +#!/bin/bash + +# Script: your_script.sh +# Description: Brief description of what the script does. +# Usage: ./your_script.sh [options] +# Options: +# -h, --help Display this help message +# -o, --option Specify an option + +# Function to display usage information +function show_usage() { + echo "Usage: $0 [options]" + echo "Options:" + echo " -h, --help Display this help message" + echo " -d, --deploy Delete possibly old images and container and deploy a new container" + echo " -f, --follow If deployed will follow the log output" + echo " -s, --stay If deployed will leave the container running instead of stopping it" +} + +# Check for the number of arguments +# if [ "$#" -eq 0 ]; then +# show_usage +# exit 1 +# fi + +# Parse command-line options +while [[ "$#" -gt 0 ]]; do + case $1 in + -h|--help) + show_usage + exit 0 + ;; + -d|--deploy) + deploy=true + ;; + -f|--follow) + follow=true + ;; + *) + echo "Unknown option: $1" + show_usage + exit 1 + ;; + esac + shift +done + +image_name="solidjs-docker" +container_name="solidjs" + + + +if [ $deploy ]; then + # Check if the container exists + if [ "$(docker ps -a -q -f name=$container_name)" ]; then + if [ "$(docker ps -q -f name=$container_name)" ]; then + # Container exists, stop and remove it + echo "Stopping and removing existing container..." + echo "Stopped $(docker stop $container_name)" + else + echo "Removing existing container..." + fi + echo "Removed $(docker rm $container_name)" + + + docker rmi $(docker images -q $image_name) + fi +fi + +# Run Docker container in the background and capture container ID +docker build -t $image_name . + +if [ $deploy ]; then + container_id=$(docker run --name $container_name -p 3000:3000 -d $image_name) + echo "Container deployed: $container_id" +elif [ !$follow ]; then + exit 0 +fi + + +# Function to display a message after interrupt +function exit_handler() { + # Clear the current line + echo -e "\033[2K" + + if [ $stay ]; then + echo "Container is still running. To stop it, use:" + echo "docker stop $container_id" + else + echo "Stopping the container ..." + docker stop $container_id + echo "Stopped." + fi +} + +# Register the exit_handler function to be called when the script receives the INT signal (Ctrl+C) +trap exit_handler INT + +# Run Docker logs on the specified container +docker logs -f $container_id