Share feedback
Answers are generated based on the documentation.

Deno language-specific guide

Learn how to containerize JavaScript applications with the Deno runtime using Docker.

The Deno getting started guide teaches you how to create a containerized Deno application using Docker.

Acknowledgment

Docker would like to thank Pradumna Saraf for his contribution to this guide.

What will you learn?

  • Containerize and run a Deno application using Docker
  • Set up a local environment to develop a Deno application using containers
  • Use Docker Compose to run the application.

Prerequisites

  • Basic understanding of JavaScript is assumed.
  • 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 guide.

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.

Start by containerizing an existing Deno application.

Containerize a Deno application

Prerequisites

  • You have a Git client. The examples in this section use a command-line based Git client, but you can use any client.

Overview

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.

Get the sample application

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

Understand the sample application

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/oak@v12.0.0/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 });

Create a Dockerfile

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.

Docker Hardened Images (DHIs) are available for Deno in the Docker Hardened Images catalog. You can pull DHIs directly from the dhi.io registry.

  1. Sign in to the DHI registry:

    $ docker login dhi.io
    
  2. 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"]

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

In addition to specifying the base image, the Dockerfile also:

  • Sets the working directory in the container to /app.
  • Copies server.ts into the container.
  • Sets the user to deno to run the application as a non-root user.
  • Exposes port 8000 to allow traffic to the application.
  • Runs the Deno server using the CMD instruction.
  • 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.

Run the application

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.

Run the application in the background

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

Use containers for Deno development

Prerequisites

Complete Containerize a Deno application.

Overview

In this section, you'll learn how to set up a development environment for your containerized application. This includes:

  • Configuring Compose to automatically update your running Compose services as you edit and save your code

Get the sample application

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

Automatically update services

Use Compose Watch to automatically update your running Compose services as you edit and save your code. For more details about Compose Watch, see Use Compose Watch.

Open your compose.yml file in an IDE or text editor and then add the Compose Watch instructions. The following example shows how to add Compose Watch to your compose.yml file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
services:
  server:
    image: deno-server
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    develop:
      watch:
        - action: rebuild
          path: .

Run the following command to run your application with Compose Watch.

$ docker compose watch

Now, if you modify your server.ts you will see the changes in real time without re-building the image.

To test it out, open the server.ts file in your favorite text editor and change the message from {"Status" : "OK"} to {"Status" : "Updated"}. Save the file and refresh your browser at http://localhost:8000. You should see the updated message.

Press ctrl+c in the terminal to stop your application.

Summary

In this section, you also learned how to use Compose Watch to automatically rebuild and run your container when you update your code.

Related information: