From f06ba38c591109e78b32bc859c03cbfb801ca684 Mon Sep 17 00:00:00 2001 From: Rishab Kumar Date: Sat, 25 May 2024 13:09:20 -0400 Subject: [PATCH 1/3] added dockerfile --- Dockerfile | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fde216c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +## Base ######################################################################## +# Use a larger node image to do the build for native deps (e.g., gcc, python) +FROM node:lts as base + +# Reduce npm log spam and colour during install within Docker +ENV NPM_CONFIG_LOGLEVEL=warn +ENV NPM_CONFIG_COLOR=false + +# We'll run the app as the `node` user, so put it in their home directory +WORKDIR /home/node/app +# Copy the source code over +COPY --chown=node:node . /home/node/app/ + +## Development ################################################################# +# Define a development target that installs devDeps and runs in dev mode +FROM base as development +WORKDIR /home/node/app +# Install (not ci) with dependencies, and for Linux vs. Linux Musl (which we use for -alpine) +RUN npm install +# Switch to the node user vs. root +USER node +# Expose port 3000 +EXPOSE 3000 +# Start the app in debug mode so we can attach the debugger +CMD ["npm", "start"] + +## Production ################################################################## +# Also define a production target which doesn't use devDeps +FROM base as production +WORKDIR /home/node/app +COPY --chown=node:node --from=development /home/node/app/node_modules /home/node/app/node_modules +# Build the Docusaurus app +RUN npm run build + +## Deploy ###################################################################### +# Use a stable nginx image +FROM nginx:stable-alpine as deploy +WORKDIR /home/node/app +# Copy what we've installed/built from production +COPY --chown=node:node --from=production /home/node/app/build /usr/share/nginx/html/ \ No newline at end of file From b046e0923fd87638995a62dfe6f1085d23820e1f Mon Sep 17 00:00:00 2001 From: rishabkumar7 Date: Sat, 25 May 2024 15:14:38 -0400 Subject: [PATCH 2/3] optimized the dockerfile --- Dockerfile | 70 +++++++++++++++++++++++++++------------------------- package.json | 2 +- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/Dockerfile b/Dockerfile index fde216c..612a465 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,40 +1,42 @@ -## Base ######################################################################## -# Use a larger node image to do the build for native deps (e.g., gcc, python) +# Stage 1: Base image. FROM node:lts as base +## Disable colour output from yarn to make logs easier to read. +ENV FORCE_COLOR=0 +## Enable corepack. +RUN corepack enable +## Set the working directory to `/opt/docusaurus`. +WORKDIR /opt/docusaurus -# Reduce npm log spam and colour during install within Docker -ENV NPM_CONFIG_LOGLEVEL=warn -ENV NPM_CONFIG_COLOR=false - -# We'll run the app as the `node` user, so put it in their home directory -WORKDIR /home/node/app -# Copy the source code over -COPY --chown=node:node . /home/node/app/ - -## Development ################################################################# -# Define a development target that installs devDeps and runs in dev mode -FROM base as development -WORKDIR /home/node/app -# Install (not ci) with dependencies, and for Linux vs. Linux Musl (which we use for -alpine) -RUN npm install -# Switch to the node user vs. root -USER node -# Expose port 3000 +# Stage 2a: Development mode. +FROM base as dev +## Set the working directory to `/opt/docusaurus`. +WORKDIR /opt/docusaurus +## Expose the port that Docusaurus will run on. EXPOSE 3000 -# Start the app in debug mode so we can attach the debugger -CMD ["npm", "start"] +## Run the development server. +CMD [ -d "node_modules" ] && npm run start --host 0.0.0.0 --poll 1000 || npm run install && npm run start --host 0.0.0.0 --poll 1000 -## Production ################################################################## -# Also define a production target which doesn't use devDeps -FROM base as production -WORKDIR /home/node/app -COPY --chown=node:node --from=development /home/node/app/node_modules /home/node/app/node_modules -# Build the Docusaurus app +# Stage 2b: Production build mode. +FROM base as prod +## Set the working directory to `/opt/docusaurus`. +WORKDIR /opt/docusaurus +## Copy over the source code. +COPY . /opt/docusaurus/ +## Install dependencies with `--immutable` to ensure reproducibility. +RUN npm ci +## Build the static site. RUN npm run build -## Deploy ###################################################################### -# Use a stable nginx image -FROM nginx:stable-alpine as deploy -WORKDIR /home/node/app -# Copy what we've installed/built from production -COPY --chown=node:node --from=production /home/node/app/build /usr/share/nginx/html/ \ No newline at end of file +# Stage 3a: Serve with `docusaurus serve`. +FROM prod as serve +## Expose the port that Docusaurus will run on. +EXPOSE 3000 +## Run the production server. +CMD ["npm", "run", "serve", "--", "--host", "0.0.0.0", "--no-open"] + +# Stage 3b: Serve with Caddy. +FROM caddy:2-alpine as caddy +## Copy the Caddyfile. +COPY --from=prod /opt/docusaurus/Caddyfile /etc/caddy/Caddyfile +## Copy the Docusaurus build output. +COPY --from=prod /opt/docusaurus/build /var/docusaurus \ No newline at end of file diff --git a/package.json b/package.json index 4eedc72..dd57c29 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "docusaurus": "docusaurus", - "start": "docusaurus start", + "start": "docusaurus start --host 0.0.0.0", "build": "docusaurus build", "swizzle": "docusaurus swizzle", "deploy": "docusaurus deploy", From ecef1736d50adeaf8fb77edf3d790750b9e76d86 Mon Sep 17 00:00:00 2001 From: Rishab Kumar Date: Tue, 28 May 2024 11:51:15 -0400 Subject: [PATCH 3/3] added docker instructions --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index eec2263..65a5283 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,36 @@ We would love to be your first PR! or any PR for that matter. Take a look at our If you have ideas for updating the guide content, please open a PR and we would take a look at it. +## Docker Support + +You can pull the public image from docker hub [rishabkumar7/ltc-website](https://hub.docker.com/r/rishabkumar7/ltc-website) or build it locally. + +### Building the Docker Image: + +To build the docker image you will need to run the following command: + +``` bash +docker build --target -t . +``` + +- `--target ` - This is the target to build. The target is the name of the stage in the dockerfile. Valid targets are `dev`, `serve` and `caddy` +- `-t ` - This is the name and tag of the image that will be built. The format is :. The name can be anything you want. The tag is optional. If you do not specify a tag, latest will be used. + +- `.` - This is the path to the build context. In this case we are using the current directory (root directory of this project) as the build context. + +### Running the Docker Image + +To run the serve target you will need to run the following command: + +``` bash +docker run --rm -d -p 3000:3000 +``` + +- `--rm` - This is an optional flag that will remove the container when it exits. +- `-d` - This is an optional flag that will run the container in detached mode. +- `-p 3000:3000` - This is an optional flag that will map port 3000 on the host to port 3000 in the container. +- `` - This is the name and tag of the image that will be run. Make sure to use the same tag that you used when building the image. + ## License Distributed under the MIT License. See [LICENSE](/LICENSE) for more information.