Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 19, 2021 08:33 pm GMT

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.

Requirements

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

Step 1 Create a New NestJS Application

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 article "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

Step 2 (Optional) Add Environment Variables

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.

Database Services

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>

Cache Services

Redis

+REDIS_HOST=cache+REDIS_PORT=6379

Memcached

+MEMCACHED_HOST=cache+MEMCACHED_PORT=11211

Step 3 Start Your Local Environment

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

$ kool run setupnpm 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 [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})audited 879 packages in 32.143s78 packages are looking for funding  run `npm fund` for detailsfound 0 vulnerabilitiesCreating network "my-project_kool_local" with the default driverCreating volume "my-project_database" with default driverCreating volume "my-project_cache" with default driverCreating my-project_database_1 ... doneCreating my-project_app_1      ... doneCreating 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.

Level Up Your Workflow

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

Add Scripts to kool.yml

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

Run Commands

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 -vv14.17.1

Connect to the Database

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.

Add Dependencies

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 appStopping my-project_app_1 ... doneGoing to remove my-project_app_1Removing my-project_app_1 ... doneCreating my-project_app_1 ... done

View the Logs

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).

Share Your Work

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=3000Thank you for using expose.Local-URL:     app:3000Dashboard-URL: http://127.0.0.1:4040Expose-URL:    https://eeskzijcbe.kool.liveRemaining time: 00:59:59Remaining time: 00:59:58Remaining time: 00:59:57

Switch Projects

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!

Dive Deeper

GitHub logo kool-dev / kool

From local development to the cloud: development workflow made easy.

kool - cloud native dev tool

Go Report CardcodecovDocker HubGolang CI LintMaintainabilityJoin Slack Kool community

About kool

Kool is a CLI tool that brings the complexities of modern software development environments down to earth - making these environments lightweight, fast and reproducible. It reduces the complexity and learning curve of Docker and Docker Compose for local environments, and offers a simplified interface for using Kubernetes to deploy staging and production environments to the cloud.

Kool gets your local development environment up and running easily and quickly, so you have more time to build a great application. When the time is right, you can then use Kool Cloud to deploy and share your work with the world!

Kool is suitable for solo developers and teams of all sizes. It provides a hassle-free way to handle the Docker basics and immediately start using containers for development, while simultaneously guaranteeing no loss of control over more specialized Docker environments.

Learn more at kool.dev.

Installation

Requirements: Kool is


Original Link: https://dev.to/kooldev/running-nestjs-on-docker-is-really-easy-with-kool-5b8m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To