Ubuntu 22.04 安裝Docker環境

Xneat發表於2024-08-20

1. 安裝docker環境

1.1 檢視系統環境

[root@docker-21 ~]$ lsb_release -a ; uname -rs
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy
Linux 5.15.0-118-generic

1.2 安裝系統工具

root@docker-21:~# apt-get update
root@docker-21:~# apt-get -y install apt-transport-https ca-certificates curl software-properties-common

1.3 安裝GPG證書

[root@docker-21 ~]$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

1.4 寫入軟體源

[root@docker-21 ~]$ add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

1.5 更新並安裝Docker-CE

[root@docker-21 ~]$ apt-get -y update
[root@docker-21 ~]$ apt-get -y install docker-ce

# 預設安裝完成是啟動狀態
[root@docker-21 ~]$ systemctl status docker   
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-08-15 03:58:31 UTC; 32s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 20446 (dockerd)
      Tasks: 10
     Memory: 23.9M
        CPU: 356ms
     CGroup: /system.slice/docker.service
             └─20446 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Aug 15 03:58:30 docker-21 systemd[1]: Starting Docker Application Container Engine...
Aug 15 03:58:30 docker-21 dockerd[20446]: time="2024-08-15T03:58:30.660769686Z" level=info msg="Starting up"
Aug 15 03:58:30 docker-21 dockerd[20446]: time="2024-08-15T03:58:30.662849129Z" level=info msg="detected 127.0.0.53 nameserver, assuming systemd-resolved, so using resolv.conf: /run/sys>
Aug 15 03:58:30 docker-21 dockerd[20446]: time="2024-08-15T03:58:30.732675253Z" level=info msg="Loading containers: start."
Aug 15 03:58:31 docker-21 dockerd[20446]: time="2024-08-15T03:58:31.152087774Z" level=info msg="Loading containers: done."
Aug 15 03:58:31 docker-21 dockerd[20446]: time="2024-08-15T03:58:31.169099184Z" level=info msg="Docker daemon" commit=f9522e5 containerd-snapshotter=false storage-driver=overlay2 versio>
Aug 15 03:58:31 docker-21 dockerd[20446]: time="2024-08-15T03:58:31.169231597Z" level=info msg="Daemon has completed initialization"
Aug 15 03:58:31 docker-21 dockerd[20446]: time="2024-08-15T03:58:31.195845843Z" level=info msg="API listen on /run/docker.sock"
Aug 15 03:58:31 docker-21 systemd[1]: Started Docker Application Container Engine.


# 設定將docker為開啟自啟動
root@docker-21:~# systemctl enable docker            
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker

1.6 檢視docker的版本

[root@docker-21 ~]$ docker version
Client: Docker Engine - Community
 Version:           27.1.2
 API version:       1.46
 Go version:        go1.21.13
 Git commit:        d01f264
 Built:             Mon Aug 12 11:50:12 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.1.2
  API version:      1.46 (minimum version 1.24)
  Go version:       go1.21.13
  Git commit:       f9522e5
  Built:            Mon Aug 12 11:50:12 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.20
  GitCommit:        8fc6bcff51318944179630522a095cc9dbf9f353
 runc:
  Version:          1.1.13
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@docker-21 ~]$

2. 新增docker加速地址

1.由於受docker受國內影響,國內很多加速地址都下架了,目前還能使用的加速地址包括:
2.支援系統:Ubuntu 16.04+、Debian 8+、CentOS 7+:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
 {
    "registry-mirrors": [
        "https://hub.uuuadc.top",
        "https://docker.anyhub.us.kg",
        "https://dockerhub.jobcher.com",
        "https://dockerhub.icu",
        "https://docker.ckyl.me",
        "https://docker.awsl9527.cn"
    ]
}
EOF

# 重新載入生效
sudo systemctl daemon-reload
sudo systemctl restart docker

3. 下載docker映象

下載一個centos映象做測試

[root@docker-21 ~]$  docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest


# 檢視拉取的映象
[root@docker-21 ~]$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   2 years ago   231MB


# 基於剛剛下載的docker映象啟動一個容器
[root@docker-21 ~]$ docker run -d -it centos
7664cc7d2ba1f94547b4bc4afbfea92ec31b95094969cfc43bc9c8c034a32db2
[root@docker-21 ~]$
[root@docker-21 ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
7664cc7d2ba1   centos    "/bin/bash"   3 seconds ago   Up 3 seconds             tender_poincare

# 進入容器
[root@docker-21 ~]$ docker exec -it 7664cc7d2ba1 /bin/bash
[root@7664cc7d2ba1 /]# ls -l
total 48
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Aug 15 04:40 dev
drwxr-xr-x   1 root root 4096 Aug 15 04:40 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 295 root root    0 Aug 15 04:40 proc
dr-xr-x---   1 root root 4096 Aug 15 04:46 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Aug 15 04:40 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
[root@7664cc7d2ba1 /]exit    #退出


# 檢視已經建立的容器所在的預設路徑
[root@docker-21 ~]$ ll /var/lib/containerd/io.containerd.runtime.v2.task/moby/
total 12
drwx--x--x 3 root root 4096 Aug 15 04:40 ./
drwx--x--x 3 root root 4096 Aug 15 04:40 ../
drwx--x--x 2 root root 4096 Aug 15 04:40 7664cc7d2ba1f94547b4bc4afbfea92ec31b95094969cfc43bc9c8c034a32db2/

至此完成安裝docker。

相關文章