Dockerize monolithic apps — Part 2

Aashi Singh
2 min readMay 28, 2023

--

This series continues to provide ways to work with database and dockers in a monolithic app.

In the first part of this series, we discussed how to add docker to a monolithic app developed using Node.js and Angular. In this part, we will add a Postgresql database to the app. You can pull the code from the Github repository link.

Adding database to the app

I have added a simple Prostgres database — user-data with table — users, and used knex to integrate with the node app.

knexfile.js

To create the database and the tables when the app will run for the first time, we are using knex migrations. Run the following command — knex migrate:make user in the terminal to create the migration file for the users table in the migrations folder in the root directory and add the table structure as—

migration_user.js

In the knex_migration.js, add the following logic to create the database and run the migration file by using the command — knex.migrate.latest().

knex_migration.js

Now to run the migration script, run the command — npm run migrate. If we check the database, the table will be created. But it will be empty. Let’s add some seed data for this.

In the root directory — add a seeds folder and add a 01_user.js file. To add the seed data, run the command — npm run seed.

01_user.js

To run the migrate and seed command before starting the server, add the commands in docker-entrypoint.sh file.

#!/bin/sh
npm run migrate
npm run seed
npm run dev

Update the docker-compose.yaml file to add the database services and the entrypoint as the file created above —

docker-compose.yaml

Update the Dockerfileserver as —

Dockerfileserver

Test the app again and it should be working now!

I hope you found this post useful and please do let me know in case of any question or query. Leave a comment here and I will surely reply!

--

--

Aashi Singh

Full Stack Developer at Salesforce | Creative coder | JavaScript Nerd