Laravel php artisan config:cache Fails with Redis DNS Error During Build on Railway

linwenhao5FREE

12 days ago

Hi everyone,

I deployed a Laravel backend on Railway and I’m using a build-app.sh script similar to the official one:

# Clear cache

php artisan optimize:clear

# Cache the various components of the Laravel application

php artisan config:cache

php artisan event:cache

php artisan route:cache

php artisan view:cache

However, when running the build, I get this error:

php_network_getaddresses: getaddrinfo for redis.railway.internal failed: Name or service not known

✕ [11/12] RUN chmod +x ./build-app.sh && sh ./build-app.sh

process "/bin/bash -ol pipefail -c chmod +x ./build-app.sh && sh ./build-app.sh" did not complete successfully: exit code: 1

Strangely, if I skip all php artisan commands, the build works fine. After deployment, the app can successfully connect to both the database and Redis services.

My .env includes:

APP_URL="http://takeaway_app_server.railway.internal"

DB_URL="${{MySQL.MYSQL_URL}}"

REDIS_URL="${{Redis.REDIS_URL}}"

Could this be related to Railway’s private networking or environment variables not being fully available during the build phase? How can I properly run these cache commands during build without this error?

Thanks for any help!

Solved$10 Bounty

2 Replies

12 days ago

This happens because during the build phase on Railway, your app container can’t actually reach the internal Redis hostname (redis.railway.internal). Private networking and service discovery only work once the app is running, not during the build step. When you run php artisan config:cache (or other artisan commands) during build, Laravel tries to resolve and connect to Redis right away, but the DNS name isn’t available yet—so you get the “getaddrinfo failed” error.

Here's how to fix it: Don’t run those artisan cache commands in your build script. Instead, move them to your app’s start command or an entrypoint script that runs after the container starts up. That way, the environment variables and private networking are fully available, and Laravel can connect to Redis and the database. So, your build script should just install dependencies and prep assets. Then, in your start command (or a script that runs on container boot), add:

php artisan optimize:clear
php artisan config:cache
php artisan event:cache
php artisan route:cache
php artisan view:cache

This way, caching happens when the app is actually able to reach Redis, and you won’t see the DNS error during build.


testuser123

This happens because during the build phase on Railway, your app container can’t actually reach the internal Redis hostname (redis.railway.internal). Private networking and service discovery only work once the app is running, not during the build step. When you run php artisan config:cache (or other artisan commands) during build, Laravel tries to resolve and connect to Redis right away, but the DNS name isn’t available yet—so you get the “getaddrinfo failed” error.Here's how to fix it: Don’t run those artisan cache commands in your build script. Instead, move them to your app’s start command or an entrypoint script that runs after the container starts up. That way, the environment variables and private networking are fully available, and Laravel can connect to Redis and the database. So, your build script should just install dependencies and prep assets. Then, in your start command (or a script that runs on container boot), add:php artisan optimize:clearphp artisan config:cachephp artisan event:cachephp artisan route:cachephp artisan view:cacheThis way, caching happens when the app is actually able to reach Redis, and you won’t see the DNS error during build.

linwenhao5FREE

11 days ago

Thank you so much, that fix worked!


Status changed to Solved chandrika 11 days ago