As I recently developed a small tool using Bun, I felt that installing Bun on my local machine could pollute my environment, especially with the fast-paced updates and iterations. To overcome this, I resorted to using Docker to set up a Bun.js environment on my machine. In this article, I will guide you through the steps to install Docker, pull the Bun image, and create a function in your shell configuration file to run Bun projects with Docker. We will also explore how to map the output port and delete the container after running or stopping it.

### Installing Docker

I use a Mac device, so I installed Docker Desktop for convenient management. You can download it from the official website: [Docker website](https://www.docker.com/products/docker-desktop).

### Pulling the Bun Image

Using the command line:

```bash
docker pull oven/bun
```

Or using the graphical interface:

* Pull the `oven/bun` image.

### Requirements

* Bun project files are stored on the local machine, not in Docker.
* The output port needs to be mapped so that it can be accessed.
* The container should stop and be deleted after the project is completed or manually stopped (Ctrl+C).

### Editing Directive Settings and Environments

In the `.zshrc` or `.bashrc` file on the Mac device, add the following function to avoid typing a long command every time you run it. Note that after adding, you need to save the file and source it to make it take effect.

```zsh
bun() {
local port=${1:-8080}
# Add the --init parameter to make the Docker container correctly process signals (such as Ctrl+C),
# which will run an init process as PID 1 inside the container,
# allowing you to correctly forward signals to the application processes
if [[ $1 =~ ^[0-9]+$ ]]; then
docker run -it --rm -w /app -v