Running Tinker on a production Laravel app in Docker
How to open Tinker in a Laravel container on a remote server, the quoting and permission traps on the way, and how to try a write without keeping it.
Sometimes the quickest way to answer a question about production is to ask the app itself: how many orders failed today, what a user’s settings look like, whether a config value is what you think it is. Tinker is made for that. With the app in Docker on a remote server, it takes a few hops to get there.
The manual way
Connect to the server, find the container, and start Tinker inside it:
ssh deploy@shop.example.comdocker ps --format '{{.Names}}'docker exec -it shop-app-1 php artisan tinkerA few things to know on the way.
-it is not optional. Tinker is an interactive shell (PsySH). Without -i it has no input to read and without -t no terminal to draw on.
The container name may change on every deploy. With plain Docker Compose it stays shop-app-1. With Coolify, Dokploy or Swarm the name carries a hash or the time of the deploy, so you look it up again each time. Filtering by a label saves the guessing:
docker ps --filter label=com.docker.compose.service=app --format '{{.Names}}'Run it as the user the app runs as. docker exec runs as the image’s default user, often root. If Tinker writes a log line or a cache file as root, PHP-FPM, running as www-data, may no longer be able to write to it, and the app starts failing with “Permission denied” on storage/logs. Pass the user when the image runs PHP as someone else:
docker exec -it -u www-data shop-app-1 php artisan tinkerOne-liners need careful quoting. tinker --execute runs code without the interactive shell, which is handy from a script. But the code then passes through your local shell, SSH and the shell on the server, and namespaces are full of backslashes:
ssh deploy@shop.example.com "docker exec shop-app-1 php artisan tinker --execute='echo App\\Models\\Order::where(\"status\", \"failed\")->count();'"It works, and it is easy to get wrong. The code also shows up in the server’s process list while it runs.
History does not survive a deploy. PsySH keeps its history in the container’s home directory, so the snippet you wrote last week went away with the old container.
Careful with production data
Tinker runs with the app’s full access to the database. A few habits keep it safe:
-
Count before you change. Run the query with
->count()or->get(['id'])first, and only then with->update()or->delete(). -
Try a write inside a transaction. You see the result and keep nothing:
DB::beginTransaction();$order = Order::find(50117);$order->markAsPaid();$order->fresh()->status;DB::rollBack(); -
Mind the memory.
Order::all()on a big table loads every row. Use->take(10),->count()or->cursor()instead. -
Check where you are.
app()->environment()andconfig('database.default')tell you which app and which database you are talking to.
With laraport
laraport opens Tinker in the booted app of the container you are looking at: pick the server, the container, press 2 for the Laravel tab, then Tinker.
▲ laraport › Production › shop-app-1 › Tinker ● ssh shop.example.com Tinker · shop-app-1 PRODUCTION 38 ms · 41.5 MB 1 User::where('plan', 'pro')->count() result = 1284 LARAPORT Production shop-app-1 ctrl+r/f5 run ctrl+p/n history pgup/pgdn scroll output
You write the code in the editor on top and run it with ctrl+r. Below it you get whatever the code printed, the value of the last expression, or the exception with its location, and the time and memory the run took. No return needed, and use statements work, since the code runs through PsySH as artisan tinker does.
What the manual way makes you think about is handled for you:
- No container name to look up. Containers are listed by their Compose service, Coolify resource or Dokploy app, and stay the same across deploys.
- No quoting. The code travels over stdin, base64-encoded, so quotes and backslashes never matter, and it never shows up in a process list on the server.
- History on your computer.
ctrl+pandctrl+ngo through your earlier snippets, kept per server and container in~/.config/laraport/history.json, so they outlive the container. - A pause before production. In an app with
APP_ENV=production, the first run asks you to confirm. Every run goes into the journal, and a server marked read-only refuses Tinker altogether.
More in the Tinker docs.