服务器安装docker环境

1.官方一键安装脚本

菜鸟教程:https://www.runoob.com/docker/ubuntu-docker-install.html

2.文字教程

在本指南中,我们将看到什么是 Docker,如何在 Ubuntu Linux 中安装 Docker 引擎。此外,我们还将了解如何安装 Docker compose,这是一个用于定义和运行多容器 Docker 应用程序的工具。

本指南已在 Ubuntu 22.04 LTS 上正式测试。但是,它应该适用于旧版本,例如 20.04 LTS 和 18.04 LTS。为了获得更好的安全性和稳定性,我建议您使用最新的 Ubuntu 22.04 LTS 版本。

什么是 Docker?

Docker 是一种快速、轻量级的操作系统级虚拟化技术,适用于开发人员和系统管理员,他们希望构建具有所有必需依赖项的应用程序,并将其仅作为一个包发布。

与其他虚拟化方法(如 VMWare、Xen 和 VirtualBox)不同,每个虚拟机不需要单独的来宾操作系统。

所有 Docker 容器都有效地共享主机操作系统的内核。每个容器将在同一操作系统的独立用户空间中运行。

Docker 容器也可以在任何 Linux 变体上运行。假设你在 Fedora 中工作,而我在使用 Ubuntu。我们仍然可以相互开发、共享和分发 Docker 映像。

您不必担心操作系统、软件、自定义设置或任何东西。只要我们在主机系统中安装了 Docker,我们就可以继续开发。简单地说,Docker可以在任何地方工作!

您在上面的段落中阅读了两个术语,即 Docker 映像和 Docker 容器。您可能想知道,它们是什么以及它们之间有什么区别。

通俗地说,Docker 镜像是描述容器应如何行为的文件,而 Docker 容器是 Docker 镜像的运行(或停止)状态。

希望你对 Docker 有一个基本的了解。有关更多详细信息,请参阅官方 Docker 用户指南。该链接附在本指南的末尾。

Docker 要求

若要安装和配置 Docker,系统必须满足以下最低要求。

64 位Linux 或 Windows 操作系统。

如果您使用的是 Linux,内核版本应为 3.10 或更高版本。

具有 sudo 权限的用户帐户。

在系统 BIOS 上启用了 VT(虚拟化技术)支持。[阅读:如何确定 CPU 是否支持虚拟化技术 (VT)]

您的系统应连接到 Internet。

在 Linux 中,要验证内核和体系结构详细信息,请从终端运行以下命令:

$ uname -a

示例输出:

Linux Ubuntu22CT 5.15.35-3-pve #1 SMP PVE 5.15.35-6 (Fri, 17 Jun 2022 13:42:35 +0200) x86_64 x86_64 x86_64 GNU/Linux

正如您在上面的输出中看到的,我的 Ubuntu 系统的内核版本是 5.15.35-3-pve,我的 Ubuntu 系统的架构是 64 位(x86_64 x86_64 x86_64 GNU/Linux)。检查上述结果中的粗体字母。

小心:在这里,我在 Proxmox 中使用 Ubuntu 22.04 容器。这就是为什么在上面的输出中,您会在内核版本中看到“pve”一词的原因。如果您使用的是 Ubuntu 物理(或虚拟机)计算机,您将看到 5.15.35-3-generic 作为内核版本。

好吧,内核版本高于最低要求,并且 arch 是 64 位。因此,我们可以毫无问题地安装和使用 Docker。

请注意,您使用哪种 Ubuntu 操作系统并不重要。此外,无论您使用 Ubuntu 桌面还是 Ubuntu Server 版本或任何其他 Ubuntu 变体,例如 Lubuntu、Kubuntu、Xubuntu,都无关紧要。

只要你的系统有内核版本3.10+,并且你的系统架构是64位,Docker就可以正常工作。

在 Ubuntu 中安装 Docker 22.04 LTS

首先,更新您的 Ubuntu 系统。

1.更新Ubuntu

打开终端,然后逐个运行以下命令:

$ sudo apt update

$ sudo apt upgrade

$ sudo apt full-upgrade

2. 添加 Docker 存储库

