Deploying a Gatsby App
Gatsby is a modern static site generator for React, enabling developers to build fast, secure, and scalable websites. It leverages GraphQL, a rich plugin ecosystem, and optimized builds, making it ideal for content-driven and high-performance web projects.
This guide explains how to deploy a Gatsby application to Klutch.sh, both with and without a Dockerfile. It covers installation, sample code, and deployment steps.
Prerequisites
- Node.js & npm installed (Download)
- Git and GitHub account
- Klutch.sh account
Getting Started: Create a Gatsby App
- Install Gatsby CLI globally:
Terminal window npm install -g gatsby-cli - Create a new Gatsby app:
Terminal window gatsby new my-gatsby-appcd my-gatsby-app - Start the development server:
Your app should be running at http://localhost:8000.
Terminal window gatsby develop
Sample Code (src/pages/index.js)
Replace the contents of src/pages/index.js
with:
import * as React from "react"
export default function Home() { return <h1>Hello from Gatsby on Klutch.sh!</h1>}
Deploying Without a Dockerfile
Gatsby apps are static sites after build. Klutch.sh can deploy static sites directly.
- Push your Gatsby app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project.
- Create a new app:
- Select your Gatsby GitHub repository and branch
- Choose “Static Site” as the app type
- Set build command:
gatsby build
- Set output directory:
public
- Configure region, compute, and environment variables as needed
- Click “Create” to deploy. Klutch.sh will build and serve your static Gatsby app.
Deploying With a Dockerfile
If you need custom server logic or want to use a Dockerfile:
- Add a
Dockerfile
to your project root. Example:# Build stageFROM node:20-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm install -g gatsby-cli && npm installCOPY . .RUN gatsby build# Production stageFROM nginx:alpineCOPY --from=builder /app/public /usr/share/nginx/htmlEXPOSE 80CMD ["nginx", "-g", "daemon off;"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, create a new app and select the Dockerfile option when prompted.
- Set service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and serve your Gatsby app via Nginx.
Resources
Deploying Gatsby to Klutch.sh is fast and flexible. Use static site deployment for most cases, or Dockerfile for custom setups.