bun not found in Docker, trying to do split base/build image – Docker

by
Ali Hasan
android-app-bundle boot2docker nuxtjs3

Quick Fix: Modify Dockerfile to use ‘bun’ as the base image and install dependencies without the ‘–production’ flag for a clean build.

The Problem:

An individual is experiencing difficulties implementing a Nuxt3 project using Bun in a Docker environment. Upon executing ‘docker compose up,’ they encounter an error specifying ‘bun not found.’ The Dockerfile employed utilizes a multi-stage build with the ‘bun:1.0.0’ image. Despite prior success with NPM, the issue persists. Determine the cause and offer solutions to resolve the ‘bun not found’ error, enabling a successful build process.

The Solutions:

Solution 1: Make changes in Dockerfile and address package.json

To fix the issue, you need to make changes to your Dockerfile and address the package.json file.

1. Changes in Dockerfile:

Replace these lines:

ARG NODE_VERSION=18.14.2

FROM node:${NODE_VERSION}-slim as base

FROM oven/bun:1.0.0

with this:

FROM oven/bun:latest AS base

2. Addressing the package.json file:

In your package.json file, you have nuxt set as devDependencies. When using the --production flag, these dependencies are not installed.

To fix this, replace:

RUN bun install --production

with:

RUN bun install

Conclusion:

After making these changes, you should be able to build and run your Docker image successfully without encountering the bun not found error.

Solution 2: Manually Installed Bun

If you installed Bun manually and encountered the error "bun: not found" when running bun install in your Dockerfile, you can resolve it by adding the following line to your Dockerfile instead of using just bun install:

RUN ~/.bun/bin/bun install

This ensures that the Bun binary is available and accessible within the container.

Here’s a breakdown of what the command does:

  • ~/.bun/bin/bun: This is the path to the Bun binary within the container. It assumes that you have installed Bun manually and it’s located in the ~/.bun/bin directory. If you installed Bun in a different location, adjust the path accordingly.
  • install: This is the command to install Bun packages.

With this modification, your Dockerfile should be able to successfully install Bun packages without encountering the "bun: not found" error.

Q&A

What is the issue with this Dockerfile?

The issue is attempting to install bun using the base image, which doesn’t have bun, and also using --production flag which excludes nuxt (devDependencies).

Video Explanation:

The following video, titled "Next.js 14 Full Course 2024 | Build and Deploy a Full Stack App ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Ultimate Next 14 Course: https://www.jsmastery.pro/next14 Next.js recently became the official React framework as outlined in React docs.