Skip to content

Commit 0ae737a

Browse files
author
Martin Ramm
committed
Initial version
1 parent f8e544e commit 0ae737a

5 files changed

Lines changed: 234 additions & 0 deletions

File tree

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Author: MartinRamm
2+
3+
This is something I created to increase my productivity with docker/docker-compose.
4+
Feel free to create a PR with improvements - but please keep this documentation up to date!
5+
6+
# Requirements
7+
8+
1. `docker` / `docker-compose` must be accessible to non-root users
9+
1. `fzf` (https://github.com/junegunn/fzf#installation)
10+
11+
# Installation instructions
12+
13+
1. Clone this repository: `git clone https://github.com/MartinRamm/fzf-docker.git`
14+
1. Add to your `.zshrc` or `.bashrc` file this command: `source /path/to/docker-fzf`
15+
16+
# Overview of available commands
17+
18+
| command | description | fzf mode | command arguments (optional) |
19+
| ------- | --------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------ |
20+
| dr | docker restart && open logs (in follow mode) | single | |
21+
| dl | docker logs (in follow mode) | single | time interval - e.g.: `1m` for 1 minute - (defaults to all available logs) |
22+
| de | docker exec | single | command to exec (default `/bin/sh`) |
23+
| ds | docker stop | multiple | |
24+
| dsa | docker stop all running containers | | |
25+
| dk | docker kill and remove | multiple | |
26+
| dka | docker kill and remove all containers | | |
27+
| drmi | docker remove image (with force) | multiple | |
28+
| drmia | docker remove all images (with force) | | |
29+
| dclean | `dka` and `drmia` | | |
30+
| 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`) |
31+
| 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`) |
32+
33+
# Learning by doing
34+
### fzf mode = single
35+
The image below shows a user opening the logs of the `infrastructure_php_1_6714fd704177` container with the `dl` command.
36+
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`.
37+
Alternatively, the user could have used the arrow keys to select the correct container id.
38+
39+
![example gif](single.gif)
40+
41+
### optional command arguments
42+
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.
43+
44+
![example gif](args.gif)
45+
46+
### fzf mode = multiple
47+
48+
The image below shows a user starting the container `whiteboard` and `redis` with the `dcu` command.
49+
To mark a containers/services in fzf, press on the `tab` key. To deselect, press `shift + tab`.
50+
To remove the input, press `Alt + Backspace`.
51+
Finally, press `Enter` to start the command. Note that when pressing `Enter`, the selected item will *not* be added automatically to your selection.
52+
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.
53+
54+
![example gif](multiple.gif)
55+
56+
# License
57+
This is free and unencumbered software released into the public domain.
58+
59+
Anyone is free to copy, modify, publish, use, compile, sell, or
60+
distribute this software, either in source code form or as a compiled
61+
binary, for any purpose, commercial or non-commercial, and by any
62+
means.
63+
64+
In jurisdictions that recognize copyright laws, the author or authors
65+
of this software dedicate any and all copyright interest in the
66+
software to the public domain. We make this dedication for the benefit
67+
of the public at large and to the detriment of our heirs and
68+
successors. We intend this dedication to be an overt act of
69+
relinquishment in perpetuity of all present and future rights to this
70+
software under copyright law.
71+
72+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
73+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
75+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
76+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
77+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
78+
OTHER DEALINGS IN THE SOFTWARE.
79+
80+
For more information, please refer to <http://unlicense.org>

args.gif

82.3 KB
Loading

docker-fzf

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#!/usr/bin/env bash
2+
A
3+
__docker_pre_test() {
4+
if [[ $(docker ps --format '{{.Names}}') ]]; then
5+
return 0;
6+
fi
7+
echo "No docker images running";
8+
return 1;
9+
}
10+
11+
__docker_compose_pre_test() {
12+
if [ -z "$1" ]; then
13+
if [[ $(docker-compose config --services) ]]; then
14+
return 0;
15+
fi
16+
echo "No docker-compose.yml found (or it contains errors). You can pass as the first argument a path to the service declaration file."
17+
return 1;
18+
fi
19+
20+
if [[ $(docker-compose --file $1 config --services) ]]; then
21+
return 0;
22+
fi
23+
echo "Invalid service declaration file $1."
24+
return 1;
25+
}
26+
27+
#docker restart
28+
dr() {
29+
if __docker_pre_test; then
30+
local container=$(docker ps -a --format '{{.Names}}' | fzf)
31+
if [ ! -z "$container" ]; then
32+
docker restart $container
33+
docker logs -f $container --since 1s
34+
fi
35+
fi
36+
}
37+
38+
#docker logs
39+
dl() {
40+
if __docker_pre_test; then
41+
local since=""
42+
if [ ! -z "$1" ]; then
43+
since="--since $1"
44+
fi
45+
46+
local container=$(docker ps -a --format '{{.Names}}' | fzf)
47+
if [ ! -z "$container" ]; then
48+
sh -c "docker logs -f $container $since"
49+
fi
50+
fi
51+
}
52+
53+
#docker exec
54+
de() {
55+
if __docker_pre_test; then
56+
local name=$(docker ps --format '{{.Names}}' | fzf)
57+
if [ ! -z "$name" ]; then
58+
docker exec -it $name ${1:/bin/sh}
59+
fi
60+
fi
61+
}
62+
63+
#docker stop
64+
ds() {
65+
if __docker_pre_test; then
66+
local names=$(docker ps --format '{{.Names}}' | fzf -m --print0)
67+
if [ ! -z "$names" ]; then
68+
docker update --restart=no $names
69+
docker stop $names
70+
fi
71+
fi
72+
}
73+
74+
#docker stop all
75+
dsa() {
76+
if __docker_pre_test; then
77+
docker update --restart=no $(docker ps -q)
78+
docker stop $(docker ps -q)
79+
fi
80+
}
81+
82+
#docker kill and remove
83+
dk() {
84+
if __docker_pre_test; then
85+
local names=$(docker ps --format '{{.Names}}' | fzf -m --print0)
86+
if [ ! -z "$names" ]; then
87+
docker update --restart=no $names
88+
docker kill $names
89+
docker rm $names -f
90+
fi
91+
fi
92+
}
93+
94+
#docker kill an remove all
95+
dka() {
96+
if __docker_pre_test; then
97+
docker update --restart=no $(docker ps -q)
98+
docker kill $(docker ps -q)
99+
fi
100+
if [[ $(docker ps -aq) ]]; then
101+
docker rm $(docker ps -aq) -f
102+
fi
103+
}
104+
105+
#docker remove image
106+
drmi() {
107+
docker images --format "{{.Repository}}:{{.Tag}}" --filter "dangling=false" |
108+
fzf -m |
109+
while read -r ref; do
110+
local id=$(docker images --filter "reference=$ref" --format "{{.ID}}")
111+
docker rmi $id -f
112+
done
113+
}
114+
115+
#docker remove all images
116+
drmia() {
117+
if [[ $(docker images -qa) ]]; then
118+
docker rmi $(docker images -qa) -f
119+
fi
120+
}
121+
122+
#docker clean
123+
dclean() {
124+
dka
125+
drmia
126+
}
127+
128+
#docker compose up
129+
dcu() {
130+
if __docker_compose_pre_test $1; then
131+
local fileref=""
132+
if [ ! -z "$1" ]; then
133+
fileref="--file $1"
134+
fi
135+
136+
local service=$(sh -c "docker-compose $fileref config --services" | fzf -m --print0)
137+
if [ -z "$service" ]; then
138+
return 1;
139+
fi
140+
sh -c "docker-compose $fileref up -d $service"
141+
fi
142+
}
143+
144+
#docker compose up all
145+
dcua() {
146+
if __docker_compose_pre_test $1; then
147+
local fileref=""
148+
if [ ! -z "$1" ]; then
149+
fileref="--file $1"
150+
fi
151+
152+
sh -c "docker-compose $fileref up -d"
153+
fi
154+
}

multiple.gif

59.4 KB
Loading

single.gif

74.4 KB
Loading

0 commit comments

Comments
 (0)