Docker / Postgresql / nodejs

this is going to be very simple and short article about how you can virtualize nodejs and postgres using docker. This article will give you basic idea of docker and docker compose. Lets start by creating nodejs app with postgres.

yarn init -y
yarn add express pg
touch server.js

we are going to use pg to connect and execute queries in postgres.You can use typeorm, sequelize ORM etc.

Lets write server.js

First we import express and Pool from express and pg. Then we created app

using express() . Then we made a table inside the database example.

We listen to a port and we created a base route, inside it we run a query to insert a user and return all users. This is ok to know that nodejs is using postgres and our docker-compose is working fine.

touch Dockerfile
touch docke-compose.yml

Dockerfile

docker-compose.yml

run

sudo docker-compose up

Thats it 🥳