From 69bed4bb6ea6df459a9ce846e698aace5719c57a Mon Sep 17 00:00:00 2001 From: Petr Bokoc Date: Sep 20 2018 12:15:13 +0000 Subject: Issue 3 - Check if sudo is really required to run docker --- diff --git a/build.sh b/build.sh index c9349e3..7d2b58d 100755 --- a/build.sh +++ b/build.sh @@ -10,7 +10,17 @@ elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then # Running on Linux. # Let's assume that it's running the Docker deamon # which requires root. - echo "" - echo "This build script is using Docker to run the build in an isolated environment. You might be asked for a root password in order to start it." -sudo docker run --rm -it -v $(pwd):/antora:z antora/antora --html-url-extension-style=indexify site.yml + if groups | grep -wq "docker"; then + # Check if the current user is in the "docker" group. If true, no sudo is needed. + echo "" + echo "This build script is using Docker to run the build in an isolated environment." + docker run --rm -it -v $(pwd):/antora:z antora/antora --html-url-extension-style=indexify site.yml + else + # User isn't in the docker group; run the command with sudo. + echo "" + echo "This build script is using Docker to run the build in an isolated environment. + You might be asked for a root password in order to start it. + Add your user to the docker group to avoid this." + sudo docker run --rm -it -v $(pwd):/antora:z antora/antora --html-url-extension-style=indexify site.yml + fi fi diff --git a/preview.sh b/preview.sh index c2fcc4b..d4a7215 100755 --- a/preview.sh +++ b/preview.sh @@ -11,8 +11,18 @@ elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then # Running on Linux. # Let's assume that it's running the Docker deamon # which requires root. - echo "" - echo "This build script is using Docker to run the build in an isolated environment. You might be asked for a root password in order to start it." - echo "The preview will be available at http://localhost:8080/" - sudo docker run --rm -v $(pwd):/antora:ro,z -v $(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf:ro,z -p 8080:80 nginx + if groups | grep -wq "docker"; then + # Check if the current user is in the "docker" group. If true, no sudo is needed. + echo "" + echo "This build script is using Docker to run the build in an isolated environment." + echo "The preview will be available at http://localhost:8080/" + docker run --rm -v $(pwd):/antora:ro,z -v $(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf:ro,z -p 8080:80 nginx + else + # User isn't in the docker group; run the command with sudo. + echo "This build script is using Docker to run the build in an isolated environment. + You might be asked for a root password in order to start it. + Add your user to the docker group to avoid this." + echo "The preview will be available at http://localhost:8080/" + sudo docker run --rm -v $(pwd):/antora:ro,z -v $(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf:ro,z -p 8080:80 nginx + fi fi