Yugabyte Docker container Init Script

Hi All,
I am creating single node master and tablet server using docker -compose file as mentioned below.

version: ‘2’

volumes:
yb-master-data-1:
yb-tserver-data-1:

services:
yb-master:
image: yugabytedb/yugabyte:latest
container_name: yb-master-n1
volumes:
- yb-master-data-1:/mnt/master
- ./src/test/resources/data.sql:/docker-entrypoint-initidb.d
command: [ “/home/yugabyte/bin/yb-master”,
“–fs_data_dirs=/mnt/master”,
“–master_addresses=yb-master-n1:7100”,
“–rpc_bind_addresses=yb-master-n1:7100”,
“–replication_factor=1”]
ports:
- “7000:7000”
environment:
SERVICE_7000_NAME: yb-master

yb-tserver:
image: yugabytedb/yugabyte:latest
container_name: yb-tserver-n1
volumes:
- yb-tserver-data-1:/mnt/tserver
- ./src/test/resources/data.sql:/docker-entrypoint-initidb.d
command: [ “/home/yugabyte/bin/yb-tserver”,
“–fs_data_dirs=/mnt/tserver”,
“–start_pgsql_proxy”,
“–rpc_bind_addresses=yb-tserver-n1:9100”,
“–tserver_master_addrs=yb-master-n1:7100”]
ports:
- “9042:9042”
- “5433:5433”
- “9000:9000”
environment:
SERVICE_5433_NAME: ysql
SERVICE_9042_NAME: ycql
SERVICE_6379_NAME: yedis
SERVICE_9000_NAME: yb-tserver
depends_on:
- yb-master

I need to run seed / init scripts after database is created. I tried using volume mount my sql file using

  • ./src/test/resources/data.sql:/docker-entrypoint-initidb.d

However these scripts are not executed. Is there any other way i can achieve this.

Hi Ganesh,
I use a side service like this for that:

  yb-demo-init:
      image: yugabytedb/yugabyte:latest
      volumes:
          - ./scripts:/home/yugabyte/scripts
      command: |
       bash -c "
        bin/ysqlsh v ON_ERROR_STOP=1 -c 'select * from yb_servers()' &&
         ./scripts/script.sh
       "
      deploy:
          restart_policy:
             condition: on-failure

Hi Franck,

Thank you for information. I am trying to run bash script like this on yugabyte tablet server to run my init script data.sql. However ysqlsh command is not getting executed by docker container.
Strange behaviour that i have observed is, after execution of command “yb-tserver” the ysqlsh command is not getting executed. There are no failure logs .

#!/bin/bash
ipaddress=`hostname  -I | cut -f1 -d' '`
/home/yugabyte/bin/yb-tserver --fs_data_dirs=/mnt/tserver --start_pgsql_proxy --rpc_bind_addresses=yb-tserver-n1:9100 --tserver_master_addrs=yb-master-n1:7100
ysqlsh -h $ipaddress -f /docker-entrypoint-initidb.d

Regards,
Ganesh

Hi Ganesh, I think the 5433 port is not immediately opened but that should show an error message

thinking about it, yb-tserver doesn’t go to the background, that’s the reason

Note that for simple configuration we have a yugabyted service that starts yb-master and yb-tserver in the background

Thank you Franck, !!!
Got it.
However , I achieved the intended result other way using spring boot.

1 Like