Running a Bun Application with Docker: Part 2
February 24, 2025 Avishka Devinda
February 24, 2025 Avishka Devinda
In this guide, we'll walk through how to run a Bun application using Docker. We'll cover everything from installing Bun to creating and managing Docker containers.
Before we start with Docker, you'll need to have Bun installed on your system. Here's how to do it:
sudo apt update
sudo apt install unzip
curl -fsSL https://bun.sh/install | bash
bun --version
Create a Dockerfile in your project root with the following configuration:
# Use an official Node.js image as the base image
FROM node:18
# Set the working directory inside the container
WORKDIR /app
# Install Bun
RUN curl -fsSL https://bun.sh/install | bash
# Add Bun to PATH
ENV PATH="/root/.bun/bin:${PATH}"
# Copy package.json
COPY package.json .
# Install dependencies using Bun
RUN bun install
# Copy the rest of your application files
COPY . .
# Expose the port the app will run on
EXPOSE 3000
# Run the app with Bun
CMD ["bun", "dev"]
Ensure your package.json includes the necessary scripts. For example, if you're using a TypeScript setup:
{
"scripts": {
"dev": "bun --hot src/index.tsx",
"start": "NODE_ENV=production bun src/index.tsx",
"build": "bun run build.ts"
}
}
Create the Docker image for your application:
docker build -t my-bun-app .
Start a new container from your image:
docker run -p 3000:3000 my-bun-app
To see all currently running containers:
docker ps
To list all containers, including stopped ones:
docker ps -a
To start a stopped container:
docker start <container_id>
If you encounter a "Script not found" error:
package.json scripts are correctly defineddev script matches your application's startup requirementspackage.jsonRemember to rebuild your Docker image whenever you make changes to your Dockerfile or application code:
docker build -t my-bun-app .
This concludes our two-part guide on Docker installation and running a Bun application in Docker. You should now have a fully functional Docker environment running your Bun application.