在Kali Linux上安装Docker和Docker Compose

时间:2020-02-23 14:31:41  来源:igfitidea点击:

欢迎来到我们的教程,了解如何在Kali Linux机器上安装Docker和Docker Compose。在Kali Linux上安装Docker CE(社区版)。 Docker是最流行且使用最广泛的容器运行时。它使开发人员可以在隔离的容器中打包,运送和运行其应用程序。使用Kubernetes轻松将其从Developer机器运送到生产环境。

以下是Docker生态系统中常用的术语。Docker守护进程:也称为Docker Engine,它是在宿主系统上运行的后台进程,负责构建和运行容器.DockerClient:这是用户使用的命令行工具与Docker守护程序进行交互。Docker镜像:镜像是一个不可变的文件,本质上是容器的快照。 Docker镜像具有运行应用程序所需的文件系统和应用程序依赖项。Docker容器:这是具有应用程序及其依赖项的Docker镜像的运行实例。每个容器都有唯一的进程ID,并且与其他容器隔离。容器唯一共享的是Kernel.Docker注册表:这是一个负责管理Docker容器镜像的存储和交付的应用程序。它可以是私人的也可以是公共的。

因此,让我们开始在Kali Linux上安装Docker。

安装依赖包

通过确保已安装docker用作依赖项的所有软件包来开始安装。

sudo apt update
sudo apt -y install curl gnupg2 apt-transport-https software-properties-common ca-certificates

第2步:导入Docker GPG密钥:

导入用于签署Docker软件包的Docker GPG密钥:

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add 

将Docker储存库添加到Kali Linux

添加包含最新稳定版本Docker CE的Docker存储库。

echo "deb [arch=amd64] https://download.docker.com/linux/debian buster stable" | sudo tee  /etc/apt/sources.list.d/docker.list

此命令会将存储库URL添加到/etc/apt/sources.list.d/docker.list。

在Kali Linux上安装Docker

更新apt包索引。

$sudo apt update
gn:1 http://dl.google.com/linux/chrome/deb stable InRelease
Get:3 https://download.docker.com/linux/debian buster InRelease [44.4 kB]                          
Hit:2 http://kali.download/kali kali-rolling InRelease
Hit:4 http://dl.google.com/linux/chrome/deb stable Release
Get:5 https://download.docker.com/linux/debian buster/stable amd64 Packages [10.9 kB]
Fetched 55.3 kB in 1s (45.2 kB/s)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
186 packages can be upgraded. Run 'apt list --upgradable' to see them.

要在Kali Linux上安装Docker CE,请运行以下命令:

sudo apt install docker-ce docker-ce-cli containerd.io

按y键开始在Kali Linux上安装Docker。

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  aufs-dkms aufs-tools cgroupfs-mount dkms linux-compiler-gcc-9-x86 linux-headers-5.4.0-kali3-amd64
  linux-headers-5.4.0-kali3-common linux-headers-amd64 linux-kbuild-5.4 pigz
Suggested packages:
  aufs-dev menu
The following NEW packages will be installed:
  aufs-dkms aufs-tools cgroupfs-mount containerd.io dkms docker-ce docker-ce-cli linux-compiler-gcc-9-x86
  linux-headers-5.4.0-kali3-amd64 linux-headers-5.4.0-kali3-common linux-headers-amd64 linux-kbuild-5.4 pigz
0 upgraded, 13 newly installed, 0 to remove and 186 not upgraded.
Need to get 98.1 MB of archives.
After this operation, 446 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

该安装会将docker组添加到系统中,而无需任何用户。将用户帐户添加到组中,以非特权用户身份运行docker命令。

sudo usermod -aG docker $USER
newgrp docker

检查已安装的Docker版本。

$docker version
Client: Docker Engine - Community
 Version:           19.03.6
 API version:       1.40
 Go version:        go1.12.16
 Git commit:        369ce74a3c
 Built:             Thu Nov 13 01:27:58 2017
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.6
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.16
  Git commit:       369ce74a3c
  Built:            Thu Nov 13 01:26:32 2017
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.12
  GitCommit:        35bd7a5f69c13e1563af8a93431411cd9ecf5021
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

在Kali Linux上安装Docker Compose

使用以下教程在Kali Linux上安装最新的Docker Compose。

如何在Linux上安装最新的Docker Compose

安装后,通过检查版本确认安装成功。

$ docker-compose version
docker-compose version 1.25.4, build 8d51620a
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

测试Docker安装。

运行测试泊坞窗容器:

$docker run --rm -it  hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

测试Docker Compose安装。

创建一个测试Docker Compose文件。

$vim docker-compose.yml

将以下数据添加到文件中。

version: '3'  
services:
  web:
    image: nginx:latest
    ports:
     - "8080:80"
    links:
     - php
  php:
    image: php:7-fpm

启动服务容器。

$docker-compose up -d
Creating network "jkmutai_default" with the default driver
Pulling php (php:7-fpm)...
7-fpm: Pulling from library/php
bc51dd8edc1b: Pull complete
a3224e2c3a89: Pull complete
be7a066df88f: Pull complete
bfdf741d72a9: Pull complete
0096578ff21c: Pull complete
52b9a3846c51: Pull complete
91c8df69c5cf: Pull complete
ba16a1822680: Pull complete
c137b651214d: Pull complete
a2738b6c9bea: Pull complete
Digest: sha256:022dcc4f1a054584660ce3d77bb0dc1f5084d25f117d4814726518b7f66af47f
Status: Downloaded newer image for php:7-fpm
Pulling web (nginx:latest)...
latest: Pulling from library/nginx
bc51dd8edc1b: Already exists
66ba67045f57: Pull complete
bf317aa10aa5: Pull complete
Digest: sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
Status: Downloaded newer image for nginx:latest
Creating jkmutai_php_1 ... done
Creating jkmutai_web_1 ... done

显示正在运行的容器:

$docker-compose ps
    Name                   Command              State          Ports        
---------------------------------------------------------------------------
jkmutai_php_1   docker-php-entrypoint php-fpm   Up      9000/tcp            
jkmutai_web_1   nginx -g daemon off;            Up      0.0.0.0:8080->80/tcp

销毁容器:

$docker-compose stop
Stopping jkmutai_web_1 ... done
Stopping jkmutai_php_1 ... done

$docker-compose rm
Going to remove jkmutai_web_1, jkmutai_php_1
Are you sure? [yN] y
Removing jkmutai_web_1 ... done
Removing jkmutai_php_1 ... done