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.
Prerequisites: Installing Bun
Before we start with Docker, you'll need to have Bun installed on your system. Here's how to do it:
- First, update your package manager and install unzip:
sudo apt update
sudo apt install unzip
- Install Bun using the official installer:
curl -fsSL https://bun.sh/install | bash
- Verify the installation:
bun --version
Setting Up Docker Configuration
Dockerfile Setup
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"]
Configure Your Application Scripts
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"
}
}
Building and Running the Docker Container
Build Your Docker Image
Create the Docker image for your application:
docker build -t my-bun-app .
Run Your Container
Start a new container from your image:
docker run -p 3000:3000 my-bun-app
Managing Docker Containers
View Running Containers
To see all currently running containers:
docker ps
View All Containers
To list all containers, including stopped ones:
docker ps -a
Starting an Existing Container
To start a stopped container:
docker start <container_id>
Troubleshooting Tips
If you encounter a "Script not found" error:
- Check that your
package.json
scripts are correctly defined - Ensure the
dev
script matches your application's startup requirements - Rebuild your Docker image after any changes to
package.json
- Verify that all files are being correctly copied into the container
Remember 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.