content/guides/deno/containerize.md
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. 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.
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.
Clone the sample application to use with this guide. Open a terminal, change directory to a directory that you want to work in, and run the following command to clone the repository:
$ git clone https://github.com/dockersamples/docker-deno.git && cd docker-deno
You should now have the following contents in your deno-docker directory.
├── deno-docker/
│ ├── compose.yml
│ ├── Dockerfile
│ ├── LICENSE
│ ├── server.ts
│ └── README.md
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.
// server.ts
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
const app = new Application();
const router = new Router();
// Define a route that returns JSON
router.get("/", (context) => {
context.response.body = { Status: "OK" };
context.response.type = "application/json";
});
app.use(router.routes());
app.use(router.allowedMethods());
console.log("Server running on http://localhost:8000");
await app.listen({ port: 8000 });
Before creating a Dockerfile, you need to choose a base image. You can either use the Deno Docker Official Image or a Docker Hardened Image (DHI) from the Hardened Image catalog.
Choosing DHI offers the advantage of a production-ready image that is lightweight and secure. For more information, see Docker Hardened Images.
{{< tabs >}} {{< tab name="Using Docker Hardened Images" >}}
Docker Hardened Images (DHIs) are available for Deno in the Docker Hardened Images catalog. You can pull DHIs directly from the dhi.io registry.
Sign in to the DHI registry:
$ docker login dhi.io
Pull the Deno DHI as dhi.io/deno:2. The tag (2) in this example refers to the version to the latest 2.x version of Deno.
$ docker pull dhi.io/deno:2
For other available versions, refer to the catalog.
# Use the DHI Deno image as the base image
FROM dhi.io/deno:2
# Set the working directory
WORKDIR /app
# Copy server code into the container
COPY server.ts .
# Set permissions (optional but recommended for security)
USER deno
# Expose port 8000
EXPOSE 8000
# Run the Deno server
CMD ["run", "--allow-net", "server.ts"]
{{< /tab >}} {{< tab name="Using the official image" >}}
Using the Docker Official Image is straightforward. In the following Dockerfile, you'll notice that the FROM instruction uses denoland/deno:latest as the base image.
This is the official image for Deno. This image is available on the Docker Hub.
# Use the official Deno image
FROM denoland/deno:latest
# Set the working directory
WORKDIR /app
# Copy server code into the container
COPY server.ts .
# Set permissions (optional but recommended for security)
USER deno
# Expose port 8000
EXPOSE 8000
# Run the Deno server
CMD ["run", "--allow-net", "server.ts"]
{{< /tab >}} {{< /tabs >}}
In addition to specifying the base image, the Dockerfile also:
/app.server.ts into the container.deno to run the application as a non-root user.CMD instruction.--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.Make sure you are in the deno-docker directory. Run the following command in a terminal to build and run the application.
$ docker compose up --build
Open a browser and view the application at http://localhost:8000. You will see a message {"Status" : "OK"} in the browser.
In the terminal, press ctrl+c to stop the application.
You can run the application detached from the terminal by adding the -d
option. Inside the deno-docker directory, run the following command
in a terminal.
$ docker compose up --build -d
Open a browser and view the application at http://localhost:8000.
In the terminal, run the following command to stop the application.
$ docker compose down
In this section, you learned how you can containerize and run your Deno application using Docker.
Related information:
In the next section, you'll learn how you can develop your application using containers.