From a7fec65e5c611510b29036138936856d536eb3a5 Mon Sep 17 00:00:00 2001 From: 89luca89 Date: Wed, 15 Dec 2021 02:01:53 +0100 Subject: [PATCH] create: add docker support. First check if we have podman, then docker, else error. Some options are only for podman, so let's put them all together in a check. Also move log level up in the command so it is compatible with both container managers. --- distrobox-create | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/distrobox-create b/distrobox-create index cde7ee1a..ab23bfcf 100755 --- a/distrobox-create +++ b/distrobox-create @@ -93,25 +93,31 @@ if [ "${verbose}" -ne 0 ]; then set -o xtrace fi -# We depend on podman let's be sure we have it -if ! command -v podman >/dev/null; then - printf >&2 "Missing dependency: podman\n" +# We depend on a container manager let's be sure we have it +# First we use podman, else docker +container_manager="podman" +if ! command -v podman >/dev/null && command -v docker >/dev/null; then + container_manager="docker" +elif ! command -v podman >/dev/null && ! command -v docker >/dev/null; then + printf >&2 "Missing dependency: we need a container manager\n." + printf >&2 "Please install one of podman or docker.\n" exit 127 fi -# Generate Podman command to execute. +# Generate Podman or Docker command to execute. # Arguments: # None # Outputs: -# prints the podman command to create the distrobox container +# prints the podman or docker command to create the distrobox container generate_command() { # Set the container hostname the same as the container name. # use the host's namespace for ipc, network, pid, ulimit - result_command="podman create" - # add podman verbose if -v is specified + result_command="${container_manager}" + # add verbose if -v is specified if [ "${verbose}" -ne 0 ]; then result_command="${result_command} --log-level debug" fi + result_command="${result_command} create" result_command="${result_command} --env=\"SHELL=${SHELL}\" --env=\"XDG_RUNTIME_DIR=/run/user/${container_user_uid}\" @@ -122,9 +128,7 @@ generate_command() { --pid host --privileged --security-opt label=disable - --user root:root - --userns keep-id - --ulimit host" + --user root:root" # let's check if we can include distrobox-export or not if [ -n "${distrobox_export_path}" ]; then @@ -146,8 +150,7 @@ generate_command() { --volume /:/run/host:rslave --volume /dev:/dev:rslave --volume /sys:/sys:rslave - --volume /tmp:/tmp:rslave - --mount type=devpts,destination=/dev/pts" + --volume /tmp:/tmp:rslave" # Mount also the XDG_RUNTIME_DIR to ensure functionality of the apps. if [ -d "/run/user/${container_user_uid}" ]; then @@ -182,6 +185,12 @@ generate_command() { fi done + if [ "${container_manager}" = "podman" ]; then + result_command="${result_command} + --userns keep-id + --ulimit host + --mount type=devpts,destination=/dev/pts" + fi # Now execute the entrypoint, refer to `distrobox-init -h` for instructions result_command="${result_command} ${container_image} /usr/bin/entrypoint -v --name ${container_user_name} @@ -202,7 +211,7 @@ fi # Check if the container already exists. # If it does, notify the user and exit. -if podman ps -a | grep -qE "(^| )${container_name}( |$)"; then +if "${container_manager}" ps -a | grep -qE "(^| )${container_name}( |$)"; then printf "Distrobox named '%s' already exists.\n" "${container_name}" printf "To enter, run:\n" printf "\tdistrobox-enter --name %s\n" "${container_name}" @@ -211,7 +220,7 @@ fi # First, check if the image exists in the host. # If not prompt to download it. -if [ -z "$(podman images -q "${container_image}")" ]; then +if [ -z "$(${container_manager} images -q "${container_image}")" ]; then # Prompt to download it. printf >&2 "Image not found.\n" printf >&2 "Do you want to pull the image now?[y/n] " @@ -221,11 +230,11 @@ if [ -z "$(podman images -q "${container_image}")" ]; then case "${response}" in y | Y | yes | Yes) # Pull the image - podman pull "${container_image}" + "${container_manager}" pull "${container_image}" ;; n | N | no | No) printf >&2 "next time, run this command first:\n" - printf >&2 " podman pull %s\n" "${container_image}" + printf >&2 "\t%s pull %s\n" "${container_manager}" "${container_image}" exit 0 ;; *) # Default case: If no more options then break out of the loop.