Working with Docker containers with the dexec bash script
spin.atomicobject.com
spin.atomicobject.com
#!/bin/bash
# Grab the container name and use fzf to let us pick it
result=$(docker container ls --format "{{.Names}}" | fzf)
# No command - try bash
if [ $# -eq 0 ]
then
echo "docker exec -it $result /bin/bash"
docker exec -it "$result" /bin/bash
fi
# Command was given so use that
if [ $# -eq 1 ]
then
echo "docker exec -it $result $1"
docker exec -it "$result" "$1"
fi
Requires fzf but other wise `ec, select container, it'll try bash or whatever shell/command you supplied.Like if you always use [[ and ]] you are going to be surprised when the default shell used for running unattended scripts in Debian, Ubuntu refuses to run your script.
dash: 1: [[: not found
I know your parent commenter mentioned "#!/bin/bash" so your point is still valid. But I recommend that always use "#!/bin/sh" and always use [ and ] so that the scripts are portable.I say this as a former package maintainer who has spent a great deal of time converting Bash scripts to POSIX sh scripts just so that I can package them as .deb.
so if someone plans to extend the script so that it injects it iself into the container or what not.
docker exec -it "$result" "${1:-/bin/bash}"
This is why I usually stop myself from trying anything very complex with shell scripts: unlike scripting languages, half the power of shell is hiding inside little piles of punctuation marks that I don’t think I can even Google!
It's why I avoid using stuff like that because I won't remember how it works in 6mths.
In particular what I've found useful:
- automatically create a user with the same uid as yourself and run the command as that user. Pretty much required whenever you mount some directories for writing.
- automatically login and pull if necessaryHere's a few project specific examples. They all have similar run scripts:
- https://github.com/nickjj/docker-flask-example
- https://github.com/nickjj/docker-rails-example
- https://github.com/nickjj/docker-django-example
- https://github.com/nickjj/docker-node-example
- https://github.com/nickjj/docker-phoenix-exampleI use "just" these days, per project, autocompletion with explanation what each command does.
alias dps='docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"'
alias dcup='docker compose up -d'
alias dcupf='docker compose up -d && docker compose --follow logs'
alias dcdown='docker compose down'
alias docker-compose='docker compose' docker_names() { docker ps | awk '{print $NF}'; }[1] https://ddev.readthedocs.io/en/stable/users/extend/custom-co...
[2] https://ddev.com
For similar docker automation I just use a Makefile with pattern rule (e.g. `docker-sh-%`). This is much more flexible than shell scripts because recipes can be easily remixed to provide higher-level functionality.
docker exec -it <container> <command>I recently fixed one of my biggest pet peeves with docker swarm - the inability to directly exec into a service without first SSHing to the host the task is running on.
https://github.com/neuroforgede/docker-swarm-proxy
Maybe your issue is in this ballpark? Happy to exchange notes on this. If you are looking for a community of Swarm users, check out https://devops.fan (that's a discord hosted by Bret Fisher)
I formalized it into a .notes folder inside every project and I have a command with useful note related tasks, that works out of the present directory .notes folder.
I have the same for project tasks, so common bash tasks go into a script inside .tasks and I have a command runner that works similar to npm run.
Sure, they better made -it the default, so we don't have to write it.