-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.sh
executable file
·145 lines (134 loc) · 5.27 KB
/
docker.sh
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
#
# Replaces docker with podman.
# In order to make it work, you have to do the following
# - Copy the script to a directory in the path and rename it to `docker` (e.g. "cp docker.sh $HOME/bin/docker")
# - Install Podman (e.g. `brew install podman`)
# - On MacOS: Start Podman with `podman-machine-start`
# - Set the variable DOCKER_HOST to: `podman-dockerhost` (on Linux: `export DOCKER_HOST=unix://$(podman info --format '{{.Host.RemoteSocket.Path}}')`)
# - Start the Podman API-Service: `mkdir -p "$(dirname "${DOCKER_HOST#unix://}")" & podman system service --time 0 "$DOCKER_HOST" &`
# if [ -n "${DEBUG}" ]; then
# set -o xtrace
# fi
set -o errexit # script stops on error (RC != 0)
# set -o nounset # abort on using undefined variable
set -o pipefail # script fails of one of the commands in the pipeline fails
# DEBUG="x" # Set to a value to output debug messages
DRYRUN="" # Set to a value to create dry run
##############################################################################
# FUNCTIONS
##############################################################################
log() {
if [ "$1" = "-n" ]; then
shift
echo -n -e "$1$2$"
else
echo -e "$1$2$"
fi
}
# For debugging
log_debug() {
if [ -n "$DEBUG" ]; then
log "" "[DCKR] $1"
fi
}
##############################################################################
# MAIN
##############################################################################
log_debug "== START OF DOCKER WRAPPER (cwd: $PWD) ========================================="
log_debug "Got arguments: ${*}"
if [ -z "$3" ]; then
log_debug "Calling plain: podman $*"
log_debug "v-------------------------------------------------------------------------------"
if [ -z "$DRYRUN" ]; then
podman "$@"
log_debug "^-------------------------------------------------------------------------------"
fi
elif [ "$3" = "buildx" ]; then
shift 3
declare -a PARAMS=()
PARAMNAME=""
TAG=""
CONTAINERNAME=""
REGISTRY=""
VERSIONTAG=""
MANIFEST=""
PUSH=0
for p in "$@"; do
if [ "$p" = "--builder" ] || [ "$PARAMNAME" = "--builder" ]; then
log_debug "Omitting parameter --builder!"
elif [ "$PARAMNAME" = "--tag" ]; then
TAG="$p"
CONTAINERNAME="$(echo "$TAG" | cut -f 2 -d "/" | cut -f 1 -d ":")"
REGISTRY="$(echo "$TAG" | cut -f 1 -d "/" -s)"
VERSIONTAG="$(echo "$TAG" | cut -f 2 -d "/" | cut -f 2 -d ":")"
MANIFEST="$CONTAINERNAME"
# PARAMS+=("$p")
elif [ "$p" = "--tag" ]; then
TAG=""
elif [ "$p" = "--push" ]; then
PUSH=1
else
PARAMS+=("$p")
fi
PARAMNAME="$p"
done
if [ -n "$TAG" ]; then
if podman manifest exists "$MANIFEST" >/dev/null 2>&1; then
ARGS=("manifest" "rm" "$MANIFEST")
log_debug "Removing manifest for buildx: podman ${ARGS[*]}"
log_debug "v-------------------------------------------------------------------------------"
if [ -z "$DRYRUN" ]; then
podman "${ARGS[@]}"
log_debug "^-------------------------------------------------------------------------------"
fi
fi
ARGS=("manifest" "create" "$MANIFEST")
log_debug "Creating manifest for buildx: podman ${ARGS[*]}"
log_debug "v-------------------------------------------------------------------------------"
if [ -z "$DRYRUN" ]; then
podman "${ARGS[@]}"
log_debug "^-------------------------------------------------------------------------------"
fi
fi
# shellcheck disable=SC2198
if [ "${PARAMS[@]:0:1}" = "build" ]; then
PARAMS=("build" "--network=host" "${PARAMS[@]:1}")
fi
if [ $PUSH -eq 1 ] && [ -n "$TAG" ]; then
PARAMS_C=("${PARAMS[@]:0:${#PARAMS[@]}-1}")
ARGS=("${PARAMS_C[@]}" "--manifest" "$MANIFEST" "${PARAMS[-1]}")
log_debug "Calling for buildx/push: podman ${ARGS[*]}"
log_debug "v-------------------------------------------------------------------------------"
if [ -z "$DRYRUN" ]; then
podman "${ARGS[@]}"
log_debug "^-------------------------------------------------------------------------------"
fi
ARGS=("manifest" "push" "$MANIFEST" "docker://$REGISTRY/$CONTAINERNAME:$VERSIONTAG")
log_debug "Calling for buildx/push: podman ${ARGS[*]}"
log_debug "v-------------------------------------------------------------------------------"
if [ -z "$DRYRUN" ]; then
podman "${ARGS[@]}"
log_debug "^-------------------------------------------------------------------------------"
fi
else
if [ "${PARAMS[0]}" = "create" ] || [ "${PARAMS[0]}" = "ls" ]; then
log_debug "Omitting call for buildx: podman ${PARAMS[*]}"
else
log_debug "Calling for buildx: podman ${PARAMS[*]}"
log_debug "v-------------------------------------------------------------------------------"
if [ -z "$DRYRUN" ]; then
podman "${PARAMS[@]}"
log_debug "^-------------------------------------------------------------------------------"
fi
fi
fi
else
log_debug "Calling: podman $*"
log_debug "v-------------------------------------------------------------------------------"
if [ -z "$DRYRUN" ]; then
podman "$@"
log_debug "^-------------------------------------------------------------------------------"
fi
fi
log_debug "== END OF DOCKER WRAPPER ======================================================="