-
Notifications
You must be signed in to change notification settings - Fork 0
/
.docker_aliases
131 lines (110 loc) · 5.22 KB
/
.docker_aliases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
printf "\nUseful Docker aliases:\n\n"
# define the path to the alias cache
DOCKER_COMPOSE_YAML_DIR="/home/ginocic"
DOCKER_COMPOSE_YAML="$DOCKER_COMPOSE_YAML_DIR/docker-compose.yaml"
isNamedContainer() {
if [ -e "$DOCKER_COMPOSE_YAML" ] ; then
echo $(grep -c -E "^ +container_name: $1" $DOCKER_COMPOSE_YAML)
else
echo 0
fi
}
if [ $(isNamedContainer home-assistant) -gt 0 ] ; then
alias hacontlogs='docker container logs home-assistant -f'
alias hacontsh='docker exec -it home-assistant sh'
alias halogs='docker logs home-assistant'
alias haupdate='dc-pull home-assistant && dc-build home-assistant && docker image prune -f'
echo " Home Assistant: hacontlogs | hacontsh | halogs"
echo " haupdate"
echo ""
fi
if [ $(isNamedContainer esphome) -gt 0 ] ; then
alias espwizard='docker run --rm -v /home/ginocic/esphome/config:/config --net=host -it esphome/esphome wizard'
alias espinstall='docker run --rm -v /home/ginocic/esphome/config:/config --net=host -it esphome/esphome --device=/dev/ttyusb0 run'
echo " ESPHome : espwizard | espinstall {<nome-progetto>.yaml}"
echo ""
fi
unset -f isNamedContainer
# define the path to the alias cache
ALIAS_CACHE_DIR="$DOCKER_COMPOSE_YAML_DIR/.cache/DockerAliases"
ALIAS_CACHE="$ALIAS_CACHE_DIR/cache"
# ensure the directory exists
mkdir -p "$ALIAS_CACHE_DIR"
# does the cache exist?
if [ ! -f "$ALIAS_CACHE" ] ; then
echo 'dps() {' >>"$ALIAS_CACHE"
echo ' local DPS_CMD="docker ps --format \"table {{.Names}}\t{{.RunningFor}}\t{{.Status}}\""' >>"$ALIAS_CACHE"
echo ' while (( "$#" )); do' >>"$ALIAS_CACHE"
echo ' DPS_CMD="$DPS_CMD --filter name=$1"' >>"$ALIAS_CACHE"
echo ' shift' >>"$ALIAS_CACHE"
echo ' done' >>"$ALIAS_CACHE"
echo ' bash -c "$DPS_CMD"' >>"$ALIAS_CACHE"
echo '}' >>"$ALIAS_CACHE"
echo "" >>"$ALIAS_CACHE"
echo 'dnet() {' >>"$ALIAS_CACHE"
echo ' local DNET_CMD="docker ps --format \"table {{.Names}}\t{{.Ports}}\""' >>"$ALIAS_CACHE"
echo ' while (( "$#" )); do' >>"$ALIAS_CACHE"
echo ' DNET_CMD="$DNET_CMD --filter name=$1"' >>"$ALIAS_CACHE"
echo ' shift' >>"$ALIAS_CACHE"
echo ' done' >>"$ALIAS_CACHE"
echo ' bash -c "$DNET_CMD"' >>"$ALIAS_CACHE"
echo '}' >>"$ALIAS_CACHE"
echo "" >>"$ALIAS_CACHE"
echo "alias dc-up='docker compose -f ${DOCKER_COMPOSE_YAML} up -d'" >>"$ALIAS_CACHE"
echo "alias dc-restart='docker compose -f ${DOCKER_COMPOSE_YAML} restart'" >>"$ALIAS_CACHE"
echo "alias dc-stop='docker compose -f ${DOCKER_COMPOSE_YAML} stop'" >>"$ALIAS_CACHE"
echo "alias dc-pull='docker compose -f ${DOCKER_COMPOSE_YAML} pull'" >>"$ALIAS_CACHE"
echo "alias dc-build='docker compose -f ${DOCKER_COMPOSE_YAML} up --build -d'" >>"$ALIAS_CACHE"
echo "alias dc-rebuild='docker compose -f ${DOCKER_COMPOSE_YAML} build --no-cache --pull'" >>"$ALIAS_CACHE"
echo "alias dc-recreate='docker compose -f ${DOCKER_COMPOSE_YAML} up -d --force-recreate'" >>"$ALIAS_CACHE"
echo "alias dc-terminate='docker compose -f ${DOCKER_COMPOSE_YAML} rm --force --stop -v'" >>"$ALIAS_CACHE"
echo "alias dc-edit='nano ${DOCKER_COMPOSE_YAML}'" >>"$ALIAS_CACHE"
echo "alias dc-test='docker compose -f ${DOCKER_COMPOSE_YAML} config'" >>"$ALIAS_CACHE"
echo "alias dc-down='docker compose -f ${DOCKER_COMPOSE_YAML} down'" >>"$ALIAS_CACHE"
echo "alias dc-prune='docker system prune -f'" >>"$ALIAS_CACHE"
echo "" >>"$ALIAS_CACHE"
echo 'echo ""' >>"$ALIAS_CACHE"
echo 'echo " Docker: dc-up | dc-restart | dc-stop {<container> …}"' >>"$ALIAS_CACHE"
echo 'echo " dc-pull | dc-build | dc-rebuild {<container> …}"' >>"$ALIAS_CACHE"
echo 'echo " dc-recreate | dc-terminate {<container> …}"' >>"$ALIAS_CACHE"
echo 'echo " dps | dnet {<container> …}"' >>"$ALIAS_CACHE"
echo 'echo " dc-edit, dc-test, dc-down, dc-prune"' >>"$ALIAS_CACHE"
echo 'echo ""' >>"$ALIAS_CACHE"
echo "" >>"$ALIAS_CACHE"
# no! fetch the list of running containers
CONTAINERS=$(curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json | jq -r '.[].Names | .[] | gsub("^/";"")')
# non-empty result?
if [ -n "$CONTAINERS" ] ; then
# yes! tell the user this may take some time
echo "Building $ALIAS_CACHE - this may take some time"
# initialise the cache file
touch "$ALIAS_CACHE"
echo "# Rebuilt $(date)" >>"$ALIAS_CACHE"
# iterate the list
for CONTAINER in $CONTAINERS ; do
echo -n " checking $CONTAINER - "
CANDIDATES=$(docker exec $CONTAINER which bash ash zsh sh)
if [ $? -eq 1 ] ; then
FIRST=$(echo $CANDIDATES | cut -d " " -f 1)
echo "$FIRST"
echo "alias ${CONTAINER^^}_SHELL='docker exec -it $CONTAINER $FIRST'" >>"$ALIAS_CACHE"
else
echo "no shell found"
fi
done
echo "" >>"$ALIAS_CACHE"
echo 'echo " Shells: <CONTAINER>_SHELL (all capital letters)"' >>"$ALIAS_CACHE"
echo "" >>"$ALIAS_CACHE"
else
echo "No containers running"
fi
fi
# does the cache exist now?
if [ -f "$ALIAS_CACHE" ] ; then
# yes! source the cache
source "$ALIAS_CACHE"
fi
# clean up
rm "$ALIAS_CACHE"
unset ALIAS_CACHE_DIR ALIAS_CACHE CONTAINERS CONTAINER CANDIDATES FIRST ALIASNAME
echo ""