最详细的Ubuntu安装与配置Docker教程

Docker是一种流行的容器化平台,它能够简化应用程序的部署和管理。本文将介绍在Ubuntu操作系统上安装Docker的步骤,以便我们可以开始使用Docker来构建和运行容器化应用程序。

系统版本

本文以Ubuntu20.05系统为例安装docker,Ubuntu官方下载地址

检查卸载老版本docker

ubuntu下自带了docker的库,不需要添加新的源。
但是ubuntu自带的docker版本太低,需要先卸载旧的再安装新的。

一、安装Docker

注:docker的旧版本不一定被称为docker,http://docker.io 或 docker-engine也有可能,所以我们卸载的命令为:

  1. 查看是否已经安装docker
docker version

(移除旧版本)

sudo apt-get remove docker docker-engine docker.io containerd runc
  1. 更新软件列表
sudo apt-get update
  1. 允许apt命令可以使用HTTP访问Docker repository
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
  1. 添加docker官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

返回 OK
验证key
清华源

add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

或者阿里源

add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
  1. 更新源
sudo apt-get update
  1. 安装docker-ce软件
sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. 重启docker
service docker restart
  1. 测试
sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
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/

9. 查看docker版本

sudo docker version
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:08:01 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:08:01 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.26
  GitCommit:        3dd1e886e55dd695541fdcd67420c2888645a495
 runc:
  Version:          1.1.10
  GitCommit:        v1.1.10-0-g18a0cb0
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
  1. 查看镜像,可以看到刚才创建的镜像
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 7 months ago 13.3kB

二、 卸载Docker

  1. 卸载Docker CE安装包
sudo apt-get purge docker-ce
  1. 删除相关目录和配置文件
rm -rf /var/lib/docker $ rm -rf /etc/docker

三、 命令补全

通过bash_complete,docker提供自动补全功能,在执行该命令时,按tab即可自动补全参数,提高命令输入效率

  1. 安装bash_completion
apt install bash-completion
  1. 加载bash_completion
source /etc/bash_completion

四、更改默认存储位置

  1. 查看docker数据存储路径
sudo docker info
  1. 查看docker数据占用的存储空间(-v参数是详细列出)
sudo docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          1         1         13.26kB   0B (0%)
Containers      1         0         0B        0B
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B
sudo docker system df -v
Images space usage:

REPOSITORY    TAG       IMAGE ID       CREATED        SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
hello-world   latest    d2c94e258dcb   7 months ago   13.3kB    0B            13.26kB       1

Containers space usage:

CONTAINER ID   IMAGE         COMMAND    LOCAL VOLUMES   SIZE      CREATED          STATUS                      NAMES
deeef9008e1b   hello-world   "/hello"   0               0B        28 minutes ago   Exited (0) 28 minutes ago   quirky_sanderson

Local Volumes space usage:

VOLUME NAME   LINKS     SIZE

Build cache usage: 0B

CACHE ID   CACHE TYPE   SIZE      CREATED   LAST USED   USAGE     SHARED

3. 新建配置文件并输入以下内容

sudo vim /etc/docker/daemon.json
{
"data-root": "/home/xxx/docker", # 新的路径
"registry-mirrors": [
	"http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"]
}
  1. 将原来docker中的数据复制到新的存储目录下
sudo cp -r /var/lib/docker /home/xxx/docker
  1. 重启Docker
sudo systemctl restart docker
  1. 查看image信息
docker images
  1. 删除之前的数据
rm -rf /var/lib/docker

五、拉取镜像

!官方镜像地址!:https://hub.docker.com/search?q=

docker pull openvino/ubuntu18_dev
using default tag:latest
latest: Pulling from openvino/ubuntu18_dev
726b8a513d66:Pulling fs layer
0f56be5e847f: pull complete
f957f773fc62: Pull complete
f722a4ecdd54: Pull complete
6b060e8bd29c:Pull complete
40b9e5ee4b53:Pull complete
a365951389a1: Pull complete
bd7c374e5019: Pull complete
c2ab551aadf8: Pull complete
05d8d25ca7b2: Pull complete
ffaeef3258cb: Pull complete
ed8677b27775:Pull complete
ef839f01f9e6: Pull complete
39540179535f:Pull complete
621ba107416a: Pull complete
b4e66e18204d:Pull complete
cc51904aeed8: Pull complete
790ce7f99b38:Pull complete
ec28e21ce553:Pull complete
87050f54d747:Pull complete
17a9417b599e:Pull complete
955c554af1bf: Pull complete
5dd2794e2c8e: pull complete
C66277640e81:Pull complete
Digest: sha256:6a13b6ef271e949a1853ff633e33c8d60fed2d2e42b81b966ad6c4ea1dab7a66
Status: Downloaded newer image for openvino/ubuntu18_dev:latest

六、国内镜像

1.Docker中国区官方镜像
https://registry.docker-cn.com

2.网易
http://hub-mirror.c.163.com

3.中科大
https://docker.mirrors.ustc.edu.cn