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. Ho…


This content originally appeared on DEV Community and was authored by Franck Pachot

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 PGPASSWORD=yugabyte bin/ysqlsh -v ON_ERROR_STOP=1 \\
      -c \"create database $${POSTGRES_DB} \" \\
      -c \"create user $${POSTGRES_USER} password '$${POSTGRES_PASSWORD}' \" \\
      2>/dev/null
      do
       echo 'Waiting for YugabyteDB to be up...' ; sleep 1
      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'

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


This content originally appeared on DEV Community and was authored by Franck Pachot


Print Share Comment Cite Upload Translate Updates
APA

Franck Pachot | Sciencx (2022-05-04T14:37:19+00:00) docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env. Retrieved from https://www.scien.cx/2022/05/04/docker-compose-yaml-to-start-yugabytedb-with-postgres_user-postgres_password-postgres_db-env/

MLA
" » docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env." Franck Pachot | Sciencx - Wednesday May 4, 2022, https://www.scien.cx/2022/05/04/docker-compose-yaml-to-start-yugabytedb-with-postgres_user-postgres_password-postgres_db-env/
HARVARD
Franck Pachot | Sciencx Wednesday May 4, 2022 » docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env., viewed ,<https://www.scien.cx/2022/05/04/docker-compose-yaml-to-start-yugabytedb-with-postgres_user-postgres_password-postgres_db-env/>
VANCOUVER
Franck Pachot | Sciencx - » docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/04/docker-compose-yaml-to-start-yugabytedb-with-postgres_user-postgres_password-postgres_db-env/
CHICAGO
" » docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env." Franck Pachot | Sciencx - Accessed . https://www.scien.cx/2022/05/04/docker-compose-yaml-to-start-yugabytedb-with-postgres_user-postgres_password-postgres_db-env/
IEEE
" » docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env." Franck Pachot | Sciencx [Online]. Available: https://www.scien.cx/2022/05/04/docker-compose-yaml-to-start-yugabytedb-with-postgres_user-postgres_password-postgres_db-env/. [Accessed: ]
rf:citation
» docker-compose.yaml to start YugabyteDB with POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB env | Franck Pachot | Sciencx | https://www.scien.cx/2022/05/04/docker-compose-yaml-to-start-yugabytedb-with-postgres_user-postgres_password-postgres_db-env/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.