Gatsby and Tailwind are a formidable combination for putting together a robust and attractive site. Throw Docker into the mix and you also have robust and reliable deployments. Hereβs how to set that up for a minimal site.
π TL;DR The code for this project can be found here.
Install Gatsby CLI & Other Packages
There are various ways to do this. See this for the options.
Youβll also need to install the Gatsby package, along with Tailwind and a few other dependencies.
npm install gatsby tailwindcss postcss autoprefixerCreate Gatsby Project
Use gatsby to create a new project folder.
gatsby new gatsby-tailwindπ‘ Depending on how you installed the Gatsby CLI you might need to precede that command with npx.
And move into that folder.
cd gatsby-tailwindThis is what the initial project structure should look like:
tree -I node_modules.
βββ gatsby-browser.js
βββ gatsby-config.js
βββ gatsby-node.js
βββ gatsby-ssr.js
βββ LICENSE
βββ package.json
βββ package-lock.json
βββ README.md
βββ src
βββ components
β βββ header.js
β βββ index.module.css
β βββ layout.css
β βββ layout.js
β βββ seo.js
βββ images
β βββ example.png
β βββ gatsby-icon.png
βββ pages
β βββ 404.js
β βββ index.js
β βββ page-2.js
β βββ using-ssr.js
β βββ using-typescript.tsx
βββ templates
βββ using-dsg.js
6 directories, 21 files
Initialise & Configure Tailwind
Now initialise Tailwind.
npx tailwindcss init -pThat will add two new files: tailwind.config.js and postcss.config.js.
To enable JIT mode we need to add the paths to the site template files to tailwind.config.js.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx},"
],
theme: {
extend: {},
},
plugins: [],
}Next we create a CSS file for the project in src/styles/global.css with the following content.
@tailwind base;
@tailwind components;
@tailwind utilities;And import that file in gatsby-browser.js.
import "./src/styles/global.css"Test on Host
With all of that setup in place we can launch the project directly on the host. Start a development server.
gatsby developπ‘ Use npx if you need to!
Open http://127.0.0.1:8000/ in your browser to see the minimal Tailwind-enabled site.
Actually Use Tailwind
It might not have escaped your attention that at this point we have not actually used Tailwind in the project. Letβs fix that. Edit src/pages/index.js.
import React from "react"
export default function Home() {
return (
<div className="min-h-screen bg-gray-100 flex flex-col justify-center items-center">
<h1 className="text-5xl text-blue-600 font-bold">
Gatsby & Tailwind!
</h1>
<p className="text-lg text-gray-700 mt-4">
A sample project using Gatsby and Tailwind CSS.
</p>
</div>
)
}Restart Gatsby and head over to http://127.0.0.1:8000/. Our test site has become a lot more minimal. But now it uses a bunch of Tailwind classes (take a careful look at the definition of the React component above, paying attention to the className attributes). Whatβs happened behind the scenes is the Tailwind JIT compiler has generated the required (and minimal) CSS for the project. Take a look at http://127.0.0.1:8000/commons.css to see for yourself.
Wrap it in Docker!
Letβs start with some housekeeping. Create a .dockerignore file. Weβll use this to reduce the volume of data being copied into the Docker build context.
node_modules
.cache
public
Now create Dockerfile.
FROM node:18-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm install -g gatsby-cli
RUN gatsby build
CMD ["gatsby", "serve", "-H", "0.0.0.0"]Build the Docker image.
docker build -t gatsby-tailwind .And finally create a running container.
docker run --rm -p 8000:9000 gatsby-tailwindHead back to http://127.0.0.1:8000/ to see the site now served from the Docker container. If you try to use http://127.0.0.1:8000/commons.css to see the CSS though youβll be disappointed. Take a look at the contents of the <head> in the page at http://127.0.0.1:8000/ (youβll need to use Developer Tools in your browser to do this) and youβll find that the CSS path is now a bit of a random muddle.
<head>
<style data-href="/styles.7ad68bcdaf01d23bf817.css" data-identity="gatsby-global-css">
</head>If you go to http://127.0.0.1:8000/styles.7ad68bcdaf01d23bf817.css though youβll see the CSS rendered by Tailwind JIT.
The difference comes from the fact that we are running a production server on the Docker container (see the Dockerfile where we launched Gatsby using gatsby serve). You can restore the development server by changing the last line of the Dockerfile.
CMD ["gatsby", "develop", "-H", "0.0.0.0"]
Rebuild the Docker image and then run. Make sure to update the port mapping because the development server runs on port 8000.
docker run --rm -p 8000:8000 gatsby-tailwindYou should now be able to open http://127.0.0.1:8000/commons.css again (but this time from the Docker container).
π³ See this for a similar approach using Next.js.