Dockerizing a Node + Typescript +Postgres App

Dockerizing a Node + Typescript +Postgres App

Quick Summary: Step 1: Creating a Typescript node postgres app.

Create a folder and initialize the node js project.

Next, install TypeScript as a dev dependency:

Once that’s downloaded, create a tsconfig.json file:

Change some configuration for tsconfig.json:

Install Express and its typing as a dev dependency and :

Create an express server :

Install and configure swc (superfast web compiler as an alternative for the swc )

npm install –save-dev @swc/cli @swc/core
Create a config file for swc as .swcrcod

Running and watching the changes install nodemon and ts-node, and create nodemon.json, you can set other files too to watch in the array.

Scripts to run :

Run the app in development:

Step 2: Dockerizing a node + typescript + app(Single Container)

Prerequisites: 

Reference links :

Docker  https://www.docker.com/101-tutorial/

Setting Up Docker File To Build The Image :

  • Firstly need to set the docker file for our application with the latest node image as 

Specify the above in the file.

  • Then set up the working directory and the code we want to copy to the container while building an image and the other necessary files and finally command to run the application inside the container at last and the port at the app will be exposed.
  • To build an image for a specific container :

     Cmd: docker build -t image-name. //build image by using the current directory 

  • To run a container :

 Cmd: docker run –name container-name -p 8001:8000 image-name //running a container exposing it to a desired port outside the container.

(Here we are going to use compose so we don’t need to build an image and run the containers with the above command we can specify it in the compose file if there is only one service the image can be directly built and run container by the above commands)

Using Docker Compose 

Running multiple containers(node app+postgres)

Docker Compose: https://docs.docker.com/compose/gettingstarted/

  • Docker compose is used to run multiple services each with separate containers.

 It automatically build the images from the docker file when specific if the image exist else uses existing images.

Dockerizing Postgres and node+ts app with docker-compose.

  • Above implementation running multiple containers using the docker-compose need to define the version which is 3.8 and the no for services we want to run containers.
  • Can define each service with its configuration including container name,env, and volumes( to mount the host app to the container without building the image again and again )
  • To build the docker image from the docker-compose file need to set the build option compose will find the Dockerfile in the root to build the image and then start the container.
  • Run the container using the docker-compose.

    Now the node app and postgres are containerized you can access them on port 8001
    From the host machine on your defined port from the compose.
    Ex: http://localhost:8001/  can define test routes while creating the server to test.
Keep In Touch With Brain Inventory Sales Executive

Have an idea?
Get in touch, we’d be
happy to hear from you

We are always looking out for new collaborations, whether you are a client who is passionate about a project or a talent who is interested in joining our team, our doors are always open.

locate us

Brain Inventory India (HQ) - 618, Shekhar Central, Palasia Square, A.B Road, Indore, Madhya Pradesh, 452001

India (HQ)

618, Shekhar Central, Palasia Square, A.B Road, Indore, Madhya Pradesh, 452001

+918109561401

Brain Inventory United Kingdom office: SBVS, 8 Roundhay Road, Leeds, UK, LS7 1AB

United Kingdom

Brain Inventory, SBVS, 8 Roundhay Road, Leeds, UK, LS7 1AB

+18008209286

Brain Inventory Canada Office: 44 Main Street East Milton, ONCanada L9T 1N3

Canada

44 Main Street East Milton, ONCanada L9T 1N3

+4166696505

Brain Inventory Jordan Office: 185 Wasfi Al-Tal Street, Ammon Oasis Complex P.O Box 4724 Amman 11953 Jordan

Jordan

185 Wasfi Al-Tal Street, Ammon Oasis Complex P.O Box 4724 Amman 11953 Jordan

+960770781000

Brain Inventory USA Office: 720 Seneca St Ste 107 Seattle, USA 98101

USA

720 Seneca St Ste 107 Seattle, USA 98101

+1(206)6533419

if it's digital,we'll make it.