首先,安装必要的证书,并允许 apt 包管理器使用 HTTPS 使用命令:

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release

接下来,通过运行以下命令添加 Docker 的官方 GPG 密钥:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

添加 Docker 官方仓库:

$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

使用命令更新 Ubuntu 源列表:

$ sudo apt update

3. 安装 Docker

最后,运行以下命令以在 Ubuntu 22.04 LTS 服务器中安装最新的 Docker CE:

$ sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

当然,您也可以安装特定的 Docker 版本。若要检查可用 Docker 版本的列表,请运行:

$ apt-cache madison docker-ce

示例输出:

 docker-ce | 5:20.10.17~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

 docker-ce | 5:20.10.16~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

 docker-ce | 5:20.10.15~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

 docker-ce | 5:20.10.14~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

 docker-ce | 5:20.10.13~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

您可以从上面的列表中选择任何可用版本并安装它。例如,要安装版本 5:20.10.16~3-0~ubuntu-jammy,请运行:

$ sudo apt install docker-ce=5:20.10.16~3-0~ubuntu-jammy docker-ce-cli=5:20.10.16~3-0~ubuntu-jammy containerd.io

安装后,使用以下命令验证 Docker 服务是否正在运行:

$ systemctl status docker
您将看到如下所示的输出。

* docker.service - Docker Application Container Engine

     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)

     Active: active (running) since Mon 2022-06-27 13:07:43 UTC; 3min 4s ago

TriggeredBy: * docker.socket

       Docs: https://docs.docker.com

   Main PID: 2208 (dockerd)

      Tasks: 8

     Memory: 29.6M

        CPU: 126ms

     CGroup: /system.slice/docker.service

             `-2208 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.071453522Z" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:>

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.071459974Z" level=info msg="ClientConn switching balancer to \"pick_first\"" >

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.130989294Z" level=info msg="Loading containers: start."

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.187439756Z" level=info msg="Default bridge (docker0) is assigned with an IP a>

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.235966874Z" level=info msg="Loading containers: done."

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.240149866Z" level=warning msg="Not using native diff for overlay2, this may c>

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.240281966Z" level=info msg="Docker daemon" commit=a89b842 graphdriver(s)=over>

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.240386856Z" level=info msg="Daemon has completed initialization"

Jun 27 13:07:43 Ubuntu22CT systemd[1]: Started Docker Application Container Engine.

Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.276336600Z" level=info msg="API listen on /run/docker.sock"

伟大!Docker 服务已启动并运行!

如果尚未启动,请运行以下命令以启动 Docker 服务。

$ sudo systemctl start docker

使 Docker 服务在每次重新启动时自动启动:

$ sudo systemctl enable docker

可以使用命令找到已安装的 Docker 版本:

$ sudo docker version

示例输出:

Client: Docker Engine - Community

 Version:           20.10.17

 API version:       1.41

 Go version:        go1.17.11

 Git commit:        100c701

 Built:             Mon Jun  6 23:02:46 2022

 OS/Arch:           linux/amd64

 Context:           default

 Experimental:      true

Server: Docker Engine - Community

 Engine:

  Version:          20.10.17

  API version:      1.41 (minimum version 1.12)

  Go version:       go1.17.11

  Git commit:       a89b842

  Built:            Mon Jun  6 23:00:51 2022

  OS/Arch:          linux/amd64

  Experimental:     false

 containerd:

  Version:          1.6.6

  GitCommit:        10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1

 runc:

  Version:          1.1.2

  GitCommit:        v1.1.2-0-ga916309

 docker-init:

  Version:          0.19.0

  GitCommit:        de40ad0

4. 测试 Docker

让我们继续,测试Docker是否正常工作。

为此,请运行:

$ sudo docker run hello-world

上面的命令将下载一个测试 Docker 映像,并在容器内执行一个示例hello_world程序。

如果您看到如下所示的输出,那么恭喜您!Docker 在我们的 Ubuntu 系统中运行良好。

Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

2db29710123e: Pull complete 

Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651

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 已准备就绪,可供使用。

你可能感兴趣的:(docker,服务器)