https://www.bilibili.com/video/BV1og4y1q7M4?p=8&spm_id_from=pageDriver
文章目錄
- 1. 安裝 Docker
- 2. 底層原理
- 2.1 Docker 如何工作?
- 2.2 Docker 為什麼比 VM 快?
- 3. Docker 常用指令
- 3.1 鏡像指令
- 3.2 容器指令
- 鏡像:image,模闆,容器通過其建立容器服務;
- 容器:container,Docker 利用容器技術,獨立運作一個或一組應用,通過鏡像來建立;
- 倉庫:repository,存放鏡像的地方;
1. 安裝 Docker
ubuntu:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
第一次使用樣例:
jiaming@ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally # 1. 本機尋找鏡像未果
latest: Pulling from library/hello-world # 2. 下載下傳
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
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/
第二次使用樣例:
jiaming@ubuntu:~$ 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/
未找到可用容器的結果:
jiaming@ubuntu:/etc/docker$ sudo docker run sajidjsaidjihfdshfsdhusisjidshusdhfisdjijiaosdjasdjkbfdjkbjkgghs
Unable to find image 'sajidjsaidjihfdshfsdhusisjidshusdhfisdjijiaosdjasdjkbfdjkbjkgghs:latest' locally
docker: Error response from daemon: pull access denied for sajidjsaidjihfdshfsdhusisjidshusdhfisdjijiaosdjasdjkbfdjkbjkgghs, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
jiaming@ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 4 months ago 13.3kB
docker 預設資源路徑:
jiaming@ubuntu:~$ sudo ls /var/lib/docker/
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
解除安裝 docker:
sudo apt-get purge docker-ce
sudo rm -rf /var/lib/docker # 删除鏡像、容器、配置檔案等内容
鏡像加速:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ub3hbfb7.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2. 底層原理
2.1 Docker 如何工作?
Docker 是一個 C/S 結構,Docker 的守護程序運作在主機上(服務端),通過 Socket 從用戶端通路,DockerServer 接收到 Docker-Client 的指令,執行指令。
2.2 Docker 為什麼比 VM 快?
- Docker 比虛拟機有更少的抽象層;
- Docker 共用一個核心,省去引導過程,響應速度極快;
3. Docker 常用指令
jiaming@ubuntu:~$ sudo docker version
jiaming@ubuntu:~$ sudo docker info
jiaming@ubuntu:~$ sudo docker --help
指令說明文檔:https://docs.docker.com/engine/reference/commandline/docker/
3.1 鏡像指令
- 查找;
- 增加;
- 删除;
root@ubuntu:~# docker images
REPOSITORY(倉庫源) TAG(鏡像标簽) IMAGE ID(鏡像的 ID) CREATED(鏡像建立的時間) SIZE(鏡像的大小)
hello-world latest d1165f221234 4 months ago 13.3kB
root@ubuntu:~# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images) 列出所有鏡像
-q, --quiet Only show image IDs 隻顯示鏡像的ID
root@ubuntu:~# docker images -q
d1165f221234
root@ubuntu:~# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11113 [OK]
mariadb MariaDB Server is a high performing open sou… 4213 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 825 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88
# 常用選項
--filter=STARS=5000
root@ubuntu:~# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11113 [OK]
# 下載下傳鏡像 docker pull 鏡像名[:tag]
root@ubuntu:~# docker pull mysql
Using default tag: latest # 如果不寫tag,預設就是 latest
latest: Pulling from library/mysql
b4d181a07f80: Pull complete # 分層下載下傳,docker image 的核心,聯合檔案系統
a462b60610f5: Pull complete
578fafb77ab8: Pull complete
524046006037: Pull complete
d0cbe54c8855: Pull complete
aa18e05cc46d: Pull complete
32ca814c833f: Pull complete
9ecc8abdb7f5: Pull complete
ad042b682e0f: Pull complete
71d327c6bb78: Pull complete
165d1d10a3fa: Pull complete
2f40c47d0626: Pull complete
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b # 簽名資訊
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真實位址
# 等價
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下載下傳
root@ubuntu:~# docker pull mysql:5.7
5.7: Pulling from library/mysql
b4d181a07f80: Already exists # 無需重複下載下傳
a462b60610f5: Already exists
578fafb77ab8: Already exists
524046006037: Already exists
d0cbe54c8855: Already exists
aa18e05cc46d: Already exists
32ca814c833f: Already exists
52645b4af634: Pull complete
bca6a5b14385: Pull complete
309f36297c75: Pull complete
7d75cacde0f8: Pull complete
Digest: sha256:1a2f9cd257e75cc80e9118b303d1648366bc2049101449bf2c8d82b022ea86b7
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 09361feeb475 2 weeks ago 447MB
mysql latest 5c62e459e087 2 weeks ago 556MB
hello-world latest d1165f221234 4 months ago 13.3kB
root@ubuntu:~# docker rmi -f 鏡像id # 删除指定的鏡像
root@ubuntu:~# docker rmi -f 鏡像id 鏡像id 鏡像id 鏡像id # 删除多個鏡像
root@ubuntu:~# docker rmi -f $(docker images -aq) # 删除全部鏡像
3.2 容器指令
有了鏡像才能建立容器;
# 建立容器并啟動 docker run [options] image
# 參數說明
--name="Name" # 容器名字 tomcat01、tomcat02... 用來區分容器
-d # 背景方式運作
-i/-t # 使用互動方式運作,進入容器檢視内容
-p # 指定容器端口 -p 8080:8080
-p 主機端口:容器端口
-p 容器端口
容器端口
-P # 随機指定端口
# 啟動并進入容器
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 7 months ago 209MB
root@ubuntu:~# docker run -it centos /bin/bash
[root@f85583780089 /]# ls # 檢視容器内的centos
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 退出指令
[root@f85583780089 /]# exit
exit
# Ctrl+P+Q:容器不停止退出
# 列出所有運作的容器
docker ps 指令
-a # 列出目前正在運作的容器+帶出曆史運作過的容器
-n=? # 顯示最近建立的容器
-q # 隻顯示容器的編号
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@ubuntu:~# docker ps -a # 曆史運作過的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f85583780089 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago priceless_kirch
80163dfdc558 d1165f221234 "/hello" 38 minutes ago Exited (0) 38 minutes ago quirky_banach
cc8c663ebfe9 d1165f221234 "/hello" 6 days ago Exited (0) 6 days ago recursing_chandrasekhar
319906d13cce d1165f221234 "/hello" 6 days ago Exited (0) 6 days ago mystifying_wilson
docker rm 容器id # 如果沒有 -f 不能删除正在運作的容器
docker rm -f $(docker ps -aq) # 删除所有容器
# 啟動和停止容器的操作
docker start 容器id # 啟動容器
docker restart 容器id # 重新開機容器
docker stop 容器id # 停止容器
docker kill 容器id # 強制停止
# 背景啟動容器
docker run -d centos # 無 -it 參數
# 問題:docker ps, 發現 centos 停止了
# 容器使用背景運作,必須要有一個前台程序,否則會自動停止
# 檢視日志
root@ubuntu:~# docker run -d centos /bin/sh -c "while true; do echo "hello"; sleep 1;done"
dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc3ea01151ae centos "/bin/sh -c 'while t…" 5 seconds ago Up 4 seconds youthful_driscoll
root@ubuntu:~# docker logs -tf --tail 10 dc3ea01151ae # -tf 顯示日志 --tail numbers 要顯示的日志條數
2021-07-12T14:18:30.602377140Z hello
2021-07-12T14:18:31.605459945Z hello
2021-07-12T14:18:32.609451241Z hello
2021-07-12T14:18:33.612117664Z hello
2021-07-12T14:18:34.615939548Z hello
2021-07-12T14:18:35.619226781Z hello
2021-07-12T14:18:36.622397637Z hello
2021-07-12T14:18:37.625884377Z hello
2021-07-12T14:18:38.627026141Z hello
2021-07-12T14:18:39.632458006Z hello
2021-07-12T14:18:40.634034443Z hello
# 檢視容器中程序資訊 docker top 容器id
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc3ea01151ae centos "/bin/sh -c 'while t…" 3 minutes ago Up 3 minutes youthful_driscoll
root@ubuntu:~# docker top dc3ea01151ae
UID PID PPID C STIME TTY TIME CMD
root 78193 78171 0 22:18 ? 00:00:00 /bin/sh -c while true; do echo hello; sleep 1;done
# 檢視鏡像的中繼資料 docker inspect 容器id
root@ubuntu:~# docker inspect dc3ea01151ae
[
{
"Id": "dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27",
"Created": "2021-07-12T14:18:05.012922826Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true; do echo hello; sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 78193,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-07-12T14:18:05.53219259Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/hostname",
"HostsPath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/hosts",
"LogPath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27-json.log",
"Name": "/youthful_driscoll",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350-init/diff:/var/lib/docker/overlay2/e02bb848b82f04ac5607439ae38332836f1a9e0bce72eb3a52fd5068681303d5/diff",
"MergedDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350/merged",
"UpperDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350/diff",
"WorkDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "dc3ea01151ae",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true; do echo hello; sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ffa8d313c5198a5d2a4392aaa14a19a042978927947dd6d1e28da9e0e701635d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/ffa8d313c519",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "1c2b9d352d58c9ccfff44adbe40ac25df724c2e08582c18324bb25729811512d",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "71a935afb831cdd5873d6ed772edaed79c2a991227cccd7ae38496fc13488738",
"EndpointID": "1c2b9d352d58c9ccfff44adbe40ac25df724c2e08582c18324bb25729811512d",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
# 進入目前正在運作的容器 docker exec -it 容器id bashshell,開啟一個新的終端
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774bb5d55750 centos "/bin/sh -c 'while t…" 5 seconds ago Up 4 seconds eager_haibt
root@ubuntu:~# docker exec -it 774bb5d55750 /bin/bash
[root@774bb5d55750 /]#
# docker attach 容器id
進入容器正在執行的終端
# 容器拷貝檔案到主機 docker cp 容器id:容器内路徑 目的路徑
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@ubuntu:~# docker run -it centos /bin/bash
[root@6da63bb2efb5 /]# root@ubuntu:~#
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6da63bb2efb5 centos "/bin/bash" 16 seconds ago Up 15 seconds mystifying_joliot
# 進入容器内部
root@ubuntu:~# docker exec -it 6da63bb2efb5 /bin/bash
[root@6da63bb2efb5 /]# cd home/
# 在容器内部建立檔案
[root@6da63bb2efb5 home]# touch hello.txt
[root@6da63bb2efb5 home]# ls
hello.txt
[root@6da63bb2efb5 home]# exit
exit
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6da63bb2efb5 centos "/bin/bash" About a minute ago Up About a minute mystifying_joliot
# 将檔案拷貝到主機
root@ubuntu:~# docker cp 6da63bb2efb5:/home/hello.txt /home
root@ubuntu:~# cd /home/
root@ubuntu:/home# ls
hello.txt
# 拷貝是一個手動過程,可以使用 -v 卷技術實作同步