【问题标题】:How to install Docker with another Debian package?如何使用另一个 Debian 软件包安装 Docker?
【发布时间】:2020-10-18 06:03:24
【问题描述】:

我在 Docker Registry 上有 2 个镜像,所以我的要求是创建 Debian 包来拉取这 2 个镜像并在已安装的机器上运行。我的问题是如果我想使用 Docker 命令,Docker 应该安装在给定的机器上。所以Docker已经安装好了,然后我们就可以使用它了。否则我必须在安装那个 Debian 软件包时安装它。

我的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

我不时为Depends 添加了Dockerdocker,但没有成功。所以我在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 此行无法运行。它给了我以下错误。

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

是的,我知道会发生这种情况,因为我已经在运行 sudo apt install my_package.deb 来安装我的软件包,因此无法在我的软件包中运行另一个 sudo apt install 命令。我该如何解决这个问题?这应该是一次性的过程。

【问题讨论】:

    标签: linux ubuntu debian deb debian-packaging


    【解决方案1】:

    有一种可能的方法。
    创建一个shell 脚本,分为两个部分。

    • 第一部分涵盖了所有docker 所需的先决条件。
    • 第二部分介绍自定义debian 包的安装命令。

    要使所有这些在一个命令中运行,有几个选项:

    • 在 http/https 服务器上托管 shell 脚本。确保您希望安装 debian 软件包的客户端计算机可以访问此 http/https 服务器。在您的客户端计算机上运行:
      $ curl -s http://example.com/my_installation_script.sh | bash
    • 如果您希望将此软件包安装在云服务器上,您可以在云提供商的对象存储(AWS:s3,Azure:Blob 存储,GCP:Google 云存储)中托管 shell 脚本,然后在您的客户端计算机上运行:
      $ cloud_provider_cli service_name get object_file | cat object_file | bash
    • 如果您可以公开脚本,您可以在Github_gist 上编写脚本作为要点并免费托管。在您的客户端计算机上:
      $ curl -s https://gist.githubusercontent.com/long_url/shell_script.sh | bash 您可以通过单击 github 上的 raw 选项从 github gist 下载您的 shell 脚本来获取提到的 url。

    【讨论】:

    • 我会试试这个,谢谢@kaustubh-desai 有时间回答我的问题。
    • @tikirintukurun 很高兴为您提供帮助。
    • 这是一场安全噩梦......你实际上让某人拥有你机器的 root 权限
    猜你喜欢
    • 2017-12-30
    • 2012-01-18
    • 2016-10-08
    • 2012-02-15
    • 2013-06-30
    • 2018-05-21
    • 2012-09-04
    • 2019-01-01
    • 2018-06-02
    相关资源
    最近更新 更多