Get Started with Deno: A Step-by-Step Guide to Deploying Your First Application
Prerequisites
Before we dive in, make sure you have:
- Docker installed on your server and local machine
- An Ubuntu server (you can get one from DigitalOcean)
- Basic understanding of git
- Basic understanding of the command line interface
Creating a Simple Deno Application
Let’s create a simple Deno application to display “Hello World”. Unlike NodeJS, you don’t need to run npm init to create a new application. Simply create a TypeScript file and start coding away!
Create a new directory named deno-deploy on your local machine by running:
mkdir deno-deploy
Change into the deno-deploy directory:
cd deno-deploy
Create a new file named server.ts within the deno-deploy directory:
touch server.ts
Open server.ts with your preferred editor and paste the following code snippet:
“`
import { serve } from “https://deno.land/[email protected]/http/server.ts”;
serve(() => {
return new Response(“Hello World”, { status: 200 });
});
“`
This code snippet creates a Deno server that serves the content “Hello World” on port 8000.
Setting Up Docker Configuration
Next, let’s set up the Docker configuration for our Deno application. Create a new file named Dockerfile via the terminal:
touch Dockerfile
Open Dockerfile with your preferred text editor and paste 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 do:
FROM hayd/deno:latestpulls the latest version of thehayd/denoimage from Docker Hub.EXPOSE 8000exposes port 8000 on our container.WORKDIR /appmakes the working directory/appin our container.ADD. /appcopies the content of the root directory into/appdirectory in our Docker container.RUN deno cache server.tscompilesserver.tsso that it doesn’t need to be compiled for each startup.CMD ["run", "--allow-net", "server.ts"]runs theserver.tsfile and enables networking.
Creating docker-compose.yml
Create a docker-compose.yml file that will piece everything together and serve our application:
touch docker-compose.yml
Open docker-compose.yml with your preferred text editor and paste the following snippet:
version: '3'
services:
web:
build:.
container_name: deno-deploy
ports:
- "8000:8000"
Let’s break down what these lines do:
version: '3'specifies the version of YAML contained in the file.build:.indicates that the Dockerfile we intend to build is in the current directory.container_name: deno-deployensures that the container name on the build will bedeno-deploy.ports: - "8000:8000"maps the container port 8000 to the host server port 8000.
Building and Running the Container
To build your Docker container locally, run:
docker-compose up
You can visit your application on http://localhost:8000 via your preferred web browser.
Deploying to Production
First, make your code available on git version control using GitHub. Create a new repository named deno-deploy. Open the terminal, while still in the deno-deploy directory, and run:
git init
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 origin master
This will push the codebase along with the Docker configuration to the master branch of your GitHub repository.
Deploying on the Server
SSH into your server:
ssh SERVER_USER@SERVER_IP
Clone the repository:
git clone https://github.com/GITHUB_USERNAME/deno-deploy.git
Change 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, the -d flag enables your Docker container to run in detached mode.
You will be able to visit your application on http://{SERVER_IP}:8000.