How to use Deno with Docker

May 13, 2020

Deno and Docker are two great technologies. Luckily you can easily use them together.

The best resource for this that I've found is hayd/deno-docker. It currently has only a few watchers or stars, but seems to be the main Docker image for Deno.

Docker files for deno published on Dockerhub:

The amazonlinux1 build is used to run deno on AWS Lambda.


Run locally:

To run main.ts from your working directory:

$ docker run -it --init -p 1993:1993 -v $PWD:/app hayd/alpine-deno:1.0.0 --allow-net /app/main.ts

Here, -p 1993:1993 maps port 1993 on the container to 1993 on the host, -v $PWD:/app mounts the host working directory to /app on the container, and --allow-net /app/main.ts is passed to deno on the container.

As a Dockerfile

FROM hayd/alpine-deno:1.0.0

EXPOSE 1993  # The port that your application listens to.

WORKDIR /app

# Prefer not to run as root.
USER deno

# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified).
# Ideally cache deps.ts will download and compile _all_ external files used in main.ts.
COPY deps.ts .
RUN deno cache deps.ts

# These steps will be re-run upon each file change in your working directory:
ADD . .
# Compile the main app so that it doesn't need to be compiled each startup/entry.
RUN deno cache main.ts

CMD ["--allow-net", "main.ts"]

and build and run this locally:

$ docker build -t app . && docker run -it --init -p 1993:1993 app

To see more details check out the examples directory at hayd/deno-docker.