Docker安裝與基本命令使用
1. 解除安裝舊版本
Docker在CentOS上的安裝
官方文件:https://docs.docker.com/engine/install/centos/
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.下載需要的安裝包
sudo yum install -y yum-utils
3. 設定yum映象倉庫
預設是國外的:
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
其他映象源
映象加速器 | 映象加速器地址 |
---|---|
Docker 中國官方映象 | https://registry.docker-cn.com |
DaoCloud 映象站 | http://f1361db2.m.daocloud.io |
Azure 中國映象 | https://dockerhub.azk8s.cn |
科大映象站 | https://docker.mirrors.ustc.edu.cn |
阿里雲 | http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo |
七牛雲 | https://reg-mirror.qiniu.com |
網易雲 | https://hub-mirror.c.163.com |
騰訊雲 | https://mirror.ccs.tencentyun.com |
這裡使用阿里映象
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4. 更新yum軟體包索引
yum makecache fast
5. 開始安裝docker
sudo yum install docker-ce docker-ce-cli containerd.io
注:docker-ce 表示社群版,企業版為 docker-ee,分別下載docker的核心,客戶端,以及容器,這個都安裝最新版本。
其他版本
yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable指定版本安裝
yum install -y docker-ce-18.09.0-3.el7 docker-ce-cli-18.09.0-3.el7 containerd.io-1.2.0-3.el7
6. 啟動docker
sudo systemctl start docker
檢視如下表示啟動成功
[root@hadoop101 ~]# sudo systemctl start docker
[root@hadoop101 ~]# docker version
Client: Docker Engine - Community
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:45:41 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.12
Git commit: 459d0df
Built: Mon Dec 13 11:44:05 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.13
GitCommit: 9cc61520f4cd876b86e77edfeb88fbcd536d1f9d
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0
7. 測試
sudo docker run hello-world
[root@hadoop101 ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
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/
[root@hadoop101 ~]#
表示執行成功。
8. docker解除安裝
解除安裝依賴
sudo yum remove docker-ce docker-ce-cli containerd.io
刪除資源
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
登入阿里控制檯,在所有產品與服務中找到 容器映象服務
在映象工具下有個映象加速器:
4個配置步驟如下:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://d5o9x66u.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
10.docker常用命令
幫助命令
docker version
docker info
docker -help
https://docs.docker.com/engine/reference/run/
映象命令
docker images 檢視主機上所有映象
可選性
-a, --all #顯示所有主機上的映象
-q, --quiet #只顯示映象id
[root@hadoop101 ~]# docker images -aq
feb5d9fea6a5
[root@hadoop101 ~]#
docker search 搜尋映象
docker search mysql -f=STARS=5000
[root@hadoop101 ~]# docker search mysql -f=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12213 [OK]
mariadb MariaDB Server is a high performing open sou… 4689 [OK]
docker pull 下載映象
預設下載,不指定版本,則下載最新版本
docker pull mysql
[root@hadoop101 ~]# docker pull mysql
Using default tag: latest #預設最新版本
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #docker分層下載
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #簽名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真實下載地址
上面真實下載地址和名稱mysql相同,如下兩個下載命令相同:
docker pull mysql
docker pull docker.io/library/mysql:latest
指定版本下載,要在官方文件中支援的版本中指定,最新支援版本如下
8.0.28-oracle, 8.0-oracle, 8-oracle, oracle
8.0.28, 8.0, 8, latest, 8.0.28-debian, 8.0-debian, 8-debian, debian
5.7.37-oracle, 5.7-oracle, 5-oracle
5.7.37, 5.7, 5, 5.7.37-debian, 5.7-debian, 5-debian
我們再下載一個5.7的版本
docker pull mysql:5.7
[root@hadoop101 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@hadoop101 ~]#
可以看到,在我們下載一個預設版本後,再下載一個指定版本,上面一些依賴是通用的,就提示已存在,這樣就極大節省了空間,並且兩個版本毫不影響。
此時我們在檢視映象時
[root@hadoop101 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 2 months ago 448MB
mysql latest 3218b38490ce 2 months ago 516MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
[root@hadoop101 ~]#
docker rmi 刪除映象
docker rmi -f c20987f18b13
:根據映象id刪除指定映象
docker rmi -f $(docker images -aq)
:刪除全部映象
容器命令
有了映象,才可以使用容器命令,我們先下載一個linux系統
docker pull centos
建立容器並啟動
文件:https://docs.docker.com/engine/reference/commandline/run/
docker run [可選引數] image
#可選引數
-- name="容器名字"
-d #後臺方式執行
-it #使用互動式執行,執行並進入容器
-p #小寫的p,指定容器埠,-p 8080:8080
-p ip:主機埠:容器埠
-p 主機埠:容器埠
-p 容器埠
容器埠
-P #大寫的p,隨機指定容器埠
執行一個容器,並進入容器
docker run -it centos /bin/bash
[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@655b77d11f20 /]#
可以看到,我們已經進入容器,並且容器的id為:655b77d11f20
退出使用 exit,表示容器停止並退出
Ctrl+p+q:容器不停止且退出
列出當前容器
docker ps
:列出當前正在執行的容器
docker ps [可選引數]
-a 列出所有容器,包括正在執行的和歷史執行的容器
-n 列出最近建立的容器
-q 列出容器編號
刪除容器
docker rm 7e9e7a1082f8
:刪除指定容器id,正在執行的無法刪除,-f 可強制刪除
docker rm -f $(docker ps -aq)
:刪除所有容器
docker ps -aq|xargs docker rm -f
:刪除所有容器
啟動和停止容器
docker start 容器id #啟動一個容器
docker restart 容器id #重啟一個容器
docker stop 容器id #停止一個容器
docker kill 容器id #強制停止一個容器
其他常用命令
後臺啟動容器
docker run -d centos
注意:這裡發現centos又停止了,沒有啟動起來。
這是docker的一個問題,docker容器使用後臺執行,就必須要有一個前臺程序,例如使用這個容器的一個應用,如果沒有,docker容器就會自動停止。
檢視日誌
docker logs -ft --tail 100 4b3bc9e6e95f
:
[root@hadoop101 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb1b1f3023a2 centos "/bin/bash" 12 seconds ago Up 11 seconds trusting_beaver
[root@hadoop101 ~]# docker logs -ft --tail 10 bb1b1f3023a2
這裡日誌為空,下面我們建立一個指令碼來顯示日誌,重新建立一個容器,然後執行指令碼
docker run -d centos /bin/sh -c "while true;do echo hello hahaha...;sleep 1;done"
[root@hadoop101 ~]# docker run -d centos /bin/sh -c "while true;do echo hello hahaha...;sleep 1;done"
62f18b85321a007b9e01c1cb237e9f4942b4d90dfae8ec22fceaa62ddb7de822
[root@hadoop101 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62f18b85321a centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds elastic_bose
[root@hadoop101 ~]# docker logs -ft --tail 10 62f18b85321a
2022-03-06T15:38:08.383093375Z hello hahaha...
2022-03-06T15:38:09.388922480Z hello hahaha...
2022-03-06T15:38:10.395572128Z hello hahaha...
2022-03-06T15:38:11.401655410Z hello hahaha...
2022-03-06T15:38:12.410696441Z hello hahaha...
2022-03-06T15:38:13.413753657Z hello hahaha...
2022-03-06T15:38:14.427307889Z hello hahaha...
2022-03-06T15:38:15.432224741Z hello hahaha...
檢視容器中的程序
docker top 630e00985bd2
[root@hadoop101 ~]# docker top 630e00985bd2
UID PID PPID C STIME TTY TIME CMD
root 2033 2013 0 22:21 pts/0 00:00:00 /bin/bash
[root@hadoop101 ~]#
檢視容器元資料資訊
docker inspect 630e00985bd2
顯示內容過長,不再展示。
進入正在執行的容器
方式一:
docker exec -it 630e00985bd2 /bin/bash
[root@hadoop101 ~]# docker exec -it 630e00985bd2 /bin/bash
[root@630e00985bd2 /]# ps
PID TTY TIME CMD
15 pts/1 00:00:00 bash
30 pts/1 00:00:00 ps
[root@630e00985bd2 /]# exit
exit
[root@hadoop101 ~]#
方式二:
docker attach 630e00985bd2
區別:docker exec
表示開啟一個新的終端,類似於命令列,可做常用操作,docker exec
進入正在執行的終端,有輸出會正常列印。
從容器內拷貝檔案到主機
格式:docker cp 容器id:容器內檔案目錄 主機目錄
docker cp fe721a2a3c06:/opt/test.txt /opt/
[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@fe721a2a3c06 /]# cd /opt
[root@fe721a2a3c06 opt]# touch test.txt
[root@fe721a2a3c06 opt]# (ctrl+p+q)
[root@hadoop101 ~]# docker cp fe721a2a3c06:/opt/test.txt /opt/
[root@hadoop101 ~]# cd /opt
[root@hadoop101 opt]# ll
total 4
drwx--x--x 4 root root 28 Mar 6 13:24 containerd
drwxr-xr-x. 23 zhangbao zhangbao 4096 Nov 14 00:12 module
drwxr-xr-x 3 root root 17 Nov 13 23:59 redis
drwxr-xr-x. 5 zhangbao zhangbao 286 Nov 14 00:00 software
-rw-r--r-- 1 root root 0 Mar 8 22:47 test.txt