Skip to content

Dockerfile to run Astro Node SSR on fly.io

NOTE: this setup does not involve CI or auto deploy on commit.

You deploy from your local Astro project directly.

I’ll probably make another post for that.

Also read run an app on fly.io.

Also see my Docker tutorials.

Dockerfile:

FROM node:lts-slim as runtime
WORKDIR /app

# Ensure that both node_modules and package-lock.json are removed.
COPY package.json .
RUN rm -rf node_modules package-lock.json

# Perform a fresh installation of npm dependencies.
RUN npm install

# Copy the rest of your application files.
COPY . .

# Build your application.
RUN npm run build

# Set environment variables and expose the appropriate port.
ENV HOST=0.0.0.0
ENV PORT=3000
EXPOSE 3000

# Define the command to run your application.
CMD node ./dist/server/entry.mjs

NOTE: I found this Dockerfile in the official Astro docs:

FROM node:lts AS runtime
WORKDIR /app

COPY . .

RUN npm install
RUN npm run build

ENV HOST=0.0.0.0
ENV PORT=4321
EXPOSE 4321
CMD node ./dist/server/entry.mjs

This wasn’t working for me on fly.io, but the one listed before (thanks ChatGPT) worked.

Also very important, add this .dockerignore (or the build will fail):

.DS_Store
node_modules
dist
.env
.git
.gitignore

After running fly launch, from your local project folder, you should have this fly.toml file:

# fly.toml app configuration file generated for aha-test-flavio on 2024-01-03T13:48:33+01:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "<your app name>"
primary_region = "<region>"

[build]

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

[[vm]]
  cpu_kind = "shared"
  cpus = 1
  memory_mb = 1024

For environment variables, import existing .env ones using:

cat .env | fly secrets import

(or add them through Fly’s dashboard).

Now run fly deploy --ha=false, from your local project folder, to deploy.


→ I wrote 17 books to help you become a better developer:

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list