Saving Command History in Containers

Save the history of tinker commands you've run within a container.

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

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.

https://www.youtube.com/watch?v=L1BDM1oBRJ8

Sign up to get announcements as well as
over 20 free videos on Docker in Development,
a course preview on Dockerizing Your Applications, and more!

What to learn more? Read the blog.

About

the true grumpy programmer, don't be fooled by imitation

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!