简体   繁体   English

如何使用另一个 Debian 软件包安装 Docker?

[英]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.我在 Docker Registry 上有 2 个镜像,所以我的要求是创建 Debian 包来提取这 2 个镜像并在已安装的机器上运行。 My problem is if I want to use Docker command, Docker should be installed in the given machine.我的问题是如果我想使用 Docker 命令,应该在给定的机器上安装 Docker。 So Docker install already, then we can use it.所以Docker已经安装好了,然后我们就可以使用它了。 Otherwise I have to install it when I install that Debian Package.否则,我必须在安装该 Debian 软件包时安装它。

My DEBIAN/control file look like this,我的DEBIAN/control文件看起来像这样,

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.我不时地为Depends添加了Dockerdocker ,但是没有用。 so I added below lines to the preinst file,所以我在preinst文件中添加了以下preinst行,

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. sudo apt install -y docker-ce这一行无法运行。 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.是的,我知道发生这种情况是因为我正在运行sudo apt install my_package.deb来安装我的包,所以无法在我的包中运行另一个sudo apt install命令。 How can I sort it out this problem?我该如何解决这个问题? This should be one time process.这应该是一次性过程。

There is one possible way.有一种可能的方法。
Create one shell script that is divided into two sections.创建一个分为两部分的shell脚本。

  • The first section covers all docker pre-requisites required.第一部分涵盖了所需的所有docker先决条件。
  • The second section covers the installation command of your custom debian package.第二部分介绍了自定义debian包的安装命令。

To make all of this run in one command, there are a few options:要在一个命令中运行所有这些,有几个选项:

  • Host the shell script on an http/https server.在 http/https 服务器上托管 shell 脚本。 Ensure that the client machine where you want the debian package to be installed has access to this http/https server.确保要安装 debian 软件包的客户端计算机可以访问此 http/https 服务器。 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:如果你想在云服务器上安装这个包,你可以在云提供商的对象存储(AWS:s3,Azure:Blob 存储,GCP:谷歌云存储)中托管 shell 脚本,并在你的客户端机器上运行:
    $ 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.如果您同意将脚本公开,您可以将脚本编写为Github_gist上的要点并免费托管在那里。 On your client machine :在您的客户端机器上:
    $ curl -s https://gist.githubusercontent.com/long_url/shell_script.sh | bash $ 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. $ curl -s https://gist.githubusercontent.com/long_url/shell_script.sh | bash您可以通过单击 github 上的raw选项来获取提到的 url,以从 github gist 下载您的 shell 脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM