鏡像下載、功能變數名稱解析、時間同步請點擊 阿裡雲開源鏡像站 Docker概述 Docker學習鏈接 官網鏈接:Home - Docker Docker與虛擬機比較 虛擬化技術 容器技術不是模擬的一個完整的操作系統。 比較Docker 和虛擬機技術不同 傳統虛擬機,虛擬出一臺硬體,運行完整的操作系統。 容器 ...
鏡像下載、功能變數名稱解析、時間同步請點擊 阿裡雲開源鏡像站
Docker概述
Docker學習鏈接
官網鏈接:Home - Docker
Docker與虛擬機比較
虛擬化技術
容器技術不是模擬的一個完整的操作系統。
比較Docker 和虛擬機技術不同
- 傳統虛擬機,虛擬出一臺硬體,運行完整的操作系統。
- 容器內的應用直接運行在宿主機的內容,容器是沒有自己的內核的,也沒有虛擬我們的聽見
- 每個容器間相互隔離,每個容器內都有一個屬於自己的文件系統,互不影響。
使用Docker的好處
1、應用更快的交付和部署。
2、更便捷的升級和擴縮容
3、更簡單的系統運維
4、更高效的計算資源利用。
Docker安裝
Docker的基本名詞
鏡像(image):
一個模板,可以通過這個模板來創建容器服務。
容器(container):
獨立運行一個或者一組應用,通過鏡像來創建的。
啟動、停止、刪除
倉庫(repository):
存放鏡像的地方
倉庫分為:公有、私有
安裝Docker步驟
安裝環境檢查
# 系統內核是 3.10以上的
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
# 系統版本
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
[root@localhost ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
Docker安裝
幫助文檔:Docker Documentation
# 前期準備:
1、修改主機名
# hostnamectl set-hostname docker
# bash
# 2、關閉防火牆
# systemctl stop firewalld && systemctl disable firewalld
# systemctl status firewalld
# 3、關閉selinux
# setenforce 0
# getenforce
# sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config
cat /etc/selinux/config
# 4、配置網卡聯網
# cat /etc/sysconfig/network-scripts/ifcfg-ens32
# 5、配置阿裡雲鏡像
# cd /etc/yum.repos.d/ && mkdir bak && mv CentOS-* bak/
# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
# 6、生成本地緩存
# yum makecache fast
# 7、更新YUM源軟體包
# yum update -y
# yum安裝gcc相關環境(需要確保虛擬機可以上外網。)
yum install -y gcc && yum install -y gcc-c++
# 本實驗基於CentOS 7.9系統進行演示安裝。
# 1、卸載舊版本docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2、安裝需要的安裝包
# yum install -y yum-utils
# 3、設置鏡像
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo # 預設鏡像源是國外的,不推薦使用
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 推薦使用國內鏡像
# 更新YUM源軟體包索引
yum makecache fast
# 4、安裝docker docker-ce ee企業版
yum install docker-ce docker-ce-cli containerd.io
# 5、啟動Docker
systemctl start docker && systemctl enable docker
systemctl status docker
# 6、查看docker版本信息
docker version
# 7、測試,運行hello-world
docker run hello-world
# 8、查看hello-world鏡像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
安裝完成。
查看docker版本信息
運行hello-world結果
卸載Docker
# 卸載docker
1、卸載依賴
yum remove docker-ce docker-ce-cli containerd.io
2、刪除資源
rm -rf /var/lib/docker # docker的預設工作路徑
rm -rf /var/lib/containerd
配置阿裡雲鏡像加速
1、登錄阿裡雲平臺
2、選擇"容器鏡像服務"
3、配置使用
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://w2kavmmf.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
回顧hello-world運行流程
Docker底層工作原理
docker是一個C/S結構的系統,Docker的守護進程運行在主機上。通過Socket從客戶端訪問。
Docker Server接收 Docker-Client的指令。就會執行這個命令。
Docker為什麼比VM塊
1、Docker有著比虛擬機更少的抽象層
2、docker利用的是宿主機的內核,vn需要時Guest OS
所以說,創建一個容器的時候,docker不需要像虛擬機一樣重新載入一個操作系統內核,避免引導。
虛擬機是載入Guest OS。分鐘級別的,而Docker是利用宿主機的操作系統,省略了這個複雜的過程,秒級。
Docker常用命令
幫助命令
docker version # 顯示docker版本信息
docker info # 顯示docker系統信息
docker 命令 --help # 幫助命令
鏡像命令
docker images 查本地主機上的所有鏡像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
# 解釋:
REPOSITORY: 鏡像的倉庫源
TAG: 鏡像的標簽
IMAGE ID : 鏡像的ID
CREATED: 鏡像的創建時間
SIZE: 鏡像的大小
[root@docker ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
# 可選項
Options:
-a, --all # Show all images
-q, --quiet # Only show image IDs
docker search 搜索鏡像
[root@docker ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12283 [OK]
mariadb MariaDB Server is a high performing open sou… 4723 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 911 [OK]
[root@docker ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
# 可選項,通過收藏來過濾
[root@docker ~]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12283 [OK]
[root@docker ~]#
docker pull 下載鏡像
[root@docker ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
# 下載鏡像 docker pull 鏡像名[:tag]
[root@docker ~]# docker pull mysql
Using default tag: latest #如果不寫 tag,預設是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分層下載,docker image的核心 聯合文件系統
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
#這兩條命令等價
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下載
[root@docker ~]# 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@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 3 months ago 448MB
mysql latest 3218b38490ce 3 months ago 516MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
docker rmi 刪除鏡像
[root@docker ~]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
# 查詢鏡像,使用IMAGE ID刪除
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 3 months ago 448MB
mysql latest 3218b38490ce 3 months ago 516MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
[root@docker ~]# docker rmi c20987f18b13
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 3 months ago 516MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
[root@docker ~]#
# 刪除多個容器
docker rmi 容器id 容器id 容器id
# 刪除所有鏡像
[root@docker ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
Untagged: hello-world:latest
Untagged: hello-world@sha256:6d60b42fdd5a0aa8a718b5f2eab139868bb4fa9a03c9fe1a59ed4946317c4318
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
容器命令
說明: 有了鏡像才可以創建容器,Linux,下載一個centos鏡像來測試學習。
docker pull centos
[root@docker ~]# 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
新建容器並啟動
docker run [可選參數] image
# 參數說明
--name="Name" # 容器名字,用來區分容器
-d # 後臺啟動方式
-it # 使用交互方式運行,進入容器查看內容
-p # 指定容器的埠 -p 8080:8080
-p ip:主機埠:容器埠
-p 主機埠:容器埠(常用)
-p 容器埠
容器埠
-P # 隨機指定埠
# 測試
# 啟動併進入容器
docker -it centos /bin/bash
[root@docker ~]# docker run -it centos /bin/bash
[root@c7dfc970a91b /]# ls #查看容器centos,基礎版本,很多命令不完善
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@c7dfc970a91b /]# exit #從容器中退回主機
exit
列出所有運行的容器
# docker ps 命令
# 列出當前正在運行的容器
-a # 列出當前正在運行的容器,帶出歷史運行過的容器
-n=?# 顯示最近創建的容器
-q # 只顯示容器的編號
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7dfc970a91b centos "/bin/bash" About a minute ago Exited (0) 46 seconds ago frosty_swirles
3f4ea26f2030 feb5d9fea6a5 "/hello" 6 hours ago Exited (0) 6 hours ago flamboyant_zhukovsky
# 顯示所有鏡像編號
[root@docker ~]# docker ps -aq
c7dfc970a91b
3f4ea26f2030
退出容器
exit #直接容器停止並退出
Ctrl + P + Q #容器不停止退出
[root@docker ~]# docker run centos /bin/bash
[root@docker ~]# docker run -it centos /bin/bash
[root@e9e247efe0ac /]#
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9e247efe0ac centos "/bin/bash" 28 seconds ago Up 27 seconds priceless_merkle
[root@docker ~]#
刪除容器
docker rm 容器id # 刪除指定容器(不能刪除正在運行的容器)
docker rm —f $(docker ps -aq) # 刪除所有容器
docker ps -a -q | xargs docker rm # 刪除所有的容器
# 刪除正在運行的容器會報錯
[root@docker ~]# docker rm e9e247efe0ac
Error response from daemon: You cannot remove a running container e9e247efe0ac9f13ebabf0fc8c10f1d770d8c08617ad870e5d16d20a763fa62f. Stop the container before attempting removal or force remove
[root@docker ~]# docker ps -aq
e9e247efe0ac
c7dfc970a91b
3f4ea26f2030
[root@docker ~]# docker rm -f $(docker ps -aq)
e9e247efe0ac
c7dfc970a91b
3f4ea26f2030
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker ps -aq
[root@docker ~]#
啟動和停止容器的操作
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
313175cf8109 centos "/bin/bash" 44 seconds ago Exited (0) 6 seconds ago cool_kirch
[root@docker ~]# docker start 313175cf8109
313175cf8109
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
313175cf8109 centos "/bin/bash" 56 seconds ago Up 5 seconds cool_kirch
[root@docker ~]# docker stop 313175cf8109
313175cf8109
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#
常用其他命令
1、後臺啟動容器
# 命令docker run -d 鏡像名
[root@docker ~]# docker run -d centos
# 問題docker ps。發現CentOS停止了
# 常見的坑:docker 容器使用是後臺運行,就必須要有一個前臺進程,docker發現沒有應用,就會自動停止
# nginx,容器啟動後,發現自己沒有提供服務,就會立刻停止,就是沒有程式了
2、查看日誌
docker logs -f -t --tail 容器,沒有日誌
# 自己編寫一段shell腳本
[root@docker ~]# docker run -d centos /bin/bash -c "while true;do echo xybdiy;sleep 1;done"
63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63334f8d0ac3 centos "/bin/bash -c 'while…" 24 seconds ago Up 24 seconds ecstatic_albattani
# 顯示日誌
-tf # 顯示日誌
--tail number #要顯示日誌條數
[root@docker ~]# docker logs -tf --tail 10 63334f8d0ac3
2022-03-21T13:45:24.983694039Z xybdiy
2022-03-21T13:45:25.986523455Z xybdiy
2022-03-21T13:45:26.990955021Z xybdiy
2022-03-21T13:45:27.993041663Z xybdiy
2022-03-21T13:45:28.995840669Z xybdiy
2022-03-21T13:45:30.000526171Z xybdiy
2022-03-21T13:45:31.006629083Z xybdiy
2022-03-21T13:45:32.015123258Z xybdiy
2022-03-21T13:45:33.018435524Z xybdiy
2022-03-21T13:45:34.021714677Z xybdiy
[root@docker ~]#
3、查看容器進程命令信息ps
#命令 docker top 容器id
[root@docker ~]# docker top 63334f8d0ac3
UID PID PPID C STIME TTY TIME CMD
root 12114 12095 0 21:44 ? 00:00:00 /bin/bash -c while true;do echo xybdiy;sleep 1;done
root 12432 12114 0 21:48 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
[root@docker ~]#
4、查看鏡像的元數據
# 幫助命令
[root@docker ~]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
[root@docker ~]#
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63334f8d0ac3 centos "/bin/bash -c 'while…" 6 minutes ago Up 6 minutes ecstatic_albattani
d735fbaa4bc1 centos "/bin/bash" 17 minutes ago Exited (0) 17 minutes ago intelligent_carver
313175cf8109 centos "/bin/bash" 28 minutes ago Exited (0) 18 minutes ago cool_kirch
# 命令
docker inspect 容器id
# 測試
[root@docker ~]# docker inspect 63334f8d0ac3
[
{
"Id": "63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e",
"Created": "2022-03-21T13:44:35.503041384Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo xybdiy;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12114,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-03-21T13:44:35.767782143Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/hostname",
"HostsPath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/hosts",
"LogPath": "/var/lib/docker/containers/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e/63334f8d0ac3e755a2f1948c0d6efe2fab6b09af468d4c8092b704786d05e21e-json.log",
"Name": "/ecstatic_albattani",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"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/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83-init/diff:/var/lib/docker/overlay2/ab65f0c4d3420c4a290a7d9956c96631e583ddb7102a700e94fe19b2b80893a0/diff",
"MergedDir": "/var/lib/docker/overlay2/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83/merged",
"UpperDir": "/var/lib/docker/overlay2/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83/diff",
"WorkDir": "/var/lib/docker/overlay2/50c20807b96cc41f57ab05f38d45f30f317437db22e2547af7edeb625f04db83/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "63334f8d0ac3",
"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/bash",
"-c",
"while true;do echo xybdiy;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"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": "88a08479f6ec55f7df18c951d743189d245ff141fb0eab1800d0ed2485dd13f3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/88a08479f6ec",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "33d8d755edf77fc0294cfef8c351e9ff6ace5aef4fdd0923ab0be0f59aaf2485",
"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": "48c02734d850fe7a67cefc00923e4e72d5adc1ef05cdb8aab68373e7289fe4a7",
"EndpointID": "33d8d755edf77fc0294cfef8c351e9ff6ace5aef4fdd0923ab0be0f59aaf2485",
"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
}
}
}
}
]
[root@docker ~]#
5、進入當前正在運行的容器
# 通常容器都是使用後臺方式運行的,需要進入容器,需要進入容器,修改一些配置。
#命令
docker exec -it 容器id bashShell
# 測試
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63334f8d0ac3 centos "/bin/bash -c 'while…" 22 minutes ago Up 22 minutes ecstatic_albattani
[root@docker ~]# docker exec -it 63334f8d0ac3 /bin/bash
[root@63334f8d0ac3 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@63334f8d0ac3 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 13:44 ? 00:00:00 /bin/bash -c while true;do echo xybdiy;sleep 1;done
root 1362 0 0 14:07 pts/0 00:00:00 /bin/bash
root 1388 1 0 14:07 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 1389 1362 0 14:07 pts/0 00:00:00 ps -ef
[root@63334f8d0ac3 /]#
#命令
docker attach 容器id
# 測試
[root@docker ~]# docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63334f8d0ac3 centos "/bin/bash -c 'while…" 25 minutes ago Up 25 minutes ecstatic_albattani
[root@docker ~]# docker attach 63334f8d0ac3
正在執行當前的代碼
# docker exec # 進入容器後開啟一個新的終端,可以在裡面操作(常用操作)
# docker attach # 進入容器正在執行的終端,不啟動新的進程
6、從容器內拷貝文件到主機上
docker cp 容器id:容器內路徑 目的主機路徑
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 6 months ago 231MB
[root@docker ~]# docker run -it centos /bin/bash
[root@76539a2682cc /]#
[root@docker ~]# ls
anaconda-ks.cfg
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76539a2682cc centos "/bin/bash" 39 seconds ago Up 39 seconds laughing_morse
[root@docker ~]# docker attach 76539a2682cc
[root@76539a2682cc /]# cd home/
[root@76539a2682cc home]# ls
# 在容器內創建test.java鏡像
[root@76539a2682cc home]# touch test.java
[root@76539a2682cc home]# ls
test.java
[root@76539a2682cc home]# exit
exit
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76539a2682cc centos "/bin/bash" About a minute ago Exited (0) 33 seconds ago laughing_morse
# 將文件拷貝出來
[root@docker ~]# docker cp 76539a2682cc:/home/test.java /home/
[root@docker ~]# ll /home/
total 0
-rw-r--r--. 1 root root 0 Mar 21 22:19 test.java
# 拷貝是一個手動的過程,以後使用 -v 捲的技術,可以實現,自動同步。
......
本文轉自:https://blog.csdn.net/qq_45392321/article/details/123729513