Skip to content

Instantly share code, notes, and snippets.

@nilsmartel
Created April 14, 2022 11:03
Show Gist options
  • Save nilsmartel/cd543d501845fda7e885734f2194ec07 to your computer and use it in GitHub Desktop.
Save nilsmartel/cd543d501845fda7e885734f2194ec07 to your computer and use it in GitHub Desktop.
Mount current directory inside an docker container and continue working inside of linux
#!/usr/local/bin/python3
import os
import sys
# some basic argument parsing, so that ports, entrycommand and image can be specified
def args():
image = 'node'
cmd = 'bash'
ports = []
for arg in sys.argv[1:]:
if arg == "help" or arg == "--help" or arg == "-h":
print("""indocker <option>*
option
image=<IMAGE NAME> (default node)
cmd=<ENTRY COMMAND> (default bash)
<PORT>
example:
indocker image=node cmd=bash 3000 8000 8081
"""
)
exit()
if arg.startswith("image="):
image = arg.replace("image=", "")
if arg.startswith("cmd="):
cmd = arg.replace("cmd=", "")
ports.append(arg)
return (image, cmd, ports)
image, cmd, ports = args()
# we want to run a command similar to this
# docker run -ti -p 3000 -v $(pwd):/$(basename $(pwd)) node bash
portConfig = " ".join(map(lambda n: "-p "+n, ports))
cwd = os.getcwd()
basename = cwd.split("/").pop()
shellCommand = "docker run -ti " + portConfig + f" -v {cwd}:/{basename} {image} {cmd}"
print(shellCommand)
os.system(shellCommand)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment