一:命令安装

1. 卸载旧版本

$ sudo apt-get remove docker docker-engine docker.io containerd runc

2. 设置存储库

1.更新apt包索引并安装包以允许apt通过 HTTPS 使用存储库

$ sudo apt-get update

$ sudo apt-get install \

ca-certificates \

curl \

gnupg \

lsb-release

2.添加 Docker 的官方 GPG 密钥:

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

3.使用以下命令设置稳定存储库

$ 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

3. 安装 Docker 引擎

$ sudo apt-get update

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

4. 测试

$ sudo docker run hello-world

输出以下结果则成功:

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/

5. 国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。

[docker镜像加速](https://www.runoob.com/docker/docker-mirror-acceleration.html)

二:二进制安装

1. [docker二进制包下载](https://download.docker.com/linux/static/stable)

wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.4.tgz

2.解压

tar zxvf docker-20.10.4.tgz

3.将docker解压下的所有文件复制(或者移动)到

sudo mv docker/* /usr/local/bin/

4. 编写docker service服务

vim /etc/systemd/system/docker.service

[Unit]

Description=Docker Application Container Engine

Documentation=https://docs.docker.com

After=network-online.target firewalld.service

Wants=network-online.target

[Service]

Type=notify

# the default is not to use systemd for cgroups because the delegate issues still

# exists and systemd currently does not support the cgroup feature set required

# for containers run by docker

ExecStart=/usr/local/bin/dockerd --selinux-enabled=false

ExecReload=/bin/kill -s HUP $MAINPID

# Having non-zero Limit*s causes performance problems due to accounting overhead

# in the kernel. We recommend using cgroups to do container-local accounting.

LimitNOFILE=infinity

LimitNPROC=infinity

LimitCORE=infinity

# Uncomment TasksMax if your systemd version supports it.

# Only systemd 226 and above support this version.

#TasksMax=infinity

TimeoutStartSec=0

# set delegate yes so that systemd does not reset the cgroups of docker containers

Delegate=yes

# kill only the docker process, not all processes in the cgroup

KillMode=process

# restart the docker process if it exits prematurely

Restart=on-failure

StartLimitBurst=3

StartLimitInterval=60s

[Install]

WantedBy=multi-user.target

5. 给docker.service文件赋予可执行的权限

chmod +x /etc/systemd/system/docker.service

6. 相关命令

systemctl daemon-reload 重新加载配置文件

systemctl start docker 开启docker服务

systemctl enable docker.service 开机自动重启

systemctl status docker 查看状态

docker -v 查看安装信息