VPS Essentials: List of Tools and Basic Commands

PM2: Processes

What is PM2

PM2 is a production process manager for Node.js applications, designed to manage and keep your application alive forever. It provides a simple interface to start, stop, restart, and monitor Node.js applications, making it an essential tool for developers looking to enhance the reliability and performance of their applications in production environments. PM2 also supports clustering, allowing you to take full advantage of multi-core systems. The benefits of using Docker containers are as follows:

  • Process Management : PM2 allows you to easily manage multiple Node.js applications, ensuring they are running smoothly and efficiently.
  • Automatic Restarts : If an application crashes, PM2 will automatically restart it, minimizing downtime and enhancing reliability.
  • Load Balancing : With built-in cluster mode, PM2 can distribute the load across multiple CPU cores, optimizing resource usage and improving performance.

Installing PM2

Installing PM2 is a straightforward process, but before you do that, you need to have Node.js and npm (Node Package Manager) installed on your system. Below is a comprehensive guide, including steps to install Node.js and npm for beginners.

  • Install Node.js and npm (Replace 16.x with the desired version)
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
  • Verify the Installation
node -v
npm -v
  • Install PM2 globally
npm install pm2 -g
  • Verify the installation
pm2 -v

Starting Applications

When using PM2 to start your Node.js applications, the command pm2 start app.js can be enhanced with various options to customize the behavior of the process. These available options help manage application instances effectively based on your needs. Here are key parameters that you can add to the pm2 start command

  • Basic Command :
pm2 start app.js --name my-app
  • Available options, such as --name, --watch, and --env.
pm2 start npm --name "PROGRESS NAME" -- run dev

Managing Processes

  • View the process list:
pm2 list
  • Stop and restart processes:
pm2 stop <app_name | id>
pm2 restart <app_name | id>
  • Delete a process:
pm2 delete <app_name | id>

Process Persistence

  • Make PM2 automatically start processes after a server reboot
pm2 startup
pm2 save

Monitoring

  • Monitor application state and performance:
pm2 monit
  • View logs:
pm2 logs <app_name>

Docker: Containers

What is a Docker

A Docker container is a standardized unit of software that packages up code and all its dependencies so that the application runs quickly and reliably in different computing environments. Containers are lightweight and share the host operating system’s kernel, which makes them more efficient than traditional virtual machines. The benefits of using Docker containers are as follows:

  • Portability : Docker containers can run on any machine that has Docker installed, eliminating the “it works on my machine” problem.
  • Isolation : Each container runs in its own isolated environment, ensuring that applications do not interfere with each other.
  • Scalability : Docker makes it easy to scale applications up or down as needed.
  • Efficiency : Containers are lightweight and consume fewer resources compared to virtual machines.

Installing Docker

To use Docker, you need to install it on your machine. You can download Docker Desktop for Windows or macOS, or install Docker Engine on various Linux distributions. Follow the official Docker installation guide for detailed instructions. Here is the installation of the old version (may not be feasible)

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
docker --version

Running a Container

When you run a Docker container, you can customize its behavior using various parameters. Here are some common options you can use with the docker run command:

  • Basic Command :
docker run -d --name <container_name> <image_name>
  • Run in Detached Mode :Use the -d flag to run the container in the background (detached mode)
  • Map Ports : Use the -p option to map a port on the host to a port on the container
  • Set Environment Variables : Use the -e option to set environment variables while running the container
  • Mount Volumes : Use the -v option to mount a directory from the host into the container
  • Run with Limitations : You can limit resource usage with --memory and --cpus
  • Specify a Command : You can override the default command defined in the Dockerfile by specifying a command at the end of the docker run command

Managing Containers

Docker containers are a vital component when working with containerized applications. Managing these containers effectively is essential for ensuring smooth operations and resource optimization. In this section, we will explore common commands for starting, stopping, and deleting Docker containers, along with other essential management tasks.

  • Viewing Container Status
docker ps
docker ps -a
  • Starting an already stopped container
docker start <container_name>
  • Stopping and Restarting a Container
docker stop <container_name>
docker restart <container_name>
  • Deleting a Container
docker rm <container_name>
docker rm -f <container_name>
  • Viewing Container Details
docker inspect <container_name>
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>
  • Viewing Logs
docker logs <container_name>
docker logs -f <container_name>

Managing Volumes

Docker volumes are essential for persisting data generated by and used by Docker containers. They provide a mechanism to share data between containers and to retain data even when containers are stopped or removed. In this section, we will explore common commands for managing Docker volumes.

  • Listing Volumes
docker volume ls
  • Inspecting a Volume
docker volume inspect <volume_name>
  • Removing a Volume
docker volume rm <volume_name>
  • Checking Volume Usage
docker system df -v

Managing Images

Docker images are the building blocks of Docker containers. They contain everything needed to run an application, including the code, runtime, libraries, and environment variables. In this section, we will explore common commands for managing Docker images.

  • Listing Images
docker images
  • Inspecting an Image
docker inspect <image_name>
  • Removing an Image
docker rmi <image_name>
docker rmi -f <image_name>
  • Deleting old unused images
docker image prune
docker image prune -a

No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next