🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Deploy Astro to Fly.io with a Dockerfile

Deploy Astro to Fly.io: a Dockerfile for static or SSR builds, fly.toml with the right internal_port, fly launch and fly deploy, custom domains, and scale-to-zero.

#Astro#Fly.io#Docker#Deployment

To deploy Astro to Fly.io, add a Dockerfile, run fly launch to create the app and fly.toml, make sure internal_port matches the port your container listens on, and ship with fly deploy. A static site runs as an Nginx container; an SSR site runs the @astrojs/node standalone server. Fly.io then runs the container close to your users in the regions you choose.

Key Takeaways

  • Fly.io deploys containers, so you need a Dockerfile for either static or SSR Astro.
  • internal_port in fly.toml must match the port inside the container.
  • SSR needs HOST=0.0.0.0; static sites need Nginx listening on the configured port.
  • Machines can stop when idle and start on the next request, which keeps small sites cheap.

1. Install flyctl and sign in

# Terminal
brew install flyctl   # or see fly.io/docs for Linux and Windows
fly auth login

2. Add a Dockerfile

For a static site, build with Node.js and serve with Nginx on port 8080:

# Dockerfile
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 8080
# nginx.conf
server {
  listen 8080;
  root /usr/share/nginx/html;
  location /_astro/ { add_header Cache-Control "public, max-age=31536000, immutable"; }
  location / { try_files $uri $uri/ $uri.html =404; }
  error_page 404 /404.html;
}

For SSR, use the Node.js runtime image from our Docker tip, with @astrojs/node in standalone mode and HOST=0.0.0.0, PORT=8080. node:22-alpine installs the latest 22.x release, which meets Astro’s Node.js 22.23.4 minimum. Add a .dockerignore with node_modules, dist and .env.

3. Launch

# Terminal
fly launch

fly launch detects the Dockerfile, asks for an app name and region, and writes fly.toml. Review it before the first deploy:

# fly.toml
app = "my-astro-site"
primary_region = "lhr"

[build]

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = "stop"
  auto_start_machines = true
  min_machines_running = 0

internal_port must match listen 8080 in Nginx or the PORT your Node server uses. A mismatch shows up as the app deploying but never passing health checks. auto_stop_machines lets idle machines stop and start again on the next request; set min_machines_running = 1 if you cannot accept a cold start.

4. Deploy

# Terminal
fly deploy

Fly.io builds the image (remotely by default), creates machines and gives you an fly.dev URL. Run fly deploy again for each release, or call it from CI with a FLY_API_TOKEN secret.

Secrets and environment variables

# Terminal
fly secrets set API_KEY=your-value

Secrets are available at runtime only. Anything a prerendered page reads through import.meta.env must be present at build time, so pass it as a Docker build argument instead. Our environment variables guide explains the difference.

Custom domain

# Terminal
fly certs add www.example.com

Fly.io prints the DNS records to add. Once the certificate is issued, set site: 'https://www.example.com' in astro.config.mjs and redeploy. Our custom domain tip covers DNS in general.

If your site is fully static and you do not need Fly.io’s regions or a server, a static host is simpler; see free hosting for Astro and the static deploy docs. Themes from our Astro theme catalog work with the static Dockerfile above as-is.

FAQ

Does Fly.io support Astro?

Yes. Fly.io runs any Docker container, so a static Astro site runs in an Nginx image and an SSR site runs the @astrojs/node standalone server.

Why does my Astro app on Fly.io fail health checks?

Usually internal_port in fly.toml does not match the port the container listens on, or the Node server is bound to localhost. Match the ports and set HOST=0.0.0.0.

Can Fly.io scale an Astro site to zero?

Yes. With auto_stop_machines and min_machines_running = 0, idle machines stop and start on the next request. The first request after a stop takes longer.

Do I need SSR to deploy Astro on Fly.io?

No. A static build served by Nginx works well. SSR is only needed for routes that render on demand.