NestJS, PostgreSQL, and Docker are three powerful tools that can help you build robust web applications quickly and easily. NestJS is a modern, progressive Node.js framework that allows you to build server-side applications using TypeScript. PostgreSQL is a powerful open-source database management system that provides reliable data storage and retrieval. Docker is a containerization platform that simplifies the deployment and management of applications.
These three tools provide a powerful toolkit for building scalable and efficient web applications. This article will give a beginner-friendly guide to using NestJS, PostgreSQL, and Docker to build a web application.
A progressive Node.js framework for building efficient and scalable server-side applications.
Nest framework TypeScript starter repository.
$ npm install
Running the app
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
Nest is MIT licensed.
- Basic understanding of TypeScript, PostgreSQL, and RESTful APIs
- Docker Desktop installed on your computer (run the
docker -vcommand to verify the installation); if not, install it from here
To install Node, we go to the Nodejs website and follow the instructions. We verify Node.js’ installation using the terminal command below:
v18.15.0 //node version installed
The result shows the version of Node.js we installed on our computer.
Next we’ll install NestJS globally and initialise a new project by running the command below in our terminal:
$ npm i -g @nestjs/cli
$ nest new nest-docker-postgres
Make sure to choose
npm as the package manager from the prompt.
Start the development server using the command below and visit http://localhost:3000/ to see the application running:
$ npm run start:dev
To containerize our NestJS application, we first create a file named
Dockerfile in the root of our project and then a
docker-compose.yml file also in the root of our project.
Dockerfile sets up a container for running the application. It installs the necessary dependencies, copies the application code into the container, builds the application, and starts the server using the production build.
docker-compose file sets up a multi-container environment. In it, we would run the NestJS app with PostgreSQL and pgAdmin.
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
CMD [ "npm", "run", "start:dev" ]
Here's what each command in this file does:
FROM node:18: Specifies the base image for the Docker container. We use the official Node.js
18.ximage as the base in this case.
WORKDIR /app: Sets the working directory inside the container to
/app. This is where the application code will be copied and where the container will run from.
COPY package*.json ./: Copies the
package-lock.jsonfiles from the current directory on the host machine to the
/appdirectory in the container. The
package*.jsonallows us to copy both files at once.
RUN npm install: Installs the application dependencies in the container. This step uses the
npm installcommand to install the dependencies listed in
COPY . .: Copies the application code from the host machine to the
/appdirectory in the container.
RUN npm run build: The
npm run buildcommand is used to build the application in the container. This command will typically create a
distfolder with the production build of the application.
CMD ["npm", "run", "start:dev"]: Specifies the command to run when the container starts. In this case, we're running the
npm run start:devcommand, which will start the server in development mode. This command watches for changes in the code and automatically rebuilds the container when changes are detected. This allows for faster development cycles, as changes to the code can be quickly tested in the container without manual rebuilding or restarting.
In this file:
version: This field at the beginning of a Docker Compose file specifies the version of the Compose file format we’re using.
db: This service sets up a PostgreSQL database using the official
postgresimage from Docker Hub. It sets the password for the
postgres, creates a named volume
pgdatafor storing the database data, and maps the container port
5432to the host port
restart: alwaysoption ensures that the container will automatically restart if it crashes or is stopped.
app: This service builds a Docker image for the NestJS app using the
Dockerfilein the current directory. It sets the container name to
nest-docker-postgres, sets the environment variable
PORTto the value of the host
PORTenvironment variable, maps the container port
3000to the host port
3000, and mounts the
srcdirectory on the host to the
/app/srcdirectory in the container. The
depends_onoption specifies that this service depends on the
dbservice, meaning that the
dbcontainer will be started before the
pgadmin: This service sets up pgAdmin, a web-based PostgreSQL administration tool, using the
dpage/pgadmin4image from Docker Hub. It sets the container name to
nest-pgadmin4, sets the default email and password for the pgAdmin login, maps the container port
80to the host port
5050, and specifies that this service depends on the
We also need to add a new file called
.dockerignore. It specifies files and directories that
.dockerignore should exclude from a Docker build context.
The next thing we need to do is build to run our app in Docker - building the containers. Run the command below to containerize the application:
$ docker compose up
The command looks for the services specified in the
docker-compose.yml file and creates containers for them. If the images required by those services are unavailable locally, Docker will pull them from Docker Hub or any other configured registry. If the images are already available locally, Docker will use them instead of pulling them again.
Login to pgAdmin by using the email and password we specified earlier in the
docker-compose.yml file, and then we will see this:
To connect to the PostgreSQL server from PgAdmin, we need to create a server object in PgAdmin with the details of the PostgreSQL server.
Here are the steps to create a server in PgAdmin:
- Open PgAdmin in the web browser by visiting http://localhost:5050 (assuming we're using the default configuration in the
- Log in using your email and password in the
docker-compose.ymlfile for the
- In the left-hand sidebar, click
Serversto expand the
- Right-click on
- In the
Generaltab of the
Create - Serverdialog, we can give the server a name of our choice.
- In the
Connectiontab, fill in the following details:
- Host name/address: db
- Port: 5432
- Maintenance database: postgres
- Username: postgres
- Password: postgres
Saveto save the server configuration.
Note: Since the PostgreSQL server is running in a Docker container, the hostname/address would be the name of the Docker service for the database container as defined in the
docker-compose.ymlfile. By default, the name of the service becomes the hostname/address of the container within the Docker network.
We should now see the server we created in the left-hand sidebar of PgAdmin. We can expand the server to see the databases and other objects within it.
Nest provides the
@nestjs/typeorm package for integrating with SQL and NoSQL databases. Install the packages using the command below:
$ npm install --save @nestjs/typeorm typeorm pg
Next, in the
app.module.ts file, add the following code in the
We import the
TypeOrmModule from the
We pass an
options object to the
TypeOrmModule to configure the PostgreSQL connection details. The
type option specifies the database management system being used, PostgreSQL. The
database options provide the connection details for the PostgreSQL server.
We use the
entities option to specify the entities that the application uses. Entities represent the data models in the application and can be used to query the database. We don't specify entities in this case because we dynamically load them by setting
We set the
synchronize option to
true, which means that TypeORM will automatically generate database tables based on the entities. However, this option should be used with caution in production because it can cause data loss and conflicts.
To rebuild our app container because of the changes made, we execute the command below:
$ docker compose up --build
And voila, our app is connected to the PostgreSQL server running in Docker. We can now start to create entities and build APIs on top of them.
In conclusion, we have seen how to set up a NestJS application with Docker and PostgreSQL in a step-by-step guide. Using Docker and PostgreSQL in combination with NestJS can make your web application development faster and more efficient.