Skip to content

Instantly share code, notes, and snippets.

@ahoffer
Last active August 19, 2021 15:34
Show Gist options
  • Save ahoffer/7862114dae5061a9702b3bc93899b8d1 to your computer and use it in GitHub Desktop.
Save ahoffer/7862114dae5061a9702b3bc93899b8d1 to your computer and use it in GitHub Desktop.
Python functions and programs for docker utilities
#!/usr/bin/env python3
# Tail the logs of a container, given the partial name of the container
import sys
from dockerutil import execute_docker
args = ' '.join(sys.argv[2:])
cmd = f'docker logs -f {args} $name'
execute_docker(cmd, sys.argv[1])
import subprocess
from os import system
from sys import exit
from string import Template
# Return a dictionary given a string.
# If the string is contained in the name of a docker containers, return the container name and ID.
# Otherwise, return an error flag and description.
# WARNING: Very naive. Doesn't account for headers or string that show up in other places than in the docker ps command.
def grepcontainer(input):
output = subprocess.run(["docker", "ps"], stdout=subprocess.PIPE, text=True)
lines= output.stdout.split("\n")
matches = [x for x in lines if input in x]
returnObject = {"error" : False}
if not matches:
print()
returnObject["error"] = True
returnObject["message"] = f'Nothing matches {input}'
else:
firstMatch = matches[0]
containerInfo = firstMatch.split(' ')
returnObject["id"] =containerInfo[0]
returnObject["name"] = containerInfo[-1]
return returnObject
# Execute a command for a docker container.
# This function does some verification of the inputs.
def execute_docker(cmd, container):
if not container:
print("The (partial) name of the docker container is missing. Please try again.")
exit(-1)
output = grepcontainer(container)
if output["error"]:
print(output["message"])
exit(-2)
completedCmd = Template(cmd).substitute(name = output["id"])
system(completedCmd)
#!/usr/bin/env python3
# Open a shell in a container, given the partial name of the container
import sys
from dockerutil import execute_docker
args = ' '.join(sys.argv[2:])
cmd = f'docker exec -it {args} $name bash'
execute_docker(cmd, sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment