Skip to content

Commit ebb2fe4

Browse files
Merge pull request #21893 from Pradumnasaraf/add-deno
docs: Add language-specific guide for Deno runtime
2 parents 98f7368 + 62f8262 commit ebb2fe4

File tree

5 files changed

+527
-0
lines changed

5 files changed

+527
-0
lines changed

content/guides/deno/_index.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
description: Containerize and develop Deno applications using Docker.
3+
keywords: getting started, deno
4+
title: Deno language-specific guide
5+
summary: |
6+
Learn how to containerize JavaScript applications with the Deno runtime using Docker.
7+
linkTitle: Deno
8+
languages: [js]
9+
params:
10+
time: 10 minutes
11+
---
12+
13+
The Deno getting started guide teaches you how to create a containerized Deno application using Docker. In this guide, you'll learn how to:
14+
15+
> **Acknowledgment**
16+
>
17+
> Docker would like to thank [Pradumna Saraf](https://twitter.com/pradumna_saraf) for his contribution to this guide.
18+
19+
## What will you learn?
20+
21+
* Containerize and run a Deno application using Docker
22+
* Set up a local environment to develop a Deno application using containers
23+
* Use Docker Compose to run the application.
24+
* Configure a CI/CD pipeline for a containerized Deno 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 Deno getting started modules, you should be able to containerize your own Deno application based on the examples and instructions provided in this guide.
33+
34+
Start by containerizing an existing Deno application.
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: Configure CI/CD for your Deno application
3+
linkTitle: Configure CI/CD
4+
weight: 40
5+
keywords: ci/cd, github actions, deno, shiny
6+
description: Learn how to configure CI/CD using GitHub Actions for your Deno application.
7+
aliases:
8+
- /language/deno/configure-ci-cd/
9+
---
10+
11+
## Prerequisites
12+
13+
Complete all the previous sections of this guide, starting with [Containerize a Deno 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 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 Deno 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/deno/containerize.md

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
title: Containerize a Deno application
3+
linkTitle: Containerize your app
4+
weight: 10
5+
keywords: deno, containerize, initialize
6+
description: Learn how to containerize a Deno application.
7+
aliases:
8+
- /language/deno/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 go-to runtime for server-side JavaScript applications. However, recent years have introduced new alternative runtimes, including [Deno](https://deno.land/). Like Node.js, Deno is a JavaScript and TypeScript runtime, but it takes a fresh approach with modern security features, a built-in standard library, and native support for TypeScript.
18+
19+
Why develop Deno applications with Docker? Having a choice of runtimes is exciting, but managing multiple runtimes and their dependencies consistently across environments can be tricky. This is where Docker proves invaluable. Using containers to create and destroy environments on demand simplifies runtime management and ensures consistency. Additionally, as Deno continues to grow and evolve, Docker helps establish a reliable and reproducible development environment, minimizing setup challenges and streamlining the workflow.
20+
21+
## Get the sample application
22+
23+
Clone the sample application to use with this guide. Open a terminal, change
24+
directory to a directory that you want to work in, and run the following
25+
command to clone the repository:
26+
27+
```console
28+
$ git clone https://github.com/dockersamples/docker-deno.git
29+
```
30+
31+
You should now have the following contents in your `deno-docker` directory.
32+
33+
```text
34+
├── deno-docker/
35+
│ ├── compose.yml
36+
│ ├── Dockerfile
37+
│ ├── LICENSE
38+
│ ├── server.ts
39+
│ └── README.md
40+
```
41+
42+
## Understand the sample application
43+
44+
The sample application is a simple Deno application that uses the Oak framework to create a simple API that returns a JSON response. The application listens on port 8000 and returns a message `{"Status" : "OK"}` when you access the application in a browser.
45+
46+
```typescript
47+
// server.ts
48+
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
49+
50+
const app = new Application();
51+
const router = new Router();
52+
53+
// Define a route that returns JSON
54+
router.get("/", (context) => {
55+
context.response.body = { Status: "OK" };
56+
context.response.type = "application/json";
57+
});
58+
59+
app.use(router.routes());
60+
app.use(router.allowedMethods());
61+
62+
console.log("Server running on http://localhost:8000");
63+
await app.listen({ port: 8000 });
64+
```
65+
66+
## Create a Dockerfile
67+
68+
In the Dockerfile, you'll notice that the `FROM` instruction uses `denoland/deno:latest`
69+
as the base image. This is the official image for Deno. This image is [available on the Docker Hub](https://hub.docker.com/r/denoland/deno).
70+
71+
```dockerfile
72+
# Use the official Deno image
73+
FROM denoland/deno:latest
74+
75+
# Set the working directory
76+
WORKDIR /app
77+
78+
# Copy server code into the container
79+
COPY server.ts .
80+
81+
# Set permissions (optional but recommended for security)
82+
USER deno
83+
84+
# Expose port 8000
85+
EXPOSE 8000
86+
87+
# Run the Deno server
88+
CMD ["run", "--allow-net", "server.ts"]
89+
```
90+
91+
Aside from specifying `denoland/deno:latest` as the base image, the Dockerfile:
92+
93+
- Sets the working directory in the container to `/app`.
94+
- Copies `server.ts` into the container.
95+
- Sets the user to `deno` to run the application as a non-root user.
96+
- Exposes port 8000 to allow traffic to the application.
97+
- Runs the Deno server using the `CMD` instruction.
98+
- Uses the `--allow-net` flag to allow network access to the application. The `server.ts` file uses the Oak framework to create a simple API that listens on port 8000.
99+
100+
## Run the application
101+
102+
Make sure you are in the `deno-docker` directory. Run the following command in a terminal to build and run the application.
103+
104+
```console
105+
$ docker compose up --build
106+
```
107+
108+
Open a browser and view the application at [http://localhost:8000](http://localhost:8000). You will see a message `{"Status" : "OK"}` in the browser.
109+
110+
In the terminal, press `ctrl`+`c` to stop the application.
111+
112+
### Run the application in the background
113+
114+
You can run the application detached from the terminal by adding the `-d`
115+
option. Inside the `deno-docker` directory, run the following command
116+
in a terminal.
117+
118+
```console
119+
$ docker compose up --build -d
120+
```
121+
122+
Open a browser and view the application at [http://localhost:8000](http://localhost:8000).
123+
124+
125+
In the terminal, run the following command to stop the application.
126+
127+
```console
128+
$ docker compose down
129+
```
130+
131+
## Summary
132+
133+
In this section, you learned how you can containerize and run your Deno
134+
application using Docker.
135+
136+
Related information:
137+
138+
- [Dockerfile reference](/reference/dockerfile.md)
139+
- [.dockerignore file](/reference/dockerfile.md#dockerignore-file)
140+
- [Docker Compose overview](/manuals/compose/_index.md)
141+
- [Compose file reference](/reference/compose-file/_index.md)
142+
143+
## Next steps
144+
145+
In the next section, you'll learn how you can develop your application using
146+
containers.

0 commit comments

Comments
 (0)