# Dockerfile to run Astro Node SSR on fly.io

> The Dockerfile I use to run an Astro Node SSR app on fly.io, plus the .dockerignore and fly.toml you need to make fly deploy work without errors.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2024-01-03 | Topics: [Astro](https://flaviocopes.com/tags/astro/), [Docker](https://flaviocopes.com/tags/docker/) | Canonical: https://flaviocopes.com/dockerfile-to-run-astro-node-ssr-on-flyio/

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

You deploy from your local [Astro](https://flaviocopes.com/astro-introduction/) project directly.

I'll probably make another post for that.

Also read [run an app on fly.io](https://flaviocopes.com/run-an-app-on-flyio/).

[Also see my Docker tutorials](https://flaviocopes.com/tags/docker/).

I also built a free [Dockerfile generator](https://flaviocopes.com/tools/dockerfile-generator/) that creates Dockerfiles for Astro, Node.js, Next.js, Bun and static sites.

`Dockerfile`:

```docker
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:

```docker
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):

```toml
.DS_Store
node_modules
dist
.env
.git
.gitignore
```

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

```toml
# 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:

```toml
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.
