How to install Docker with another Debian package?

645 views Asked by At

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 <[email protected]>
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.

1

There are 1 answers

3
Kaustubh Desai On BEST ANSWER

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

  • The first section covers all docker pre-requisites required.
  • The second section covers the installation command of your custom debian package.

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

  • Host the shell script on an http/https server. Ensure that the client machine where you want the debian package to be installed has access to this http/https server. On your client machine run:
    $ curl -s http://example.com/my_installation_script.sh | bash
  • If you want this package installed on a Cloud server, you could host the shell-script inside the Cloud Provider's object storage(AWS: s3, Azure: Blob storage,GCP: Google Cloud Storage) and on you client machine run:
    $ cloud_provider_cli service_name get object_file | cat object_file | bash
  • If you are okay with keeping the script public, you could write the script as a gist on Github_gist and host it there for free. On your client machine :
    $ curl -s https://gist.githubusercontent.com/long_url/shell_script.sh | bash You can get the mentioned url by clicking raw option on github to download your shell script from github gist.