In this step, you will be creating a simple Deno application to display hello world. Unlike NodeJS, you do not need to run npm init
to create a new application. You can simply create a TypeScript file and start coding away.
To begin, create a new directory named deno-deploy
on your local machine, by running:
mkdir deno-deploy
Change directory to deno-deploy
by running:
cd deno-deploy
Create a new file named server.ts
within the deno-deploy
directory by running:
touch server.ts
Note: alternatively, you could use your favourite editor to create this file.
Open server.ts
with your preferred editor and paste and save the following code snippet:
import { serve } from "https://deno.land/[email protected]/http/server.ts"; const server = serve({ port: 8000 }); console.log("Now serving on port 8000 🔥"); for await (const req of server) { req.respond({ body: "Hello World"}); }
The snippet above will create a Deno server and serves the content Hello World
on port 8000
.
In this step, you will set up the Docker configuration for your Deno application. First, you will create a new file named Dockerfile
via the terminal by running:
touch Dockerfile
Open Dockerfile
with your preferred text editor then paste and save the following snippet:
FROM hayd/deno:latest EXPOSE 8000 WORKDIR /app ADD . /app RUN deno cache server.ts CMD ["run", "--allow-net", "server.ts"]
Let’s break down what these lines in our Dockerfile
will do when executed:
Pulls the latest version of hayd/deno
image from Docker hub.
Exposes port 8000
on our container when built.
Makes the working directory /app
in our container.
Copies the content of the root directory into /app
directory in your Docker container.
Compiles server.ts
so that it doesn’t need to be compiled for each startup.
This will run the server.ts
file and enables networking.
In this step, you will be creating a docker-compose.yml
file that will be used to piece everything together and serve our application. To create a docker-compose.yml
file, run:
touch docker-compose.yml
Open docker-compose.yml
with your preferred text editor then paste and save the following snippet:
version: '3' services: web: build: . container_name: deno-deploy ports: - "8000:8000"
Let us break down what these lines in our docker-compose.yml
will do when executed. version: '3'
specifies the version of YAML
contained in the file:
web: build: . container_name: deno-deploy ports: - "8000:8000"
This section contains the web
service.
build: .
This indicates that the Dockerfile
we intend to build is in the current directory.
container_name: deno-deploy
This will ensure that the container name on the build will be deno-deploy
.
ports: - "8000:8000"
Will map the container port 8000
to the host server port 8000
.
To build your Docker container locally, run:
docker-compose up
You can visit your application on http://localhost:8000
via your preferred web browser.
Docker makes it easy to quickly deploy applications anywhere. First, you will need to make your code available on git version control, a good provider is GitHub. Create a new repository named deno-deploy
.
Open the terminal, while still in the deno-deploy
directory. Run:
git init
This will initiate a new git repository. Next, stage all files by running:
git add .
Commit the staged files with the commit message "deno deploy"
:
git commit -m "deno deploy"
Push to the master branch by running:
git push -u origin master
This will push the codebase along with the Docker configuration to the master branch of your Github repository.
In this step, you will be making your source code available on the server and making it accessible via the internet.
SSH into your server:
ssh {SERVER_USER}@{SERVER_IP}
SERVER_USER
is the user of the serverSERVER_IP
is the IP address of the serverClone the repository:
git clone https://github.com/{GITHUB_USERNAME}/deno-deploy.git
Note:
GITHUB_USERNAME
is your actual GitHub username
Change directory into the cloned repository:
cd deno-deploy
Execute the docker-compose
command:
docker-compose up -d
Unlike how you executed docker-compose
on your local machine, there is a -d
flag which enables your docker container to run in detached mode. In simpler terms, it allows your docker container to run in the background.
You will be able to visit your application on http://{SERVER_IP}:8000
. In my case, you can visit http://104.248.172.220:8000
.
In this article, we learned how to create a simple Deno web server, how to create a Docker configuration for a Deno web server, how to push your code to GitHub, and how to make your Deno application available on the internet. Happy coding!
Install LogRocket via npm or script tag. LogRocket.init()
must be called client-side, not
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Angular’s new `defer` feature, introduced in Angular 17, can help us optimize the delivery of our apps to end users.
ElectricSQL is a cool piece of software with immense potential. It gives developers the ability to build a true local-first application.
Leptos is an amazing Rust web frontend framework that makes it easier to build scalable, performant apps with beautiful, declarative UIs.
Learn more about the 5 best JavaScript libraries for dealing with multidimensional arrays, such as ndarray, math.js, and NumJs.
5 Replies to "How to deploy Deno applications to production"
You missed the point of deno if you use docker
I very much doubt that. Deno certainly does not replace containerization, that’s a different concern.
Thank you for the article – you have a typo in the port line in yml. 3000:3000 should be 8000:8000
Thanks for the catch, we’ve updated the post
Ehhh no @Babab … time to take a nap.
Thanks Michael Okoh for the article. Very helpful!