Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 14, 2020 02:59 pm GMT

DockerDeno: Containerize a Deno hello world server

Deno is the buzzword of the moment, and we still don't know how it will end.

Honestly, I am very fond of node, but I was intrigued, so I created a hello world server with deno, and immediately I tried to containerize it with docker.

Warning!
this example is based on this docker image https://hub.docker.com/r/hayd/deno

Which is not an official deno repository

Github Repository: https://github.com/FrancescoXX/deno-docker

Please note the this the first time I use deno, so it is something pretty new also to me

Short version

you can just clone the repository

git clone https://github.com/FrancescoXX/deno-docker

navigate to the folder where the docker-compose.yml is located, and run

docker-compose up --build

Done!

Long version

If you want to follow along, and you use Visual Studio Code, I suggest you install this extension:

Alt Text

First of all, we create a file for the deno dependencies, called deps.ts.

In Deno, we import third part code directly from urls, no node_modules or package.json is needed

export { serve } from "https://deno.land/[email protected]/http/server.ts";

Then, we create the main.ts file, and import the deps.ts file as a regular ts file

We also define a very simple Server, based on the official example

import { serve } from "./deps.ts";const PORT = 8000;const s = serve({ port: 8000 });const body = new TextEncoder().encode("Hello World\n");console.log(`Server started on port ${PORT}`);for await (const req of s) {  req.respond({ body });}

then, we create a very simple Dockerfile, using the hayd/alpine-deno:1.0.0 image

FROM hayd/alpine-deno:1.0.0EXPOSE 8000WORKDIR /appUSER denoCOPY deps.ts .RUN deno cache deps.tsCOPY . .RUN deno cache main.tsCMD ["run", "--allow-net", "main.ts"]

And last, we create a very simple docker-compose.yml file, which is no needed for now, but it could be useful in the future, for example, if we start using a database or some other services

We define port 8000 as both inner and outer one and a default network

version: "3.7"services:  deno:    image: "deno-docker:0.0.1"    build: .    ports:      - "8000:8000"    networks:       - denonetworks:  deno: {}

from the folder where the docker-compose.yml file is located, we build the image:

docker-compose build

And finally, we can launch the service with this command

docker-compose up deno

visit localhost:8000 from a browser

Alt Text

That is not the best and most useful server, but it is intended as a point to start from

Also, using docker, we didn't need to install deno on the machine

Please leave a comment if you want more articles about this topic

Github Repository: Github Repository: https://github.com/FrancescoXX/deno-docker


Original Link: https://dev.to/francescoxx/docker-deno-containerize-a-deno-hello-world-server-1ha1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To