News & views

VS Code, tmux, docker

Written by Steve Pike | Mar 31, 2020

At Demand Logic we use Docker containers. This allows us to have a development environment that closely mirrors our production environment, and a production environment that is agile and flexible. All of us in the tech team use them to a greater or lesser extent in our various responsibilities when crafting the platform, however, what varies between us quite a bit is our choice of dev setup around the containers. Some use pycharm, others vim, I don't think we have an emacs user (unless they're just keeping it to themselves?!) I have been using vim since 2010, and along with tmux this has served me pretty well for 10 years, but vim can be a bit slow and occasionally I just miss certain aspects of interactivity which I'm sure exist but elude me.

I (finally?) moved away from tmux in my terminal with different windows for vim, docker-compose up and docker-compose exec (for running tests) to VS Code. Obviously I kept the vim key bindings in VS Code... and I also wanted to keep tmux in the terminal pane of VS Code, because it is an old friend and much more flexible than VS Code's own terminal options (for example, it will persist after the workspace is closed).

I found a little how-to here and after a bit of faff I managed to adapt the script for my setup; namely I want one tmux window open in the workspace root, and another window with a split, running the test stack on the left and a shell on the test container on the right. We have separate docker-compose stack files for each repo, and we have a script that generates these stacks which (by convention) names them $REPO_NAME-test/stack.yml. Ensuring that the VS Code workspace is in the repo root, it all goes pretty smoothly.

The contents of the file in the setting for terminal.integrated.shell.osx is:

#!/bin/zsh
SESSION="vscode`pwd | md5`"
PWD=`pwd`
REPO=`basename "${PWD}"`
STACK="/Users/spike/Code/DL/conduit/stacks/dl/${REPO}-test/"

tmux attach-session -d -t $SESSION || tmux new-session -s $SESSION -c ${PWD} \; \
new-window -c ${STACK} "docker-compose -f stack.yml up" \; \
split-window -c ${STACK} "sleep 5 && docker-compose -f stack.yml exec ${REPO}_test entrypoint env" \; \
select-layout even-horizontal

Et voila! It's nice to be keeping the familiar key patterns and fast aspects of using vim and tmux with the added benefits that VS Code brings :)