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.
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.mjsNOTE: 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.mjsThis 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
.gitignoreAfter 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 = 1024For 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.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.