前置准备
使用自有或购买一台Ubuntu云服务器
阿里云服务器配置指南:【Linux系统实例快速入门】
注意选择ubuntu镜像初始化服务器
Ubuntu 安装docker
示例Ubuntu服务器版本: Ubuntu 20.04 LTS (GNU/Linux 5.4.0-139-generic x86\_64)
移除旧版本 docker
sudo apt-get remove docker docker-engine docker.io containerd runc
如果没有安装过docker则输出如下所示
Hit:1 http://mirrors.tencentyun.com/ubuntu focal InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu focal-security InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu focal-updates InRelease
Reading package lists... Done
使用apt repository 安装
更新apt包索引
sudo apt-get update
安装依赖包
sudo apt-get install ca-certificates curl gnupg
添加Docker的官方GPG密钥
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
设置docker存储库
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装 Docker Engine
更新apt包索引
sudo apt-get update
安装最新版本的docker和docker compose
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
验证docker engine是否安装成功
sudo docker run hello-world
输出如下所示
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:fc6cf906cbfa013e80938cdf0bb199fbdbb86d6e3e013783e5a766f50f5dbce0
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 版本
sudo docker version
输出如下所示
Client: Docker Engine - Community
Version: 23.0.6
API version: 1.42
Go version: go1.19.9
Git commit: ef23cbc
Built: Fri May 5 21:18:22 2023
OS/Arch: linux/amd64
Context: default
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied
ubuntu@VM-2-6-ubuntu:~$ sudo docker version
Client: Docker Engine - Community
Version: 23.0.6
API version: 1.42
Go version: go1.19.9
Git commit: ef23cbc
Built: Fri May 5 21:18:22 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 23.0.6
API version: 1.42 (minimum version 1.12)
Go version: go1.19.9
Git commit: 9dbdbd4
Built: Fri May 5 21:18:22 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit: v1.1.7-0-g860f061
docker-init:
Version: 0.19.0
GitCommit: de40ad0
sudo docker compose version
如下所示
Docker Compose version v2.17.3
配置非root管理docker
创建docker组
sudo groupadd docker
添加当前用户到docker组
sudo usermod -aG docker $USER
激活对组的更改
newgrp docker
验证不使用sudo 运行docker 命令
docker images
输出如下所示
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 9 days ago 13.3kB
配置开机自启动
sudo systemctl enable docker.service
sudo systemctl enable containerd.service