Deploying Bun application to Fly.io
Oct 30, 2023
Recently built an API server with Elysia and Bun to scrape meta data from any URL. Here are a few things I did to deploy the app smoothly on Fly.io in production mode.
Dockerfile with necessary steps to, here is the docker file I used
FROM oven/bun AS base
# Install dependencies only when needed
FROM base AS deps
COPY package.json bun.lockb ./
RUN bun install
# Development image, copy all the files and run the server
FROM base AS run
# Copy files
COPY --from=deps /app/node_modules/ ./node_modules/
COPY src/ ./src/
COPY public/ ./public/
COPY .env* package.json tsconfig.json ./
RUN bun build:prod
# Set environment variables
CMD bun run start
ENV PORT 3000
ENV HOSTNAME localhost
Most of the file is self explanatory, as we are just copying files and installing dependencies.
The unknowns in the file are
Looking at the project
package.json scripts section, we can find the details of them
"description": "An API server for retrieving meta data of web pages",
"build:prod": "bun build --target=bun src/index.ts --outfile=dist/index.js",
"start": "bun dist/index.js"
we are running
Once you have the above things setup, you can run
this will start a CLI wizard which will gather details about the app name, region to deploy and it will create a
fly.toml file with the config.
commit your config file and we are all set to deploy it using
if everything went well the command should log the hosted app url.
If you are curious to checkout the application I deployed on Fly.io, check out the project on rvgpl/meta and the deployed url is getmetadata.fly.dev.