Max Rohowsky, Ph.D.

VPS Learning Log

Serverless or Virtual Private Servers (VPS)? I've had a hard time choosing between the two. Since I have a good grasp on serverless, I spend the last few days learning about VPS. The post below is a brain-dump of what I've learned for future reference.

Linux Basics

When you setup a VPS, you'll usually have a Linux distribution installed (e.g. Ubuntu) and a few goodies like git, docker, etc. Here are some basic commands to navigate the server via the terminal:

Secure Shell (SSH) Login

  • ssh keygen - generate a public and private key pair
  • ssh root@server_ip - connect to the server

The public key is stored on the server and the private key is stored on the local machine in the default .ssh/ directory.

Basic Linux Commands

  • ls - list the files in the directory
    • -l flag to show long format
    • -a flag to show all files including hidden ones
    • -R flag to show recursively files in subdirectories
  • mkdir - create a directory
  • rm - remove a file
  • rm -r - remove a directory and its contents recursively
  • rmdir - remove an empty directory
  • cd .. - go up one directory
  • cd / - go to the root directory

FTP Login on Hetzner (FileZilla)

  1. Open FileZilla and press Ctrl + S to open Site Manager.
  2. Configure the connection:
    • Protocol: SFTP
    • Host: IP address (e.g., 49.12.205.120)
    • User: root
    FileZilla connection to Hetzner
  3. Set up the private key:
    • Click "Browse" next to "Key file" and show all the files
    • Select the private key that was generated from ssh-keygen
    • Then accept the prompt to convert the key to PuTTY format
    • Set a password to protect the converted file
    • Keyfile location: Should then point to a .ppk file
  4. Finally click "Connect" to establish the connection

Docker Basics

Keywords

  • Dockerfile: A text file containing instructions to build a Docker image. Each instruction creates a layer in the image.
  • Docker Image: The output from building a Dockerfile. It's a read-only template with instructions for creating a Docker container.
  • Docker Container: A runnable instance of an image that encapsulates an application and its environment isolated from other containers and the host system.
  • Volume: Persist data generated by and used by Docker containers. They can be shared and reused among containers and exists outside the lifecycle of a container.
  • Docker Compose: Tool for defining and running multi-container Docker applications. Every service defined in the compose file will be run in a separate container.

Docker Commands

  • docker ps - list running containers
  • docker ps -a - show all containers, including stopped ones
  • docker inspect <container name> - get detailed information about a container
  • docker images - list all Docker images
  • docker images -q - list image IDs
  • docker rmi $(docker images -q) - remove all images
  • docker volume ls - list all volumes
  • docker volume inspect <volume_name> - inspect a specific volume

Docker Compose Commands

  • docker-compose up - build and start containers
  • docker-compose up -d - build and start containers in detached mode
  • docker-compose -f docker-compose.dev.yml up - build and start using a specific compose file
  • docker-compose down - stop and remove containers
  • docker-compose down -v - stop and remove containers and volumes
  • docker-compose exec <service name> <command> - execute a command in a specific container
  • docker-compose logs <service name> - view logs for a specific container
  • docker-compose build - build the images
  • docker-compose build --no-cache - build the images without using cache

Containerized PostgreSQL

Accessing the Database in a Container

Open a shell in the container using the following command:

docker exec -it <container_name> bash

From within the container, the PostgreSQL command line interface is available. So you can connect to the database using:

psql -U <username> <database_name>

The username and database name are defined in the docker-compose.yml file.

If you make changes to the docker-compose.yml file, you need to rebuild the image using docker-compose down -v and docker-compose up.

Useful PostgreSQL Commands

  • \l - list databases
  • \dt - list tables in the current database
  • \du - list users
  • \dt - list tables in the current database
  • \q - quit the PostgreSQL command line interface

Notes on Localhost

  • Localhost refers to the local machine you are using. It is a hostname that resolves to the IP address 127.0.0.1, which is the loopback address for your computer. This means you are trying to access a service running on your own machine.
  • The :3000 specifies the port number on which the service is listening. In the context of a web application, this is the port where the web server (in my case, a Next.js application) is running.
  • When you enter localhost:3000 in your browser, the browser sends an HTTP request to the web server running on your local machine at port 3000.
  • If you have a Docker container running your Next.js application and you mapped port 3000 of the container to port 3000 of your host (using the -p 3000:3000 option), the request will be routed to the application running inside the Docker container.

Miscellaneous

  • In a docker compose environment, services can communicate with each other using their service names. For example, db will resolve to the the IP address of the container running the database service.
  • Always make sure to rebuild the docker image if you make changes to the docker compose file.
  • For local connections, PostgreSQL won't ask for a password because it's configured with "trust" authentication, as indicated by the warning message during initialization.