Skip to navigation

Docker Development

This demonstrates usage of the Linux VM with a Docker environment using a sample app.

Pre-requisite

Run script to connect to Linux VM via SSH. Use linuxVMConnectionSetup-SEA or linuxVMConnectionSetup script depending on VM location. Ensure that connection is successful.

Steps

  1. Open Visual Studio Code.
  2. Display the Terminal.
  3. Create a working directory.
mkdir ~/node-docker-app
cd ~/node-docker-app
  1. Pull the Node LTS image from Docker.
docker image pull node:lts
  1. Run the container.
docker run -it -v `pwd`:`pwd` -w `pwd` -p 127.0.0.1:3000:4000 node:lts sh

This command does the following:

  • Starts a Node.js Alpine container.
  • Provides interactive shell access (sh) inside the container.
  • Mounts the current project directory into the container so changes persist.
  • Ensures that files created inside the container appear on the VM.
  • Maps port 4000 in the container to 3000 on the VM, but only accessible locally (127.0.0.1).

[!NOTE] The port mapping is needed to call the API later.

  1. Initialize the node project.
npm init -y
  1. Install Fastify and Typescript dependencies
npm install fastify
npm install --save-dev typescript ts-node @types/node
  1. In VS Code, select Explorer then click Open Folder. When prompted select the directory that was created node-docker-app.
  2. Verify that package.json, package-lock.json, and node_modules folder are present.
  3. Create a tsconfig.json file in the root directory:
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"strict": true,
"outDir": "dist",
"rootDir": "src",
"esModuleInterop": true
}
}
  1. In Explorer or Terminal create a src folder and create file: server.ts and add the following:
import Fastify from "fastify";
const fastify = Fastify({ logger: true });
// Define a route
fastify.get("/", async (request, reply) => {
return { message: "Hello from Fastify + TypeScript inside Docker (Alpine)!" };
});
// Start the server
const start = async () => {
try {
await fastify.listen({ port: 4000, host: "0.0.0.0" });
console.log("Fastify TypeScript server running on port 4000");
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();
  1. Run the server inside the docker container.
npx ts-node src/server.ts
  1. Test the API route:
curl http://127.0.0.1:3000
  1. Verify that the following is displayed:
~/code/node-docker-app$ curl http://127.0.0.1:3000
{"message":"Hello from Fastify + TypeScript inside Docker (Alpine)!"}