Skip to content

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

  1. Install Gatsby CLI globally:
    Terminal window
    npm install -g gatsby-cli
  2. Create a new Gatsby app:
    Terminal window
    gatsby new my-gatsby-app
    cd my-gatsby-app
  3. Start the development server:
    Terminal window
    gatsby develop
    Your app should be running at http://localhost:8000.

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.

  1. Push your Gatsby app to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project.
  4. 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
  5. 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:

  1. Add a Dockerfile to your project root. Example:
    # Build stage
    FROM node:20-alpine AS builder
    WORKDIR /app
    COPY package*.json ./
    RUN npm install -g gatsby-cli && npm install
    COPY . .
    RUN gatsby build
    # Production stage
    FROM nginx:alpine
    COPY --from=builder /app/public /usr/share/nginx/html
    EXPOSE 80
    CMD ["nginx", "-g", "daemon off;"]
  2. Push your code (with Dockerfile) to GitHub.
  3. In Klutch.sh, create a new app and select the Dockerfile option when prompted.
  4. Set service details and environment variables as needed.
  5. 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.