|
| 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