Blog » tutorials

Running NestJS on Docker is Really Easy with Kool

In just 3 simple steps, you can use Kool to start a new NestJS application running in a local Docker development environment.

Kool is a free, open source CLI tool that makes local development with Docker super easy. Kool CLI will level up your development workflow, and help you and your team improve the way you develop and deploy cloud native applications.

If you haven't done so already, you first need to install Docker and the Kool CLI.

If you already have kool installed, make sure you're running the latest version:

$ kool self-update

Use the kool create command to create your new NestJS project.

$ kool create nestjs my-project

Under the hood, this command will run nest new my-project to install NestJS using a customized Kool Docker Image: kooldev/node:14-nest.

Learn more about Kool Docker Images in our blog post "Use Kool to Dockerize Your Local Development Environment the Right Way".

After installing NestJS, kool create automatically runs the kool preset nestjs command, which helps you set up the initial tech stack for your project using an interactive wizard.

$ Preset nestjs is initializing! ? Which database service do you want to use [Use arrows to move, type to filter] > MySQL 8.0 MySQL 5.7 PostgreSQL 13.0 none ? Which cache service do you want to use [Use arrows to move, type to filter] > Redis 6.0 Memcached 1.6 none ? Which package manager did you choose during Nest setup [Use arrows to move, type to filter] > npm yarn $ Preset nestjs initialized!

Now, move into your new NestJS project:

$ cd my-project

The kool preset command auto-generated the following configuration files and added them to your project, which you can easily modify and extend to suit your needs.

+docker-compose.yml +kool.yml

You can skip this step if you did not add a database or cache service to your project via the wizard.

If you added a database and/or cache service, you'll need to add some local environment variables to match the services in your docker-compose.yml file (see below). For example, you'll need to change the hosts from localhost to the appropriate service container name, which are accessible from within Docker.

Create a .env file inside your project's root directory, and add the appropriate environment variables to this file (as provided below) based on the services used in your project.

Learn more about how to configure NestJS.

MySQL 5.7 and 8.0

+DB_CONNECTION=mysql +DB_HOST=database +DB_DATABASE=<some_database_name> +DB_USERNAME=<some_username> +DB_PASSWORD=<some_password>

PostgreSQL 13.0

+DB_CONNECTION=pgsql +DB_HOST=database +DB_PORT=5432 +DB_DATABASE=<some_database_name> +DB_USERNAME=<some_username> +DB_PASSWORD=<some_password>

Redis

+REDIS_HOST=cache +REDIS_PORT=6379

Memcached

+MEMCACHED_HOST=cache +MEMCACHED_PORT=11211

Now, spin up your local environment for the first time using the setup script in your kool.yml file:

$ kool run setup npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.1 (node_modules/chokidar/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) audited 879 packages in 32.143s 78 packages are looking for funding run `npm fund` for details found 0 vulnerabilities Creating network "my-project_kool_local" with the default driver Creating volume "my-project_database" with default driver Creating volume "my-project_cache" with default driver Creating my-project_database_1 ... done Creating my-project_app_1 ... done Creating my-project_cache_1 ... done

That's it!

Once kool run setup finishes, you can access your new NestJS app at http://localhost:3000 and see the NestJS "Hello World!" welcome page.

Now that you have your new NestJS app up and running, you can use the Kool CLI to start leveling up your development workflow.

Think of kool.yml as a super easy-to-use task helper. Instead of writing custom shell scripts, you can add your own scripts to kool.yml (under the scripts key), and run them with kool run SCRIPT. You can add single line commands (kool run nest), or add a list of commands that will be executed in sequence (kool run setup). For example, add scripts to run database migrations, reset local environments, run static analysis tools, and so on. Think about how much easier it will be to onboard a teammate or new developer :)

scripts: mysql: kool exec -e MYSQL_PWD=$DB_PASSWORD database mysql -u $DB_USERNAME $DB_DATABASE # or psql for PostgreSQL nest: kool exec app nest npm: kool exec app npm # or yarn npx: kool exec app npx setup: - kool docker kooldev/node:14 npm install # or yarn install - kool start

When you need to execute a command inside a running service container, use the kool exec command. Run the following to check the version of Node running in your app container.

$ kool exec app node -v v14.17.1

If you added a database service, start a new SQL client session inside your running database container by executing kool run mysql (MySQL) or kool run psql (PostgreSQL). This runs the mysql or psql script in your kool.yml.

As your project evolves, and you add more dependencies to package.json, use the kool restart command to restart your app container and load the new packages.

$ kool restart app Stopping my-project_app_1 ... done Going to remove my-project_app_1 Removing my-project_app_1 ... done Creating my-project_app_1 ... done

View container logs using the kool logs command. Run kool logs to see the logs for all running containers, or kool logs app to specify a service and only see the logs for the app container. Add the -f option after kool logs to follow the logs (i.e. kool logs -f app).

When you need to quickly share local changes with your team, use the kool share command to share your local environment over the Internet via an HTTP tunnel. Specify your own subdomain using the --subdomain flag.

$ kool share --port=3000 Thank you for using expose. Local-URL: app:3000 Dashboard-URL: http://127.0.0.1:4040 Expose-URL: https://eeskzijcbe.kool.live Remaining time: 00:59:59 Remaining time: 00:59:58 Remaining time: 00:59:57

Kool supports any language or framework, so you can standardize the way you work across all your tech stacks. When it's time to stop working on your new NestJS app and switch to a different project, you can easily change local Docker environments by running kool stop on the NestJS project, moving into the other project directory, and running kool start.

$ kool stop $ cd ~/my-other-project $ kool start

Pretty kool, right?

If you like what we're doing, show your support for this new open source project by starring us on GitHub!

Published Jul 16, 2021

Tags

nestjs preset

Categories

kool.dev
By choosing "Accept all cookies" you agree to the use of cookies to help us provide you with a better user experience and to analyse website usage. Only the essential cookies are necessary for the proper functioning of our website and cannot be refused.
Check out our Cookie Policy and Privacy Policy for more information.