/url-shortener

Showcasing dockerised dependent containers with healthchecks and using Compose Watch

Primary LanguageJavaScript

URL Shortener

Introduction

Screencast.from.22-10-23.21.49.38.webm

This repository showcases an express service backed by a MongoDB database. It exposes an API that allows for the creation and visiting of shortened URLs. While the service itself is fully functional, the real purpose of this repository is to demonstrate other useful tools that make development easier:

  • Docker Healthchecks: Allows docker to test containers and confirm not only that they’re running but that they’re “healthy”. Containers with healthchecks have a health status in addition to their normal status. This extra status can be used to control startup order of containers.

  • Compose Watch: Automatically updates running composed services with updates to files being watched. These changes can either update files within the running container or trigger an entire rebuild of the container.

  • Testcontainers: Provides a programatic way of using docker containers for dependent services within integration tests. This means we need to stub / mock less and can have actual integration with databases and services our own application relies upon.

  • Environment Variable Validation: Using Zod, we can ensure that required environment variables are in place as soon as our application boots up. This gives us a quicker feedback loop to spot misconfigured environments that might only become obvious at a later time when functionality that uses the environment variables gets invoked and fails.

Instructions

These instructions will assume that the system already has Docker and Docker Compose present. Firstly, get the containers for the database and the application up and running:

docker compose down && docker compose build && docker compose up

While the containers are starting, you can have docker list them out to see their container status and health status:

docker ps --all

You’ll note that, thanks to the depends_on declarations, the application container won't attempt to start until the database container considers itself healthy. You can also inspect a container for detailed healthcheck logging:

docker inspect --format "{{json .State.Health }}" url-shortener-database-1 | jq

Then in another terminal, start watching for source file changes to have them replicated into running containers:

docker compose watch

Further Information

Healthchecks

Compose Watch

Testcontainers

Environment Variable Validation