Integrate vault system in laravel application managed by docker

I am trying to integrate the vault system to manage the env credential. Earlier I was managing the credentials using heml-chart. After vault integration application sometimes throws db refused to connect error.

I am creating .env in Dockerfile and update the .env inside service-entry.sh

Env file is getting generated correctly which I am verifying using cat .env command inside service-entry.sh.

Any suggestion would be helpful in why the application is throwing db refused to connect error in some requests.

Here is my Dockerfile

FROM php:7.3-fpm

# Set working directory
WORKDIR /b2b

# Install dependencies
RUN apt-get update && apt-get install -y apt-utils
RUN apt-get install -y 
   wget 
   build-essential 
   libpng-dev 
   libjpeg62-turbo-dev 
   libfreetype6-dev 
   locales 
   zip 
   libzip-dev 
   jpegoptim optipng pngquant gifsicle 
   vim 
   unzip 
   curl 
   libxml2-dev 
   openssl 
   procps 
   jq

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl soap
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-install bcmath

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy existing application directory contents
COPY service-entry.sh .

COPY ./aqualens_b2b_in .

# Create .env file and set permissions
RUN touch .env && chmod 0777 .env

RUN chmod -R 0777 ./storage
RUN chmod -R 0777 ./bootstrap

RUN mkdir -p ./storage/framework/cache
RUN mkdir -p ./storage/framework/sessions
RUN mkdir -p ./storage/framework/views

RUN chmod -R 0777 ./storage

# Set the entry point for the container
CMD ["sh","/b2b/service-entry.sh" ]

RUN composer install
RUN php artisan optimize

Here is service-entry.sh file

#!/bin/sh

# For VAULT

: ${VAULT_ENABLED:="true"}
: ${VAULT_SERVICE_ACCOUNT_TOKEN_FILE:="/var/run/secrets/kubernetes.io/serviceaccount/token"}
: ${VAULT_URI:="http://vault-cluster-url.local:2000"}
: ${VAULT_ROLE:="vault-myapp-config-b2b-auth-role"}
: ${VAULT_BACKEND:="company/myapp/myapp-config"}
: ${VAULT_DEFAULT_CONTEXT:="myapp-b2b"}

# Check and print the current directory and its permissions
echo "Current directory: $(pwd)"
ls -ld .
ls -a
ls -l

if [ "$VAULT_ENABLED" = "true" ]; then
    kubernetesToken=$(cat "${VAULT_SERVICE_ACCOUNT_TOKEN_FILE}")
    if [ $? -ne 0 ]; then
        echo "Error: Unable to read Kubernetes service account token file"
    fi

    vaultEndpoint="${VAULT_URI}/v1/auth/kubernetes/login"
    H1_KUBE="Content-Type: application/json"
    H2_KUBE="Authorization: Bearer ${kubernetesToken}"

    requestData="{"role":"${VAULT_ROLE}","jwt":"${kubernetesToken}"}"
    response=$(curl -s -X POST -H "$H1_KUBE" -H "$H2_KUBE" --data "${requestData}" "${vaultEndpoint}")


    if [ $? -ne 0 ]; then
        echo "Error: Unable to authenticate with Vault"
    fi

    access_token=$(echo "${response}" | jq -r .auth.client_token)
    if [ -z "$access_token" ] || [ "$access_token" = "null" ]; then
        echo "Error: Unable to retrieve access token from Vault response"
    fi

    H1_VAULT="X-Vault-Token: ${access_token}"
    H2_VAULT="Accept: application/json"

    RESPONSE_VAULT=$(curl -s -H "$H1_VAULT" -H "$H2_VAULT" "${VAULT_URI}/v1/${VAULT_BACKEND}/${VAULT_DEFAULT_CONTEXT}")

    if [ $? -ne 0 ]; then
        echo "Error: Unable to fetch vault data"
    fi

    export_statements=$(echo "$RESPONSE_VAULT" | jq -r '.data | to_entries[] | @sh "export (.key)=(.value)"')
    if [ -z "$export_statements" ]; then
        echo "Error: No data retrieved from Vault"
    fi

    echo "Logging Qualens Vault Integration:"
    echo "export_statements"
    echo "$export_statements"

    # Read each export statement and process it
    echo "$export_statements" | while IFS= read -r line; do
        eval "$line"
        key=$(echo "$line" | cut -d'=' -f1 | cut -d' ' -f2)
        value=$(echo "$line" | cut -d'=' -f2- | sed "s/^'//" | sed "s/'$//")
        if grep -q "^${key}=" .env; then
            # Update existing variable
            sed -i "s|^${key}=.*|${key}=${value}|g" .env
        else
            # Add new variable
            echo "${key}=${value}" >> .env
        fi
    done

     # Debug: Print contents of .env file after update
    echo "Contents of .env file after update:"
    cat .env

fi

# For PHP
php-fpm

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật