Skip to content

Commit

Permalink
create: check if container exists using inspect
Browse files Browse the repository at this point in the history
enter: improve regex to check if a container is started
  • Loading branch information
89luca89 committed Dec 12, 2021
1 parent 3089e20 commit f4680da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions distrobox-create
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ generate_command() {
fi
result_command="${result_command}
--env=\"XDG_RUNTIME_DIR=/run/user/${container_user_uid}\"
--hostname ${container_name}
--ipc host"
--hostname ${container_name}"
# mount useful stuff inside the container.
# we also mount host's root filesystem to /run/host, to be
# able to syphon dynamic configurations from the host
#
# also mount the distrobox-init utility as the container entrypoint
result_command="${result_command} --name ${container_name}
--env=\"SHELL=${SHELL}\"
--ipc host
--network host
--pid host
--privileged
Expand Down Expand Up @@ -207,8 +207,8 @@ if [ -z "$(podman images -q "${container_image}")" ]; then
fi

# Check if the container already exists.
if podman ps -a | grep -q "${container_name}\$"; then
printf "Distrobox named '%s' already exists." "${container_name}"
if podman inspect "${container_name}" >/dev/null 2>&1; then
printf "Distrobox named '%s' already exists.\n" "${container_name}"
printf "To enter, run:\n"
printf "\tdistrobox-enter --name %s\n" "${container_name}"
exit 0
Expand All @@ -219,7 +219,7 @@ cmd="$(generate_command)"
# Eval the generated command. If successful display an helpful message.
# shellcheck disable=SC2086
if eval ${cmd}; then
printf "Distrobox '%s' successfully created." "${container_name}"
printf "Distrobox '%s' successfully created.\n" "${container_name}"
printf "To enter, run:\n"
printf "\tdistrobox-enter --name %s\n" "${container_name}"
fi
2 changes: 1 addition & 1 deletion distrobox-enter
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fi
# prints the podman command to enter the distrobox container
generate_command() {
# If the container is not already running, we need to start if first
if ! podman ps | grep -q "${container_name}\$"; then
if ! podman ps | grep -qE "(^| )${container_name}( |$)"; then
# if container is not running, start it first
if ! podman start "${container_name}" >/dev/null; then

Expand Down

0 comments on commit f4680da

Please sign in to comment.