๐Ÿš€ Getting Started with Dozer with Docker: A Step-by-Step Tutorial

ยท

3 min read

In the world of data-driven applications, accessing and manipulating data in real-time is crucial. Dozer is a powerful tool that helps you create low-latency data APIs (gRPC and REST) from any data source, enabling seamless integration with frontend applications. In this tutorial, we'll walk you through setting up Dozer with Docker and connecting it to PostgreSQL using Docker.

๐ŸŒŸ Why Dozer?

Dozer addresses the challenges of accessing and manipulating data in real-time by providing an easy-to-use solution to create efficient data APIs. With Dozer, you can quickly set up APIs that can be easily integrated with your frontend applications. Furthermore, using Docker allows you to containerize your application, making it portable and easy to manage.

In this step-by-step tutorial, we'll guide you through connecting PostgreSQL with Dozer using Docker. Let's get started!

๐Ÿ”ง Prerequisites:

  • Docker/Docker desktop installed on your machine

  • Basic knowledge of Docker and PostgreSQL

๐Ÿ“ Overall steps involved:

  • Create a docker-compose file for PostgreSQL

  • Create a Dozer config with PostgreSQL connector and API configuration

  • Run PostgreSQL (via Docker)

  • Run Dozer (via Docker)

  • Query your API

๐Ÿ”น Step 1: Create a docker-compose.yml file

First, create a docker-compose.yml file and copy the following configuration to it or download the file here:

version: '3.9'
services:
  postgres:
    platform: linux/amd64
    container_name: quick-start-postgres
    image: public.ecr.aws/getdozer/dozer-samples-pg-stocks:latest
    command: postgres -c hba_file=/var/lib/stock-sample/pg_hba.conf
    environment:
      POSTGRES_DB: stocks
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      ALLOW_IP_RANGE: 0.0.0.0/0
    ports:
      - '5434:5432'
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres  -h 0.0.0.0 -d stocks" ]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  sample-data:

๐Ÿ”น Step 2: Create a Dozer config file

Next, create a dozer-config.yaml file with the following content:

app_name: 1-hypercharge-postgres-sample
connections:
  - config: !Postgres
      user: postgres
      password: postgres
      host: host.docker.internal
      port: 5434
      database: stocks
    name: stocks
sources:
  - name: stocks
    table_name: stocks
    columns:
      - id
      - ticker
      - date
      - open
      - high
      - low
      - close
      - adj_close
      - volume
    connection: !Ref stocks
  - name: stocks_meta
    table_name: stocks_meta
    columns:
      - symbol
      - security_name
    connection: !Ref stocks
endpoints:
  - name: stocks
    path: /stocks
    table_name: stocks
    index:
      primary_key:
        - id
  - id: null
    name: stocks_meta
    path: /stocks-meta
    table_name: stocks_meta
    index:
      primary_key:
        - symbol

๐Ÿ”น Step 3: Run PostgreSQL with sample stock data

Open a terminal or command prompt and navigate to the directory containing the docker-compose.yml file. Run the following command to start PostgreSQL with sample stock data:

docker-compose up

๐Ÿ”น Step 4: Run Dozer with PostgreSQL connector (via Docker)

Open another terminal or command prompt and navigate to the directory containing the dozer-config.yaml file. To run Dozer as a Docker container, execute the following command:

docker run -it \
       -v "$PWD":/usr/dozer \
       -p 8080:8080 \
       -p 50051:50051 \
       --platform linux/amd64 \
       public.ecr.aws/k7k6x1d4/dozer \
       dozer

Congratulations ๐Ÿฅณ , you should see Dozer up and running!

๐ŸŽ‰ Query your API

Dozer automatically generates APIs in both REST and gRPC formats, along with documentation, OpenAPI, and proto files. In the following sections, we'll showcase some possible queries. Refer to the APIs section for more details.

๐Ÿ”ธ Query Using gRPC:

You can use gRPCurl or Postman to interact with gRPC APIs.

gRPC query example:

grpcurl -plaintext localhost:50051 dozer.generated.stocks_meta.StocksMetas/query

๐Ÿ”ธ Query Using REST:

You can use curl or Postman to interact with REST APIs.

REST query example:

curl --location --request GET 'localhost:8080/stocks-meta'

That's it! You've successfully set up Dozer with Docker and connected it to PostgreSQL. Now you can easily create APIs for your data and integrate them into your frontend applications.

If you're interested in learning more about how to use Dozer with applications, we recommend reading our previous blog post: "Building a Real-Time Data App with Dozer, React, and PostgreSQL". In this blog, you'll find a detailed tutorial that demonstrates how to integrate Dozer with a React frontend application and PostgreSQL as a data source. This comprehensive guide will give you a better understanding of how to build powerful, real-time data-driven applications using Dozer.

Happy coding, Happy Data APIng! ๐Ÿš€๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

ย