linuxubuntudebiandebdebian-packaging

How to install Docker with another Debian package?


I have 2 images on Docker Registry, so my requirement is to create Debian package to pull those 2 images and run on the installed machine. My problem is if I want to use Docker command, Docker should be installed in the given machine. So Docker install already, then we can use it. Otherwise I have to install it when I install that Debian Package.

My DEBIAN/control file look like this,

Package: bla-bla
Version: 1.0
Architecture: all
Priority: optional
Maintainer: Bla Bla <bla.bla@bla.com>
Installed-Size: 327000
Depends: unzip, curl, sqlite3, libsqlite3-dev, xdg-utils, apt-transport-https, ca-certificates, software-properties-common
Homepage: https://www.example.com/
Section: Network, Databases, Web Servers, JavaScript, Python;
Description: bla bla bla

I added Docker and docker for Depends time to time, but it didn't work. so I added below lines to the preinst file,

function check_whether_docker_installed() {
  service=docker
  is_running=$(ps aux | grep -v grep | grep -v "$0" | grep $service | wc -l | awk '{print $1}')

  if [ $is_running != "0" ]; then
    echo -e "\nservice $service is running"
  else
    initd=$(ls /etc/init.d/ | grep $service | wc -l | awk '{ print $1 }')

    if [ $initd = "1" ]; then
      startup=$(ls /etc/init.d/ | grep $service)
      echo -e "\nStarting Docker service"
      /etc/init.d/${startup} start
      echo "Docker service successfully started"
    else
      echo -e "\nService $service not yet installed, going to install it"
      sudo apt update
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
      sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
      sudo apt update
      apt-cache policy docker-ce
      sudo apt install -y docker-ce
      sudo chmod +x /var/run/docker.sock
      echo "Docker successfully installed, let's check it again"
      check_whether_docker_installed
    fi
  fi
}

function download_and_install_docker_compose() {
  which docker-compose

  if [ $? -eq 0 ]; then
    echo -e "\ndocker-compose is installed!"
  else
    echo -e "\ndocker-compose is not installed!"
    sudo curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    echo "docker-compose is successfully installed, let's check it again"
    download_and_install_docker_compose
  fi
}

check_whether_docker_installed
download_and_install_docker_compose

sudo apt install -y docker-ce this line wasn't able to run. It gave me below error.

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
chmod: cannot access '/var/run/docker.sock': No such file or directory

Yes I know this happens because I'm running sudo apt install my_package.deb to install my package already, so cannot run another sudo apt install command inside my package. How can I sort it out this problem? This should be one time process.


Solution

  • There is one possible way.
    Create one shell script that is divided into two sections.

    To make all of this run in one command, there are a few options: