介绍

如果你的网络不能访问外网,使用参考网址:https://yeasy.gitbook.io/docker_practice/install/ubuntu

如果可以访问外网参考:https://docs.docker.com/engine/install/ubuntu/

卸载旧的版本

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

apt安装

可使用官方安装脚本自动安装:

1
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

或者按以下步骤手动安装。

更新apt源

1
2
3
4
5
6
7
8
sudo apt-get update

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release

centos7设置开机自启动

1
systemctl enable docker

在系统中添加Docker的官方密钥

1
2
3
4
5
6
7
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


# echo \
# "deb [arch=amd64 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

添加Docker源,选择stable长期稳定版

1
2
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

查看可以安装的Docker版本

1
sudo apt-cache policy docker-ce

安装(ce表示社区版)

1
2
3
4
5
6
7
8
9
10
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
docker version

# docker-compose (可选安装)
sudo curl -L "https://github.com/docker/compose/releases/download/v2.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

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

docker-compose -v

权限设置

最好通过非 root 用户来使用 Docker。这时需要添加非 root 用户到本地 Docker Unix 组当中。

1
2
$ sudo usermod -aG docker pumpkin
$ cat /etc/group | grep docker

重启才会生效。

启动Docker服务

1
sudo systemctl start docker//wsl 不好使换成service

设置开机自启动docker

1
sudo systemctl enable docker

查看Docker是否开启,出现绿色圆点表示服务正常开启

1
sudo systemctl status docker

验证

1
docker run hello-world

打印以下消息并退出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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镜像加速器

1
2
3
4
5
6
7
8
9
sudo mkdir -p /etc/docker

vi /etc/docker/daemon.json
# 添加以下内容
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]

sudo systemctl daemon-reload
sudo systemctl restart docker

ubunut离线安装

  1. 下载离线包,网址:https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/
    离线安装docker需要下载3个包,containerd.io ,docker-ce-cli,docker-ce

    containerd.io_1.4.6-1_amd64.deb
    docker-ce_20.10.7_3-0_ubuntu-xenial_amd64.deb
    docker-ce-cli_20.10.7_3-0_ubuntu-xenial_amd64.deb

  2. 下载完毕后拷贝到ubuntu上用 dpkg 命令安装,先安装containerd.io 跟 docker-ce-cli,最后安装docker-ce,命令sudo dpkg -i xxxx.deb

    1
    2
    3
    sudo dpkg -i containerd.io_1.4.6-1_amd64.deb  
    sudo dpkg -i docker-ce-cli_20.10.7_3-0_ubuntu-xenial_amd64.deb
    sudo dpkg -i docker-ce_20.10.7_3-0_ubuntu-xenial_amd64.deb
  3. 离线安装docker-compose: https://github.com/docker/compose/releases,解压

    1
    2
    3
    4
    5
    6
    7
    8
    # 文件转移至/usr/local/bin目录
    mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

    # 添加权限
    chmod +x /usr/local/bin/docker-compose

    # 测试
    docker-compose -v