Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mcgstrategic's avatar

Issue with session saving in database with containerized app

I am experienceing an issue in a production site hosted with a Docker container (Runcloud Containerized app). I am tracking session in my Postgres database which is NOT in the container, it is on the host server. On dev (no container) I have no issue, but on prod, the app does not store the session. If I switch to SESSION_DRIVER=file, the session is saved. I assume I need some additional configuration in pg_hba.conf to get this working. Has anyone had and solved this issue? Thank you!!

0 likes
5 replies
LaryAI's avatar
Level 58

It sounds like your containerized Laravel app is unable to connect to the external Postgres server to store sessions. Since switching to SESSION_DRIVER=file works, the core app is fine—it's specifically the database connectivity for session storage that's the problem.

Here’s a step-by-step checklist and some common solutions:

1. Check Your .env Configuration

Ensure your .env has the correct connection details for Postgres:

SESSION_DRIVER=database
DB_CONNECTION=pgsql
DB_HOST=<host_ip_or_name>
DB_PORT=5432
DB_DATABASE=<your_db>
DB_USERNAME=<your_user>
DB_PASSWORD=<your_password>
  • Make sure <host_ip_or_name> isn’t localhost (inside Docker, localhost refers to the container itself).
  • Use the host machine’s IP or a Docker network name if applicable.

2. Enable Remote Connections in postgresql.conf

Open your postgresql.conf and set:

listen_addresses = '*'

This allows Postgres to accept connections from non-local addresses.

3. Configure pg_hba.conf Properly

You need to allow connections from the container’s IP/network. Add a line like:

host    your_db    your_user    0.0.0.0/0    md5

Or, to be more secure, limit to your Docker bridge network IP range (often something like 172.17.0.0/16):

host    your_db    your_user    172.17.0.0/16    md5

After this, reload/restart Postgres.

4. Ensure Network Accessibility

  • Make sure the Postgres port (5432) is open on your host’s firewall.
  • From inside the container, test connectivity with:
psql -h <host_ip_or_name> -U <your_user> <your_db>

Install psql in the container if needed.

5. Rebuild Migration

Ensure your sessions table exists in your database:

php artisan session:table
php artisan migrate

6. Check Laravel Logs

Look in storage/logs/laravel.log for any connection errors—they’ll indicate if authentication, host, or network problems exist.


Summary:
It’s almost always a networking or Postgres configuration problem when this occurs in a containerized setup. Double-check the .env variables, update pg_hba.conf for external connections (based on your docker network), and confirm your firewall rules.

If possible, post any error message from Laravel logs or Postgres logs if you’re still stuck!

ian_h's avatar

You should be able to set your DB_HOST in your app's .env to host.docker.internal which points to the host system itself.

mcgstrategic's avatar

Thanks! That's all fine.... all normal app data connecton works, it's just session that is not storing.

jlrdw's avatar

You did correct migration for session table?

Please or to participate in this conversation.