Skip to content

Instantly share code, notes, and snippets.

@omaraboumrad
Created October 6, 2017 14:18
Show Gist options
  • Save omaraboumrad/7fb7c60038591ee65961b85e7f8d2096 to your computer and use it in GitHub Desktop.
Save omaraboumrad/7fb7c60038591ee65961b85e7f8d2096 to your computer and use it in GitHub Desktop.
View docker-compose services logs in split tmux panes
# Requires pyyaml
import os
import yaml
run = os.system
new_window = lambda cmd: run('tmux new-window -n "logs" "{}"'.format(cmd))
split_vertical = lambda cmd: run('tmux split-window "{}"'.format(cmd))
split_horizontal = lambda cmd: run('tmux split-window -h "{}"'.format(cmd))
even_vertical = lambda: run('tmux select-layout even-vertical')
if __name__ == '__main__':
try:
config_text = open('docker-compose.yaml').read()
except IOError:
config_text = open('docker-compose.yml').read()
config = yaml.load(config_text)
services = config['services'].keys()
new_window('docker-compose logs -f {}'.format(services[0]))
for service in services[1:]:
split_horizontal('docker-compose logs -f {}'.format(service))
even_vertical()
@pletnes
Copy link

pletnes commented Apr 2, 2019

This works great for me! One minor issue - to run on python 3.7, line 20 should be:

    services = list(config['services'].keys())

@saintaardvark
Copy link

To get around this warning:

/shell.py:18: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, 
as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  config = yaml.load(config_text)

...you need to change line 18 to:

    config = yaml.load(config_text, Loader=yaml.SafeLoader)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment