We cover two ways of persisting data in containers:

Re-using a single container

Sharing saved data amonst many containers

The Tinker Command

Laravel's tinker command uses Psysh for a PHP REPL.

When we run artisan tinker , command history is saved in ~/.config/psysh/psysh_history .

It does this using the HOME environment variable to find the `.config/psysh` directory: $HOME/.config/psysh

We can save history in one of two ways with Docker.

Re-use a container

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!

Volumes to Save History

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