Last active
February 24, 2024 06:58
-
-
Save RakibFiha/3aa5dedc1fc7b98fd0ea1a346ccc6b55 to your computer and use it in GitHub Desktop.
docker wrapper for podman, colima and docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Author: rakibfiha | |
# Usage: Save it to $HOME/.local/bin/docker and add this path to your $PATH | |
# ENV: DOCKER_RUNTIME=podman|colima-containerd; Default: colima-containerd | |
# For colima: colima start --runtime containerd | |
# For podman: podman machine init && podman machine start and set DOCKER_RUNTIME=podman | |
# For docker: set DOCKER_RUNTIME=full/path/to/docker/executable | |
runtime_exists() { | |
local status runtime | |
runtime="$1" | |
type -p "$runtime" > /dev/null && status=0 || status=1 | |
if [[ $status == 1 ]]; then | |
echo "Runtime: $runtime does not exist.." >&2 && return 1; | |
fi | |
} | |
_docker() { | |
local runtime="$1" | |
local status docker_host | |
shift 2 | |
case "$runtime" in | |
colima-containerd) | |
! runtime_exists colima && exit 1; | |
type -p nerdctl > /dev/null || colima nerdctl install 2> /dev/null; | |
docker_host="unix:///$HOME/.colima/docker.sock" | |
DOCKER_HOST="${DOCKER_HOST:-$docker_host}" nerdctl "$@" | |
;; | |
podman) | |
! runtime_exists podman && exit 1; | |
docker_host="unix:///$HOME/.local/share/containers/podman/machine/podman-machine-default/podman.sock" | |
DOCKER_HOST="${DOCKER_HOST:-$docker_host}" podman "$@" | |
;; | |
*) | |
if [[ -x "$runtime" ]]; then | |
if "$runtime" -v | awk '{print $1}' | grep -qi '^Docker$'; then | |
"$runtime" "$@" && status=$? || status=$? | |
exit "$status" | |
fi | |
fi | |
echo "Runtime: $runtime does not exist.. Exiting.." && exit 1; | |
;; | |
esac | |
} | |
docker() { | |
local runtime=$1 | |
_docker "$runtime" "$@" | |
} | |
main() { | |
set -euo pipefail && docker "${DOCKER_RUNTIME:-colima-containerd}" "$@" | |
} | |
[[ "${BASH_SOURCE[0]}" == "$0" ]] && main "$@"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment