Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ramm committed Feb 11, 2019
1 parent f8e544e commit 0ae737a
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 0 deletions.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Author: MartinRamm

This is something I created to increase my productivity with docker/docker-compose.
Feel free to create a PR with improvements - but please keep this documentation up to date!

# Requirements

1. `docker` / `docker-compose` must be accessible to non-root users
1. `fzf` (https://github.com/junegunn/fzf#installation)

# Installation instructions

1. Clone this repository: `git clone https://github.com/MartinRamm/fzf-docker.git`
1. Add to your `.zshrc` or `.bashrc` file this command: `source /path/to/docker-fzf`

# Overview of available commands

| command | description | fzf mode | command arguments (optional) |
| ------- | --------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------ |
| dr | docker restart && open logs (in follow mode) | single | |
| dl | docker logs (in follow mode) | single | time interval - e.g.: `1m` for 1 minute - (defaults to all available logs) |
| de | docker exec | single | command to exec (default `/bin/sh`) |
| ds | docker stop | multiple | |
| dsa | docker stop all running containers | | |
| dk | docker kill and remove | multiple | |
| dka | docker kill and remove all containers | | |
| drmi | docker remove image (with force) | multiple | |
| drmia | docker remove all images (with force) | | |
| dclean | `dka` and `drmia` | | |
| dcu | docker-compose up (in detached mode) | multiple | path to docker-compose file (defaults to recursive search for `docker-compose.yml` or `docker-compose.yaml`) |
| dcua | docker-compose up all services (in detached mode) | multiple | path to docker-compose file (defaults to recursive search for `docker-compose.yml` or `docker-compose.yaml`) |

# Learning by doing
### fzf mode = single
The image below shows a user opening the logs of the `infrastructure_php_1_6714fd704177` container with the `dl` command.
The command `dl` was entered into a terminal. The user now typed `php` to narrow the search for containers that contain that phrase. When the correct container was selected, the user pressed `Enter`.
Alternatively, the user could have used the arrow keys to select the correct container id.

![example gif](single.gif)

### optional command arguments
This image does the same as above, but with `10m` as an argument. Therefore the command will only show the logs the selected container produced in the last 10 minutes.

![example gif](args.gif)

### fzf mode = multiple

The image below shows a user starting the container `whiteboard` and `redis` with the `dcu` command.
To mark a containers/services in fzf, press on the `tab` key. To deselect, press `shift + tab`.
To remove the input, press `Alt + Backspace`.
Finally, press `Enter` to start the command. Note that when pressing `Enter`, the selected item will *not* be added automatically to your selection.
If you only want to mark one container, you don't have to select it with tab - you can follow the instuctions of fzf mode single.

![example gif](multiple.gif)

# License
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
Binary file added args.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 154 additions & 0 deletions docker-fzf
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env bash
A
__docker_pre_test() {
if [[ $(docker ps --format '{{.Names}}') ]]; then
return 0;
fi
echo "No docker images running";
return 1;
}

__docker_compose_pre_test() {
if [ -z "$1" ]; then
if [[ $(docker-compose config --services) ]]; then
return 0;
fi
echo "No docker-compose.yml found (or it contains errors). You can pass as the first argument a path to the service declaration file."
return 1;
fi

if [[ $(docker-compose --file $1 config --services) ]]; then
return 0;
fi
echo "Invalid service declaration file $1."
return 1;
}

#docker restart
dr() {
if __docker_pre_test; then
local container=$(docker ps -a --format '{{.Names}}' | fzf)
if [ ! -z "$container" ]; then
docker restart $container
docker logs -f $container --since 1s
fi
fi
}

#docker logs
dl() {
if __docker_pre_test; then
local since=""
if [ ! -z "$1" ]; then
since="--since $1"
fi

local container=$(docker ps -a --format '{{.Names}}' | fzf)
if [ ! -z "$container" ]; then
sh -c "docker logs -f $container $since"
fi
fi
}

#docker exec
de() {
if __docker_pre_test; then
local name=$(docker ps --format '{{.Names}}' | fzf)
if [ ! -z "$name" ]; then
docker exec -it $name ${1:/bin/sh}
fi
fi
}

#docker stop
ds() {
if __docker_pre_test; then
local names=$(docker ps --format '{{.Names}}' | fzf -m --print0)
if [ ! -z "$names" ]; then
docker update --restart=no $names
docker stop $names
fi
fi
}

#docker stop all
dsa() {
if __docker_pre_test; then
docker update --restart=no $(docker ps -q)
docker stop $(docker ps -q)
fi
}

#docker kill and remove
dk() {
if __docker_pre_test; then
local names=$(docker ps --format '{{.Names}}' | fzf -m --print0)
if [ ! -z "$names" ]; then
docker update --restart=no $names
docker kill $names
docker rm $names -f
fi
fi
}

#docker kill an remove all
dka() {
if __docker_pre_test; then
docker update --restart=no $(docker ps -q)
docker kill $(docker ps -q)
fi
if [[ $(docker ps -aq) ]]; then
docker rm $(docker ps -aq) -f
fi
}

#docker remove image
drmi() {
docker images --format "{{.Repository}}:{{.Tag}}" --filter "dangling=false" |
fzf -m |
while read -r ref; do
local id=$(docker images --filter "reference=$ref" --format "{{.ID}}")
docker rmi $id -f
done
}

#docker remove all images
drmia() {
if [[ $(docker images -qa) ]]; then
docker rmi $(docker images -qa) -f
fi
}

#docker clean
dclean() {
dka
drmia
}

#docker compose up
dcu() {
if __docker_compose_pre_test $1; then
local fileref=""
if [ ! -z "$1" ]; then
fileref="--file $1"
fi

local service=$(sh -c "docker-compose $fileref config --services" | fzf -m --print0)
if [ -z "$service" ]; then
return 1;
fi
sh -c "docker-compose $fileref up -d $service"
fi
}

#docker compose up all
dcua() {
if __docker_compose_pre_test $1; then
local fileref=""
if [ ! -z "$1" ]; then
fileref="--file $1"
fi

sh -c "docker-compose $fileref up -d"
fi
}
Binary file added multiple.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added single.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ae737a

Please sign in to comment.