Deploy a Bun App

This guide explains how to deploy a Bun (opens in a new tab) application to Koyeb using:

  1. Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
  2. Pre-built containers you can deploy from any public or private registry.

You will need:

You can deploy and preview the Bun application from this guide by clicking the Deploy to Koyeb button below.

Deploy to Koyeb (opens in a new tab)

Consult the repository on GitHub (opens in a new tab) to view this example application.

Create the Bun app

Get started by creating a basic Bun application.

Alternatively, you can fork the repository on GitHub (opens in a new tab) to get a complete copy of the code. If you fork the repository, then you can skip to section on git-driven deployment on Koyeb.

In your terminal, run the following commands to create the directory that will hold the application code:

mkdir example-bun
cd example-bun

To generate a new Bun application, in the example-bun directory, run the following command:

bun init

The following output will appear.

? Select a project template - Press return to submit.
   Blank
    React
    Library

Toggle to the Library template, and click Enter. Input server.ts as the entry point:

✓ Select a project template: Library
package name (example-bun): 
entry point (index.ts): server.ts
 
 + .gitignore
 + .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc
 + server.ts
 + tsconfig.json (for editor autocomplete)
 + README.md
 
To get started, run:
 
    bun run server.ts
 
bun install v1.3.0 (b0a6feca)
 
+ @types/bun@1.3.0
+ typescript@5.9.3
 
7 packages installed [15.00ms]
 

This command initializes a Bun project and creates various files and folders:

  • .gitignore: To ignore files that should not be committed to the repository
  • server.ts: The main entry point of the application
  • tsconfig.json: To define the TypeScript configuration required to compile the project
  • README.md: A starter README file for your project
  • package.json: Basic Node.js configuration, metadata, and dependency file

Open the file called server.ts and add the following code:

const port = process.env.PORT || 3000
 
console.log(`Launching Bun HTTP server on port: ${port}, url: http://0.0.0.0:${port} 🚀`)
 
Bun.serve({
  port: port,
  fetch(_request) {
    return new Response('Hello from Koyeb')
  },
})

Run the Bun app locally

Use the following command to run the Bun application locally:

bun run ./server.ts

Navigate to http://0.0.0.0:3000 to view the "Hello from Koyeb" message.

Dockerize the Bun app

To deploy the Bun application on Koyeb, you need to create a Dockerfile in your project root directory. The Dockerfile is used to build and deploy the Bun application on Koyeb. Koyeb automatically rebuilds and deploys your app any time a change is detected on your branch.

Add the following to the Dockerfile:

Dockerfile
FROM oven/bun:1
WORKDIR /app
COPY . .
RUN bun install
 
ARG PORT
EXPOSE ${PORT:-3000}
 
CMD ["bun", "server.ts"]

This Dockerfile provides the minimum requirements to run the Bun application. You can extend it depending on your needs.

(Optional) Build and run the Bun app locally with Docker

If you have Docker installed, you can optionally choose to build and run the app locally with Docker.s

docker build -t example-bun . # Build the Docker image
docker run --name example-bun -p 3000:3000 example-bun # Run the Docker image

Visit the http://localhost:3000 URL to see the same "Hello from Koyeb" result, but now the application is running inside a container.

Push the project to GitHub

In the project directory, initialize a new git repository by running the following command:

git init

You will use this repository to version the application code and push the changes to a GitHub repository. Run the following commands to commit and push changes to your GitHub repository, replacing the GitHub username and repository name with values from your account and the GitHub repo name:

git add .
git commit -m "Initial commit"
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git push -u origin main

Replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Deploy to Koyeb using git-driven deployment

To deploy the Bun app on Koyeb, using the control panel (opens in a new tab), follow the steps:

  1. Click Create Service on the Overview tab of the Koyeb control panel, and select Web Service.
  2. Select GitHub as the deployment option.
  3. Choose the GitHub repository and branch containing your application code. Alternatively, you can enter our public Bun example repository (opens in a new tab) into the Public GitHub repository: https://github.com/koyeb/example-bun.
  4. Select Dockerfile from the Build options.
  5. Choose a CPU for your Service. The default Nano works well for this project.
  6. Click the Deploy button.

This creates a Koyeb App and Service which builds and deploys your application on Koyeb. You can access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy to Koyeb using a pre-built container

As an alternative to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if you need more control over how the build is performed.

To build and push the Docker image to a registry and deploy it on Koyeb, refer to the page on deploying pre-built container images.