DEV Community

Franck Pachot
Franck Pachot

Posted on • Updated on

docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env

The Docker image provided by Yugabyte for YugabyteDB doesn't create and start a database because it can be used for different purposes: start one yb-master or yb-tserver, or both with yugabyted. An operational database is composed of multiple nodes. However, for development or automated tests, you just want to start one container with a defined database, user and password.

YugabyteDB is PostgreSQL compatible, which means that you can replace PostgreSQL by YugabyteDB without changing the application. The PostgreSQL image allows to create a database and set user and password with the following environment variables: POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB and you probably want the same for YugabyteDB. And, why not, with the same environment variables.

Here is an example based on knex.js test suite https://github.com/knex/knex/blob/master/scripts/docker-compose.yml

This docker-compose.yml starts PostgreSQL with a database named knex_test and user/password testuser/knextest and starts another container to check when this connection is available.

Here is how I add YugabyteDB in the same way:

version: '3'
services:

  yugabytedb:
    image: docker.io/yugabytedb/yugabyte:latest
    command: |
     bash -c "
     # create database and user as soon as database is up
     until [ -z "POSTGRES_USER" ] || PGPASSWORD=yugabyte bin/ysqlsh -v ON_ERROR_STOP=1 \\
      -c \"create database $${POSTGRES_DB:-$${POSTGRES_USER}} \" \\
      -c \"create user $${POSTGRES_USER} password '$${POSTGRES_PASSWORD}' \" \\
      2>/dev/null
      do
       echo \"Waiting for YugabyteDB to be up for creating user $${POSTGRES_USER}.\" ; sleep 5
      done &
     # start YugabyteDB
     bin/yugabyted start --daemon=false --tserver_flags='ysql_enable_auth=true'
     "
    environment:
      - POSTGRES_USER=testuser
      - POSTGRES_PASSWORD=knextest
      - POSTGRES_DB=knex_test
    ports:
      - 25433:5433
      - 27000:7000

  waityugabyte:
    image: postgres:13-alpine
    links:
      - yugabytedb
    depends_on:
      - yugabytedb
    entrypoint:
      - bash
      - -c
      - 'until PGPASSWORD=knextest /usr/local/bin/psql postgres://testuser@yugabytedb:5433/knex_test -c "SELECT 1"; do sleep 5; done'

Enter fullscreen mode Exit fullscreen mode

The YugabyteDB database is started with bin/yugabyted start --daemon=false --tserver_flags='ysql_enable_auth=true' and a background loop tries to connect to create the database and the user according to the environment variables.

The default port for YugabyteBD is 5433 which makes it possible to start it along with PostgreSQL on its default port. The port 7000 is the graphical console.

If you want a more complex docker-compose.yaml with full control on yb-master/yb-tserver cluster topology, here is how I generate it for my labs: https://github.com/FranckPachot/ybdemo/tree/main/docker/yb-lab

Discussion (0)