Containerization
Learning objectives
- You know the term containerization and know what Docker is.
Containerization refers to packaging an application so that it comes with the necessary operating system libraries and dependencies needed to run the application. An application within a container can be run practically anywhere, given that there exists support for the containerization software. One of the most commonly used software for containerization is Docker.
Here, we visit how a web application can be developed with the help of Docker. In the following subsections, we set up multiple containers: one that will run a Deno web application, one that will run the database that the application uses, and one that will be responsible for database migrations.
The steps outlined in this chapter act as the basis for the subsequent chapters. Follow along the example and mimic the example to create a local version of the project. At the end of this chapter, you are expected to submit a zip file that contains the necessary files to run the project.
Required software
For the examples in this part, and quite a handful of the subsequent parts, you need to have Docker and Docker compose installed.
To start working on the example, create a new folder, and create a folder called api
to the new folder. Create the files app.js
, app-run.js
, and deps.js
and place them to the api
folder. The structure should be as follows.
tree --dirsfirst
.
└── api
├── app.js
├── app-run.js
└── deps.js
Now, copy the following code to the app.js
file.
import { Hono } from "./deps.js";
const app = new Hono();
app.get("/", (c) => c.text("Hello World!"));
export default app;
Copy the following code to the app-run.js
file.
import app from "./app.js";
Deno.serve(app.fetch);
And finally, copy the following code to the deps.js
file.
export { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
The application should look quite familiar at this point. When we run the application on the command line, we notice that everything is in order.
tree --dirsfirst
.
└── api
├── app.js
├── app-run.js
└── deps.js
1 directory, 3 files
deno run -A --unstable --watch api/app-run.js
Watcher Process started.
Listening on http://localhost:8000/
When we make a query to the server, we get the expected response.
curl localhost:8000
Hello World!
As usual, you may shut down the application by pressing Ctrl + C on the command line where you launched the application.