phplaraveldockerssh-tunnellaradock

Laravel DB::connection unable to connect postgreSQL with laradock+ssh tunnel


I have a Laravel project running in docker with laradock, it use two db connections, one from postgres docker and another one from remote server, throw ssh tunnel.

If I run DB::connection('remotedb')->getPDO(); from php artisan tinker, I get:

Doctrine/DBAL/Driver/PDO/Exception with message 'SQLSTATE[08006] [7] could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.9512"?'

But if i do: pg_connect("host=postgres_tunnel dbname=remotedbname user=postgres port=9512"); inside laravel tinker, I get pgsql link resource #1483so there is no errors and connection was success. The problem is only using laravel DB class.

laradock docker-compose.yaml

    workspace-7.4:
      build:
        context: ./workspace-7.4
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - SHELL_OH_MY_ZSH=${SHELL_OH_MY_ZSH}
          - SHELL_OH_MY_ZSH_AUTOSUGESTIONS=${SHELL_OH_MY_ZSH_AUTOSUGESTIONS}
          - SHELL_OH_MY_ZSH_ALIASES=${SHELL_OH_MY_ZSH_ALIASES}
          - BASE_IMAGE_TAG_PREFIX=${WORKSPACE_BASE_IMAGE_TAG_PREFIX}
          - LARADOCK_PHP_VERSION=7.4
          - LARADOCK_PHALCON_VERSION=${PHALCON_VERSION}
          - INSTALL_SUBVERSION=${WORKSPACE_INSTALL_SUBVERSION}
          - INSTALL_BZ2=${WORKSPACE_INSTALL_BZ2}
          - INSTALL_GMP=${WORKSPACE_INSTALL_GMP}
          - INSTALL_GNUPG=${WORKSPACE_INSTALL_GNUPG}
          - INSTALL_XDEBUG=${WORKSPACE_INSTALL_XDEBUG}
          - XDEBUG_PORT=${WORKSPACE_XDEBUG_PORT}
          - INSTALL_PCOV=${WORKSPACE_INSTALL_PCOV}
          - INSTALL_PHPDBG=${WORKSPACE_INSTALL_PHPDBG}
          - INSTALL_BLACKFIRE=${INSTALL_BLACKFIRE}
          - INSTALL_SSH2=${WORKSPACE_INSTALL_SSH2}
          - INSTALL_SOAP=${WORKSPACE_INSTALL_SOAP}
          - INSTALL_XSL=${WORKSPACE_INSTALL_XSL}
          - INSTALL_LDAP=${WORKSPACE_INSTALL_LDAP}
          - INSTALL_SMB=${WORKSPACE_INSTALL_SMB}
          - INSTALL_IMAP=${WORKSPACE_INSTALL_IMAP}
          - INSTALL_MONGO=${WORKSPACE_INSTALL_MONGO}
          - INSTALL_AMQP=${WORKSPACE_INSTALL_AMQP}
          - INSTALL_CASSANDRA=${WORKSPACE_INSTALL_CASSANDRA}
          - INSTALL_ZMQ=${WORKSPACE_INSTALL_ZMQ}
          - INSTALL_GEARMAN=${WORKSPACE_INSTALL_GEARMAN}
          - INSTALL_PHPREDIS=${WORKSPACE_INSTALL_PHPREDIS}
          - INSTALL_MSSQL=${WORKSPACE_INSTALL_MSSQL}
          - NVM_NODEJS_ORG_MIRROR=${WORKSPACE_NVM_NODEJS_ORG_MIRROR}
          - INSTALL_NODE=${WORKSPACE_INSTALL_NODE}
          - NPM_REGISTRY=${WORKSPACE_NPM_REGISTRY}
          - NPM_FETCH_RETRIES=${WORKSPACE_NPM_FETCH_RETRIES}
          - NPM_FETCH_RETRY_FACTOR=${WORKSPACE_NPM_FETCH_RETRY_FACTOR}
          - NPM_FETCH_RETRY_MINTIMEOUT=${WORKSPACE_NPM_FETCH_RETRY_MINTIMEOUT}
          - NPM_FETCH_RETRY_MAXTIMEOUT=${WORKSPACE_NPM_FETCH_RETRY_MAXTIMEOUT}
          - INSTALL_PNPM=${WORKSPACE_INSTALL_PNPM}
          - INSTALL_YARN=${WORKSPACE_INSTALL_YARN}
          - INSTALL_NPM_GULP=${WORKSPACE_INSTALL_NPM_GULP}
          - INSTALL_NPM_BOWER=${WORKSPACE_INSTALL_NPM_BOWER}
          - INSTALL_NPM_VUE_CLI=${WORKSPACE_INSTALL_NPM_VUE_CLI}
          - INSTALL_NPM_ANGULAR_CLI=${WORKSPACE_INSTALL_NPM_ANGULAR_CLI}
          - INSTALL_NPM_CHECK_UPDATES_CLI=${WORKSPACE_INSTALL_NPM_CHECK_UPDATES_CLI}
          - INSTALL_DRUSH=${WORKSPACE_INSTALL_DRUSH}
          - INSTALL_WP_CLI=${WORKSPACE_INSTALL_WP_CLI}
          - INSTALL_DRUPAL_CONSOLE=${WORKSPACE_INSTALL_DRUPAL_CONSOLE}
          - INSTALL_AEROSPIKE=${WORKSPACE_INSTALL_AEROSPIKE}
          - INSTALL_OCI8=${WORKSPACE_INSTALL_OCI8}
          - INSTALL_V8JS=${WORKSPACE_INSTALL_V8JS}
          - COMPOSER_GLOBAL_INSTALL=${WORKSPACE_COMPOSER_GLOBAL_INSTALL}
          - COMPOSER_VERSION=${WORKSPACE_COMPOSER_VERSION}
          - COMPOSER_AUTH_JSON=${WORKSPACE_COMPOSER_AUTH_JSON}
          - COMPOSER_REPO_PACKAGIST=${WORKSPACE_COMPOSER_REPO_PACKAGIST}
          - INSTALL_WORKSPACE_SSH=${WORKSPACE_INSTALL_WORKSPACE_SSH}
          - INSTALL_LARAVEL_ENVOY=${WORKSPACE_INSTALL_LARAVEL_ENVOY}
          - INSTALL_LARAVEL_INSTALLER=${WORKSPACE_INSTALL_LARAVEL_INSTALLER}
          - INSTALL_XLSWRITER=${WORKSPACE_INSTALL_XLSWRITER}
          - INSTALL_DEPLOYER=${WORKSPACE_INSTALL_DEPLOYER}
          - INSTALL_PRESTISSIMO=${WORKSPACE_INSTALL_PRESTISSIMO}
          - INSTALL_LINUXBREW=${WORKSPACE_INSTALL_LINUXBREW}
          - INSTALL_MC=${WORKSPACE_INSTALL_MC}
          - INSTALL_SYMFONY=${WORKSPACE_INSTALL_SYMFONY}
          - INSTALL_PYTHON=${WORKSPACE_INSTALL_PYTHON}
          - INSTALL_PYTHON3=${WORKSPACE_INSTALL_PYTHON3}
          - INSTALL_IMAGE_OPTIMIZERS=${WORKSPACE_INSTALL_IMAGE_OPTIMIZERS}
          - INSTALL_IMAGEMAGICK=${WORKSPACE_INSTALL_IMAGEMAGICK}
          - INSTALL_TERRAFORM=${WORKSPACE_INSTALL_TERRAFORM}
          - INSTALL_DUSK_DEPS=${WORKSPACE_INSTALL_DUSK_DEPS}
          - INSTALL_PG_CLIENT=${WORKSPACE_INSTALL_PG_CLIENT}
          - PG_CLIENT_VERSION=${POSTGRES_CLIENT_VERSION}
          - INSTALL_PHALCON=${WORKSPACE_INSTALL_PHALCON}
          - INSTALL_SWOOLE=${WORKSPACE_INSTALL_SWOOLE}
          - INSTALL_TAINT=${WORKSPACE_INSTALL_TAINT}
          - INSTALL_LIBPNG=${WORKSPACE_INSTALL_LIBPNG}
          - INSTALL_GRAPHVIZ=${WORKSPACE_INSTALL_GRAPHVIZ}
          - INSTALL_IONCUBE=${WORKSPACE_INSTALL_IONCUBE}
          - INSTALL_APCU=${WORKSPACE_INSTALL_APCU}
          - INSTALL_MYSQL_CLIENT=${WORKSPACE_INSTALL_MYSQL_CLIENT}
          - INSTALL_PING=${WORKSPACE_INSTALL_PING}
          - INSTALL_SSHPASS=${WORKSPACE_INSTALL_SSHPASS}
          - INSTALL_INOTIFY=${WORKSPACE_INSTALL_INOTIFY}
          - INSTALL_FSWATCH=${WORKSPACE_INSTALL_FSWATCH}
          - INSTALL_AST=${WORKSPACE_INSTALL_AST}
          - INSTALL_YAML=${WORKSPACE_INSTALL_YAML}
          - INSTALL_RDKAFKA=${WORKSPACE_INSTALL_RDKAFKA}
          - INSTALL_MAILPARSE=${WORKSPACE_INSTALL_MAILPARSE}
          - INSTALL_GIT_PROMPT=${WORKSPACE_INSTALL_GIT_PROMPT}
          - INSTALL_XMLRPC=${WORKSPACE_INSTALL_XMLRPC}
          - PUID=${WORKSPACE_PUID}
          - PGID=${WORKSPACE_PGID}
          - CHROME_DRIVER_VERSION=${WORKSPACE_CHROME_DRIVER_VERSION}
          - NODE_VERSION=${WORKSPACE_NODE_VERSION}
          - YARN_VERSION=${WORKSPACE_YARN_VERSION}
          - DRUSH_VERSION=${WORKSPACE_DRUSH_VERSION}
          - AST_VERSION=${WORKSPACE_AST_VERSION}
          - IMAGEMAGICK_VERSION=${WORKSPACE_IMAGEMAGICK_VERSION}
          - TZ=${WORKSPACE_TIMEZONE}
          - BLACKFIRE_CLIENT_ID=${BLACKFIRE_CLIENT_ID}
          - BLACKFIRE_CLIENT_TOKEN=${BLACKFIRE_CLIENT_TOKEN}
          - INSTALL_POWERLINE=${WORKSPACE_INSTALL_POWERLINE}
          - INSTALL_SUPERVISOR=${WORKSPACE_INSTALL_SUPERVISOR}
          - INSTALL_FFMPEG=${WORKSPACE_INSTALL_FFMPEG}
          - INSTALL_AUDIOWAVEFORM=${WORKSPACE_INSTALL_AUDIOWAVEFORM}
          - INSTALL_WKHTMLTOPDF=${WORKSPACE_INSTALL_WKHTMLTOPDF}
          - WKHTMLTOPDF_VERSION=${WORKSPACE_WKHTMLTOPDF_VERSION}
          - INSTALL_GNU_PARALLEL=${WORKSPACE_INSTALL_GNU_PARALLEL}
          - INSTALL_LNAV=${WORKSPACE_INSTALL_LNAV}
          - INSTALL_PROTOC=${WORKSPACE_INSTALL_PROTOC}
          - INSTALL_PHPDECIMAL=${WORKSPACE_INSTALL_PHPDECIMAL}
          - INSTALL_ZOOKEEPER=${WORKSPACE_INSTALL_ZOOKEEPER}
          - INSTALL_SSDB=${WORKSPACE_INSTALL_SSDB}
          - INSTALL_TRADER=${WORKSPACE_INSTALL_TRADER}
          - PROTOC_VERSION=${WORKSPACE_PROTOC_VERSION}
          - INSTALL_DOCKER_CLIENT=${WORKSPACE_INSTALL_DOCKER_CLIENT}
          - INSTALL_MEMCACHED=${WORKSPACE_INSTALL_MEMCACHED}
          - INSTALL_EVENT=${WORKSPACE_INSTALL_EVENT}
          - INSTALL_DNSUTILS=${WORKSPACE_INSTALL_DNSUTILS}
          - INSTALL_POPPLER_UTILS=${WORKSPACE_INSTALL_POPPLER_UTILS}
          - INSTALL_JDK=${WORKSPACE_INSTALL_JDK}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - docker-in-docker:/certs/client
        - ./php-worker-7.4/supervisord.d:/etc/supervisord.d
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
      ports:
        - "2224:22"
        # - "${WORKSPACE_BROWSERSYNC_HOST_PORT}:3000"
        # - "${WORKSPACE_BROWSERSYNC_UI_HOST_PORT}:3001"
        # - "${WORKSPACE_VUE_CLI_SERVE_HOST_PORT}:8080"
        # - "${WORKSPACE_VUE_CLI_UI_HOST_PORT}:8000"
        # - "${WORKSPACE_ANGULAR_CLI_SERVE_HOST_PORT}:4200"
        # - "${WORKSPACE_VITE_PORT}:5173"
      tty: true
      environment:
        - PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
        - DOCKER_HOST=tcp://docker-in-docker:2376
        - DOCKER_TLS_VERIFY=1
        - DOCKER_TLS_CERTDIR=/certs
        - DOCKER_CERT_PATH=/certs/client
        - CHOKIDAR_USEPOLLING=true
      networks:
        - frontend
        - backend
      links:
        - docker-in-docker

  ### postgres_tunnel ##################################################
    postgres_tunnel:
      build:
        context: ./docker-ssh-tunnel
      environment:
        - SSH_DEBUG="-v"
        - REMOTE_HOST=localhost
        - REMOTE_PASS=password
        - LOCAL_PORT=9512
        - REMOTE_PORT=9512
        - TUNNEL_HOST=user@hostip -i /root/.ssh/id_rsa
      volumes:
        - $HOME/.ssh:/root/ssh:ro
      networks:
        - frontend
        - backend
      links:
        - docker-in-docker


Solution

  • I found the problem. For some reason, database config doesn't accept read/write keys or works different. I change database connections config like bellow, comment read, write lines and add host:

    return [
        
        'connections' => [
            'remotedb' => [
                'host' => env('DB_OLD_HOST', '127.0.0.1'),
    //            'read' => [
    //                'host' => env('DB_OLD_HOST', '127.0.0.1'),
    //            ],
    //            'write' => [],
                'driver' => 'pgsql',
                'url' => env('DATABASE_URL'),
                'port' => env('DB_OLD_PORT', '5432'),
                'database' => env('DB_OLD_DATABASE', 'database'),
                'username' => env('DB_OLD_USERNAME', 'root'),
                'password' => env('DB_OLD_PASSWORD', ''),
                'charset' => 'utf8',
                'prefix' => '',
                'prefix_indexes' => true,
                'schema' => 'public',
                'sslmode' => env('DB_SSLMODE', 'prefer'),
            ],
        ],
    ];