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:
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/std@0.50.0/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.0 EXPOSE 8000 WORKDIR /app USER deno COPY deps.ts . RUN deno cache deps.ts COPY . . RUN deno cache main.ts CMD ["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: - deno networks: 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
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
Top comments (10)
Repo not found ....
github.com/FrancescoXX/deno-docker
I still only get a 404 from that address; unsure why this is happening :/
it should be public now. consider that this si something I did 5 years ago, so it will probably not work or will need some fix. let me know how it goes
Ah thank you. I was really wanting to see if I could run it, I am running into seg fault trying to create a dum Deno Docker server atm ;)
[edit] the unmodified code works flawlessly thank you.
it was hard but we made it!
@francescoxx
Thank you for sharing yes that is interesting!
you are welcome !!!
We can use some APIs like Browser APIs via global object ("window-like") without any module. I really love that.
yes that is interesting!