1

I am trying to build a bash script which ultimately runs a docker container. I am running Ubuntu 20.04 LTS on a VM.

Docker works perfectly and I can run the image I want to, but now I want to automatize the startup in a script for the end user to just execute the script and have a working container.

To make the bash script foolproof (well, as much as possible), I want to check first if docker is installed. I have already installed docker on my machine via apt-get therefore, I assumed, that this code will just skip:

REQUIRED_PKG="docker"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  printf "No docker installation was found. Please install first. Manual can be found 
    here: https://docs.docker.com/engine/install/ubuntu/ \n"
    exit
fi

(this is copied and adapted from another thread here: How can I check if a package is installed and install it if not? )

When I now run my script, it exits here though. Running the dpkg-query isolated (alternatively dpgk -s results in the following output: dpkg: package 'docker' is not installed

A friend of mine told me that dpkg and apt-get have different install directories which makes it impossible to mix both methods.

The confusing part is that almost all solutions in this thread use both methods in combination and it seems to have no issue. But for me it does not work. Why is that?

I am now trying to use apt list docker but I am not sure how to handle the output. It should be empty when nothing is installed so the code would propably look something like this:

REQUIRED_PKG="docker"
if apt list $REQUIRED_PKG|grep $REQUIRED_PKG; then
    printf "Docker installed, continiung"
else
    printf "No docker installation was found. Please install first. Manual can be found 
    here: https://docs.docker.com/engine/install/ubuntu/ \n"
    exit
fi

This returns a warning that apt list does not have a stable CLI interface and it should not be used in scripts. What can I do here?

I would have asked the question there but the thread is very confusing by now and I did not find an answer there since all the solutions seem to use the combination of both methods

mr_harm
  • 61
  • 1
  • 1
  • 6
  • 1
    _A friend of mine told me that dpkg and apt-get have different install directories which makes it impossible to mix both methods._ > your friend is incorrect, apt actually uses dpkg in order to install packages. – β.εηοιτ.βε Apr 21 '22 at 10:58
  • 2
    APT internally calls `dpkg` to do the actual package installation, so the `dpkg-query` approach should work. The [Debian `docker` package](https://packages.debian.org/bullseye/docker) is a different tool, though; do you mean `docker.io` or `docker-ce`? – David Maze Apr 21 '22 at 10:59
  • 2
    Why not check if the command `docker` exists? `[[ -x "$(command -v docker)" ]]` – DannyB Apr 21 '22 at 11:03
  • it is `docker-ce` installed according to the installation guide from the website https://docs.docker.com/engine/install/ubuntu/ I`ve seen this thread too @β.εηοιτ.βε but I did not connect the dots to use that. I could do that. Nevertheless I am wondering why I got this behavior since it should work that way – mr_harm Apr 21 '22 at 11:40
  • ...but checking the `docker` command would also fail if i have `docker.io` installed (assuming that it uses the same command prefix?) - I am also not too sure if there are big differences here? – mr_harm Apr 21 '22 at 11:43
  • 1
    This all depends what your purpose is. You end result is not to check the fact that a package is installed, it would be useless. If your goal is to check that the end user does have the command `docker` installed to use it later in the same script, then testing the presence of it is what you should aim to. You would also test its version if needed (e.g. `docker --version` than act on the return). – β.εηοιτ.βε Apr 21 '22 at 11:46
  • 1
    IMO, ultimately, the way the end user installed it (apt, download of some sort, compiling it from the sources, via the docker desktop) shouldn't be your concern, when being sure that the user have the binary at the right version should. – β.εηοιτ.βε Apr 21 '22 at 11:47
  • 1
    And why does your command fails? Well probably because you have a `set -e` at the beginning of your script that makes the whole script stop if any command fails. If a package is not there, `dpkg` will return a non-zero exit code and will make your script fail. – β.εηοιτ.βε Apr 21 '22 at 11:50
  • The script itself does not actually fail, but it exits (by design) telling the user to install docker. This is due to the (wrong?) behaviour of `dpkg-query` as pointed out. Which IMHO is wrong behavior since the package was installed. Anyway, since there is currently no requirement on a specific version of `docker`, you're right that checking if the command is available in my case is sufficient. I will implement that. Thanks for your help :) – mr_harm Apr 21 '22 at 12:05
  • 2
    Try `REQUIRED_PKG=docker-ce` – Philippe Apr 21 '22 at 12:12

0 Answers0