# 介绍

Docker 是一个开放源代码的开放平台软件,用于开发应用、交付应用和运行应用。
Docker 容器与虚拟机类似,但二者在原理上不同。容器是将操作系统层虚拟化,虚拟机则是虚拟化硬件,因此容器更具有便携性、更能高效地利用服务器。

# 安装 Docker

终端执行以下命令添加 Docker 官方 GPG 密钥、库,并更新 Ubuntu 源列表

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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
sudo apt update

安装最新 Docker CE

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

更新系统源列表

sudo apt-get update

脚本安装

sudo curl -sSL https://get.docker.com | sh

# 查看 Docker 信息

查看 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 Sat 2023-08-05 00:59:47 CST; 1min 3s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 37944 (dockerd)
      Tasks: 9
     Memory: 26.7M
        CPU: 185ms
     CGroup: /system.slice/docker.service
             └─37944 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
8月 05 00:59:47 Ubuntu systemd[1]: Starting Docker Application Container Engine...
8月 05 00:59:47 Ubuntu dockerd[37944]: time="2023-08-05T00:59:47.672379676+08:00" level=info msg="Starting up"
8月 05 00:59:47 Ubuntu dockerd[37944]: time="2023-08-05T00:59:47.673189117+08:00" level=info msg="detected 127.0.0.53 n>
8月 05 00:59:47 Ubuntu dockerd[37944]: time="2023-08-05T00:59:47.732504712+08:00" level=info msg="Loading containers: s>
8月 05 00:59:47 Ubuntu dockerd[37944]: time="2023-08-05T00:59:47.888476232+08:00" level=info msg="Loading containers: d>
8月 05 00:59:47 Ubuntu dockerd[37944]: time="2023-08-05T00:59:47.904123857+08:00" level=info msg="Docker daemon" commit>
8月 05 00:59:47 Ubuntu dockerd[37944]: time="2023-08-05T00:59:47.904179813+08:00" level=info msg="Daemon has completed >
8月 05 00:59:47 Ubuntu dockerd[37944]: time="2023-08-05T00:59:47.929405964+08:00" level=info msg="API listen on /run/do>
8月 05 00:59:47 Ubuntu systemd[1]: Started Docker Application Container Engine.

使用 systemctl 自启 Docker 服务

sudo systemctl enable docker

查看 Docker 版本

sudo docker version

输出样例如下

Client: Docker Engine - Community
 Version:           24.0.5
 API version:       1.43
 Go version:        go1.20.6
 Git commit:        ced0996
 Built:             Fri Jul 21 20:35:18 2023
 OS/Arch:           linux/amd64
 Context:           default
Server: Docker Engine - Community
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.6
  Git commit:       a61e2b4
  Built:            Fri Jul 21 20:35:18 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.22
  GitCommit:        8165feabfdfe38c65b599c4993d227328c231fca
 runc:
  Version:          1.1.8
  GitCommit:        v1.1.8-0-g82f18fe
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

运行样例程序

sudo docker run hello-world

输出样例如下

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:926fac19d22aa2d60f1a276b66a20eb765fbeea2db5dbdaafeb456ad8ce81598
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 Compose

安装最新 Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

v2.20.2 可以替换为最新的版本号,不要忘记数字前的 v

赋予执行权限

sudo chmod +x /usr/local/bin/docker-compose

查看 Docker Compose 版本

docker-compose version

输出样例如下

Docker Compose version v2.20.2

# 安装 Portainer

安装最新 Portainer,通过图形界面管理本地 Dcoker。

sudo docker pull portainer/portainer

创建 Portainer 容器

sudo docker volume create portainer_data

运行 Portainer

sudo docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

可以通过 http://ip:port 登录 Portainer 界面,例如 http://192.168.1.20:9000

更新于