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