Save the history of tinker commands you've run within a container.
We cover two ways of persisting data in containers:
tinker command uses Psysh for a PHP REPL.
When we run
artisan tinker, command history is saved in
It does this using the
HOME environment variable to find the `.config/psysh` directory:
We can save history in one of two ways with Docker.
docker run -it \ -e "HOME=/home" \ -v ~/.dckrconfig:/home/.config \ -v $(pwd):/opt \ -w /opt \ php:7.0-cli \ php artisan tinker
Once tinker is started, we can run some commands, then stop the container. Since we didn't use the
--rm flag, the stopped container will still exist.
$ docker ps -a CONTAINER ID IMAGE COMMAND STATUS NAMES 6e9c39b313ed php:7.0-cli "php artisan tinker" Exited (0) high_visvesvaraya $ docker start -i 6e9c39b313ed
The container was not destroyed when it was stopped! Any change made inside the container is persisted. We'll still have the tinker command history available if we restart the container!
Alternatively, we can still destroy container when we're done with them, but save history locally via a volume mount.
The following sets the
$HOME environment variable that Psysh uses to find/create a
$HOME/.config/psysh/psysh_history file. On my Macintosh (host system), I create a directory for the container to use to write to the Psysh history to, and then setup the container in a way that Psysh can find/write to that shared volume.
# Create local directory mkdir ~/.dckrconfig # Head to my-app cd ~/Sites/docker/my-app # Run a container docker run --rm -it \ -e "HOME=/home" \ -v ~/.psyhistory:/home/.config \ -v $(pwd):/opt \ -w /opt \ php:7.0-cli \ php artisan tinker
This container is destroyed when it is stopped, but since we persisted the Psysh history on a local volume, we can re-use it on future containers by sharing the same directory. We can re-use the exact same command to create a new container anytime - that container will have the previous history as it can read the
psysh_history file saved.
I'm Chris Fidao. I wrote the Servers for Hackers eBook, and publish related videos at the Servers for Hackers video site. I also curate the Servers for Hackers newsletter, which is read by over 12,000 programmers who want to learn about servers.
I'm also the author of the Deploy PHP! video series, a video series about automating your deployments. A few years ago, I wrote Implementing Laravel, a book all about coding in Laravel - now offered free!
A Servers for Hackers project.
© 2017 Fideloper LLC
Docker and the Docker logo are trademarks or registered trademarks of Docker, Inc. in the United States and/or other countries. Docker, Inc. and other parties may also have trademark rights in other terms used herein.