Skip to content

Commit 29fc291

Browse files
Pradumnasarafdvdksn
authored andcommitted
docs: add Bun language-specific guide
Signed-off-by: David Karlsson <[email protected]>
1 parent f31f2e7 commit 29fc291

File tree

5 files changed

+508
-0
lines changed

5 files changed

+508
-0
lines changed

content/guides/bun/_index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
description: Containerize and develop Bun applications using Docker.
3+
keywords: getting started, bun
4+
title: Bun language-specific guide
5+
summary: |
6+
Learn how to containerize JavaScript applications with the Bun runtime.
7+
linkTitle: Bun
8+
levels: [beginner]
9+
languages: [js]
10+
params:
11+
time: 10 minutes
12+
---
13+
14+
The Bun getting started guide teaches you how to create a containerized Bun application using Docker. In this guide, you'll learn how to:
15+
16+
> **Acknowledgment**
17+
>
18+
> Docker would like to thank [Pradumna Saraf](https://twitter.com/pradumna_saraf) for his contribution to this guide.
19+
20+
## What will you learn?
21+
22+
* Containerize and run a Bun application using Docker
23+
* Set up a local environment to develop a Bun application using containers
24+
* Configure a CI/CD pipeline for a containerized Bun application using GitHub Actions
25+
* Deploy your containerized application locally to Kubernetes to test and debug your deployment
26+
27+
## Prerequisites
28+
29+
- Basic understanding of JavaScript is assumed.
30+
- You must have familiarity with Docker concepts like containers, images, and Dockerfiles. If you are new to Docker, you can start with the [Docker basics](/get-started/docker-concepts/the-basics/what-is-a-container.md) guide.
31+
32+
After completing the Bun getting started modules, you should be able to containerize your own Bun application based on the examples and instructions provided in this guide.
33+
34+
Start by containerizing an existing Bun application.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: Configure CI/CD for your Bun application
3+
linkTitle: Configure CI/CD
4+
weight: 40
5+
keywords: ci/cd, github actions, bun, shiny
6+
description: Learn how to configure CI/CD using GitHub Actions for your Bun application.
7+
aliases:
8+
- /language/bun/configure-ci-cd/
9+
---
10+
11+
## Prerequisites
12+
13+
Complete all the previous sections of this guide, starting with [Containerize a Bun application](containerize.md). You must have a [GitHub](https://github.com/signup) account and a [Docker](https://hub.docker.com/signup) account to complete this section.
14+
15+
## Overview
16+
17+
In this section, you'll learn how to set up and use GitHub Actions to build and test your Docker image as well as push it to Docker Hub. You will complete the following steps:
18+
19+
1. Create a new repository on GitHub.
20+
2. Define the GitHub Actions workflow.
21+
3. Run the workflow.
22+
23+
## Step one: Create the repository
24+
25+
Create a GitHub repository, configure the Docker Hub credentials, and push your source code.
26+
27+
1. [Create a new repository](https://github.com/new) on GitHub.
28+
29+
2. Open the repository **Settings**, and go to **Secrets and variables** >
30+
**Actions**.
31+
32+
3. Create a new **Repository variable** named `DOCKER_USERNAME` and your Docker ID as value.
33+
34+
4. Create a new [Personal Access Token (PAT)](/manuals/security/for-developers/access-tokens.md#create-an-access-token)for Docker Hub. You can name this token `docker-tutorial`. Make sure access permissions include Read and Write.
35+
36+
5. Add the PAT as a **Repository secret** in your GitHub repository, with the name
37+
`DOCKERHUB_TOKEN`.
38+
39+
6. In your local repository on your machine, run the following command to change
40+
the origin to the repository you just created. Make sure you change
41+
`your-username` to your GitHub username and `your-repository` to the name of
42+
the repository you created.
43+
44+
```console
45+
$ git remote set-url origin https://github.com/your-username/your-repository.git
46+
```
47+
48+
7. Run the following commands to stage, commit, and push your local repository to GitHub.
49+
50+
```console
51+
$ git add -A
52+
$ git commit -m "my commit"
53+
$ git push -u origin main
54+
```
55+
56+
## Step two: Set up the workflow
57+
58+
Set up your GitHub Actions workflow for building, testing, and pushing the image
59+
to Docker Hub.
60+
61+
1. Go to your repository on GitHub and then select the **Actions** tab.
62+
63+
2. Select **set up a workflow yourself**.
64+
65+
This takes you to a page for creating a new GitHub actions workflow file in
66+
your repository, under `.github/workflows/main.yml` by default.
67+
68+
3. In the editor window, copy and paste the following YAML configuration and commit the changes.
69+
70+
```yaml
71+
name: ci
72+
73+
on:
74+
push:
75+
branches:
76+
- main
77+
78+
jobs:
79+
build:
80+
runs-on: ubuntu-latest
81+
steps:
82+
-
83+
name: Login to Docker Hub
84+
uses: docker/login-action@v3
85+
with:
86+
username: ${{ vars.DOCKER_USERNAME }}
87+
password: ${{ secrets.DOCKERHUB_TOKEN }}
88+
-
89+
name: Set up Docker Buildx
90+
uses: docker/setup-buildx-action@v3
91+
-
92+
name: Build and push
93+
uses: docker/build-push-action@v6
94+
with:
95+
platforms: linux/amd64,linux/arm64
96+
push: true
97+
tags: ${{ vars.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest
98+
```
99+
100+
For more information about the YAML syntax for `docker/build-push-action`,
101+
refer to the [GitHub Action README](https://github.com/docker/build-push-action/blob/master/README.md).
102+
103+
## Step three: Run the workflow
104+
105+
Save the workflow file and run the job.
106+
107+
1. Select **Commit changes...** and push the changes to the `main` branch.
108+
109+
After pushing the commit, the workflow starts automatically.
110+
111+
2. Go to the **Actions** tab. It displays the workflow.
112+
113+
Selecting the workflow shows you the breakdown of all the steps.
114+
115+
3. When the workflow is complete, go to your
116+
[repositories on Docker Hub](https://hub.docker.com/repositories).
117+
118+
If you see the new repository in that list, it means the GitHub Actions
119+
successfully pushed the image to Docker Hub.
120+
121+
## Summary
122+
123+
In this section, you learned how to set up a GitHub Actions workflow for your Bun application.
124+
125+
Related information:
126+
- [Introduction to GitHub Actions](/manuals/build/ci/github-actions/_index.md)
127+
- [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)
128+
129+
## Next steps
130+
131+
Next, learn how you can locally test and debug your workloads on Kubernetes before deploying.

content/guides/bun/containerize.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: Containerize a Bun application
3+
linkTitle: Containerize your app
4+
weight: 10
5+
keywords: bun, containerize, initialize
6+
description: Learn how to containerize a Bun application.
7+
aliases:
8+
- /language/bun/containerize/
9+
---
10+
11+
## Prerequisites
12+
13+
* You have a [Git client](https://git-scm.com/downloads). The examples in this section use a command-line based Git client, but you can use any client.
14+
15+
## Overview
16+
17+
For a long time, Node.js has been the de-facto runtime for server-side
18+
JavaScript applications. Recent years have seen a rise in new alternative
19+
runtimes in the ecosystem, including [Bun website](https://bun.sh/). Like
20+
Node.js, Bun is a JavaScript runtime. Bun is a comparatively lightweight
21+
runtime that is designed to be fast and efficient.
22+
23+
Why develop Bun applications with Docker? Having multiple runtimes to choose
24+
from is great. But as the number of runtimes increases, it becomes challenging
25+
to manage the different runtimes and their dependencies consistently across
26+
environments. This is where Docker comes in. Creating and destroying containers
27+
on demand is a great way to manage the different runtimes and their
28+
dependencies. Also, as it's fairly a new runtime, getting a consistent
29+
development environment for Bun can be challenging. Docker can help you set up
30+
a consistent development environment for Bun.
31+
32+
## Get the sample application
33+
34+
Clone the sample application to use with this guide. Open a terminal, change
35+
directory to a directory that you want to work in, and run the following
36+
command to clone the repository:
37+
38+
```console
39+
$ git clone https://github.com/Pradumnasaraf/bun-docker.git
40+
```
41+
42+
You should now have the following contents in your `bun-docker` directory.
43+
44+
```text
45+
├── bun-docker/
46+
│ ├── compose.yml
47+
│ ├── Dockerfile
48+
│ ├── LICENSE
49+
│ ├── server.js
50+
│ └── README.md
51+
```
52+
53+
In the Dockerfile, you'll notice that the `FROM` instruction uses `oven/bun`
54+
as the base image. This is the official image for Bun created by Oven, the
55+
company behind Bun. This image is [available on the Docker Hub](https://hub.docker.com/r/oven/bun).
56+
57+
```dockerfile
58+
# Use the Bun image as the base image
59+
FROM oven/bun:latest
60+
61+
# Set the working directory in the container
62+
WORKDIR /app
63+
64+
# Copy the current directory contents into the container at /app
65+
COPY . .
66+
67+
# Expose the port on which the API will listen
68+
EXPOSE 3000
69+
70+
# Run the server when the container launches
71+
CMD ["bun", "server.js"]
72+
```
73+
74+
Aside from specifying `oven/bun` as the base image, this Dockerfile also:
75+
76+
- Sets the working directory in the container to `/app`
77+
- Copies the contents of the current directory to the `/app` directory in the container
78+
- Exposes port 3000, where the API is listening for requests
79+
- And finally, starts the server when the container launches with the command `bun server.js`.
80+
81+
## Run the application
82+
83+
Inside the `bun-docker` directory, run the following command in a terminal.
84+
85+
```console
86+
$ docker compose up --build
87+
```
88+
89+
Open a browser and view the application at [http://localhost:3000](http://localhost:3000). You will see a message `{"Status" : "OK"}` in the browser.
90+
91+
In the terminal, press `ctrl`+`c` to stop the application.
92+
93+
### Run the application in the background
94+
95+
You can run the application detached from the terminal by adding the `-d`
96+
option. Inside the `bun-docker` directory, run the following command
97+
in a terminal.
98+
99+
```console
100+
$ docker compose up --build -d
101+
```
102+
103+
Open a browser and view the application at [http://localhost:3000](http://localhost:3000).
104+
105+
106+
In the terminal, run the following command to stop the application.
107+
108+
```console
109+
$ docker compose down
110+
```
111+
112+
## Summary
113+
114+
In this section, you learned how you can containerize and run your Bun
115+
application using Docker.
116+
117+
Related information:
118+
119+
- [Dockerfile reference](/reference/dockerfile.md)
120+
- [.dockerignore file](/reference/dockerfile.md#dockerignore-file)
121+
- [Docker Compose overview](/manuals/compose/_index.md)
122+
- [Compose file reference](/reference/compose-file/_index.md)
123+
124+
## Next steps
125+
126+
In the next section, you'll learn how you can develop your application using
127+
containers.

0 commit comments

Comments
 (0)