Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry

This guide walks you through the steps to create a TypeScript Express app, containerize it with Docker, and deploy it to Google Kubernetes Engine (GKE) using Google Artifact Registry.

1. Create a TypeScript Express App

Initialize the P…


This content originally appeared on DEV Community and was authored by Emi Roberti

This guide walks you through the steps to create a TypeScript Express app, containerize it with Docker, and deploy it to Google Kubernetes Engine (GKE) using Google Artifact Registry.

1. Create a TypeScript Express App

  1. Initialize the Project:
   mkdir ts_express_server
   cd ts_express_server
   npm init -y
  1. Install Required Packages:
   npm install express cors
   npm install --save-dev typescript @types/node @types/express @types/cors
  1. Set Up TypeScript: Create a tsconfig.json file:
   {
     "compilerOptions": {
       "outDir": "./dist",
       "module": "commonjs",
       "target": "es6",
       "strict": true
     },
     "include": ["src/**/*"]
   }
  1. Write the Server Code: Create src/server.ts:
   import express, { Request, Response } from "express";
   import Cors from "cors";

   const app = express();
   app.use(express.json());
   app.use(Cors());

   app.get("/api", (req: Request, res: Response) => {
     res.status(200).json({ message: "Server up " + new Date().toISOString() });
   });

   app.listen(8090, () => {
     console.log("Server listening on port 8090");
   });
  1. Build the App: Add build scripts to package.json:
   "scripts": {
     "build": "tsc",
     "start": "node dist/server.js"
   }

Build the app:

   npm run build

2. Create a Docker Image

  1. Write a Dockerfile:
   FROM node:18-alpine
   WORKDIR /app
   COPY package*.json ./
   RUN npm install
   COPY . .
   RUN npm run build
   EXPOSE 8090
   CMD ["npm", "run", "start"]
  1. Build the Docker Image:
   docker build -t emirob/emi-repo:my-express-app .
  1. Push the Image to Docker Hub:
   docker tag emirob/emi-repo:my-express-app emirob/emi-repo:my-express-app
   docker push emirob/emi-repo:my-express-app

3. Set Up GCP for Artifact Registry and GKE

  1. Enable Required APIs:

    • Go to the Google Cloud Console.
    • Enable the Artifact Registry API.
    • Enable the Kubernetes Engine API.
  2. Set Your Project in Cloud Shell:

   export PROJECT_ID=gketest-448214
   gcloud config set project $PROJECT_ID
  1. Create an Artifact Registry Repository:
   gcloud artifacts repositories create emi-repo --repository-format=docker --location=us-west1 --description="Docker repo"
  1. Authenticate Docker with GCP:
   gcloud auth configure-docker us-west1-docker.pkg.dev

4. Push the Docker Image to Artifact Registry

  1. Pull the Image from Docker Hub:
   docker pull emirob/emi-repo:my-express-app
  1. Tag the Image for Artifact Registry:
   docker tag emirob/emi-repo:my-express-app        us-west1-docker.pkg.dev/gketest-448214/emi-repo/my-express-app:latest
  1. Push the Image to Artifact Registry:
   docker push us-west1-docker.pkg.dev/gketest-448214/emi-repo/my-express-app:latest
  1. Verify the Image:
   gcloud artifacts docker images list us-west1 docker.pkg.dev/gketest-448214/emi-repo

5. Deploy the App on GKE

  1. Create a GKE Cluster:
   gcloud container clusters create my-cluster --num-nodes=3 --zone=us-west1-a
  1. Connect to the Cluster:
   gcloud container clusters get-credentials my-cluster --zone us-west1-a
  1. Create a Kubernetes Deployment: Save the following as deployment.yaml:
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: my-express-app
   spec:
     replicas: 2
     selector:
       matchLabels:
         app: my-express-app
     template:
       metadata:
         labels:
           app: my-express-app
       spec:
         containers:
         - name: my-express-app
           image: us-west1-docker.pkg.dev/gketest-448214/emi-repo/my-express-app:latest
           ports:
           - containerPort: 8090
  1. Apply the Deployment:
   kubectl apply -f deployment.yaml
  1. Expose the Service:
   kubectl expose deployment my-express-app --type=LoadBalancer --port 80 --target-port 8090
  1. Access the Application: Get the external IP of the LoadBalancer:
   kubectl get service my-express-app

Visit http://<EXTERNAL-IP>/api.

6. GitHub Repository

For the full project setup and Docker image creation, check the GitHub repository:

build_node_server_docker_images

Emi Roberti - Happy coding


This content originally appeared on DEV Community and was authored by Emi Roberti


Print Share Comment Cite Upload Translate Updates
APA

Emi Roberti | Sciencx (2025-01-18T21:04:10+00:00) Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry. Retrieved from https://www.scien.cx/2025/01/18/deploying-a-typescript-express-app-to-gke-using-docker-and-artifact-registry/

MLA
" » Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry." Emi Roberti | Sciencx - Saturday January 18, 2025, https://www.scien.cx/2025/01/18/deploying-a-typescript-express-app-to-gke-using-docker-and-artifact-registry/
HARVARD
Emi Roberti | Sciencx Saturday January 18, 2025 » Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry., viewed ,<https://www.scien.cx/2025/01/18/deploying-a-typescript-express-app-to-gke-using-docker-and-artifact-registry/>
VANCOUVER
Emi Roberti | Sciencx - » Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/18/deploying-a-typescript-express-app-to-gke-using-docker-and-artifact-registry/
CHICAGO
" » Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry." Emi Roberti | Sciencx - Accessed . https://www.scien.cx/2025/01/18/deploying-a-typescript-express-app-to-gke-using-docker-and-artifact-registry/
IEEE
" » Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry." Emi Roberti | Sciencx [Online]. Available: https://www.scien.cx/2025/01/18/deploying-a-typescript-express-app-to-gke-using-docker-and-artifact-registry/. [Accessed: ]
rf:citation
» Deploying a TypeScript Express App to GKE Using Docker and Artifact Registry | Emi Roberti | Sciencx | https://www.scien.cx/2025/01/18/deploying-a-typescript-express-app-to-gke-using-docker-and-artifact-registry/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.