docker学习
环境准备
ubuntu20.04修改清华大学镜像源
备份
cp /etc/apt/sources.list /etc/apt/sources.list.bak
替换sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # 预发布软件源,不建议启用 # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
附:vim全选、复制、粘贴、删除
全选(高亮显示):按esc后,然后ggvG或者ggVG
复制
全部复制:按esc后,然后ggyG
复制多行
方法1:
- (1)将光标移动到要复制的文本开始的地方,按v进入可视模式。
- (2)将光标移动到要复制的文本的结束的地方,按y复制。此时vim会自动将光标定位到选中文本的开始的地方,并退出可视模式。
- (3)我移动光标到文本结束的地方,按p粘贴 复制多行
- 方法2: :9,15 copy 16 表示复制9~15行内容,并将复制好的内容放置到16行 选中内容后复制:y 复制选中内容到0号寄存器 +y 复制选中内容到+寄存器,也就是系统的剪贴板,供其他程序用
删除
全部删除:按esc后,然后dG选中内容后删除:d 删除选中内容
删除光标指向的行:dd
解析:
gg:是让光标移到首行,在vim才有效,vi中无效
v : 是进入Visual(可视)模式
G :光标移到最后一行
选中内容以后就可以其他的操作了,比如:
d 删除选中内容
y 复制选中内容到0号寄存器
"+y 复制选中内容到+寄存器,也就是系统的剪贴板,供其他程序用ctrl+insert复制,shift+insert粘贴。
执行更新列表
apt-get update
安装docker
卸载docker
卸载依赖
apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
删除目录
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
*配置阿里云镜像加速
流程图
常用命令
帮助命令
docker version #显示版本信息
docker info #显示docker的系统信息,包括容器和镜像的数量
docker --help #帮助命令
镜像命令
https://docs.docker.com/reference/
docker images
查看主机上的所有镜像
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 12 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a, --all 列出所有镜像
-q, --quiet 只显示镜像的id
docker search
搜索镜像
root@ubuntu:~# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13234 [OK]
mariadb MariaDB Server is a high performing open sou… 5062 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 640 [OK]
#可选项
-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
##
--filter=STARS=3000 #搜索收藏数大于3000的
docker pull
下载镜像
#下载镜像 docker pull 镜像名[:tag]
root@ubuntu:~# docker pull mysql
Using default tag: latest #如果不写tag,默认latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分层下载
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@ubuntu:~# 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
docker rmi
删除镜像
root@ubuntu:~# docker rmi -f 镜像id #删除指定的镜像
root@ubuntu:~# docker rmi -f 镜像id 镜像id 镜像id #删除多个镜像
root@ubuntu:~# docker rmi -f $(docker images -aq) #删除全部
容器命令
有了镜像才可以创建容器
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
-p 容器端口
-p 主机端口:容器端口(常用)
-p ip:主机端口:容器端口
-P 随机指定端口
#测试,启动并进入容器
root@ubuntu:~# docker run -it centos /bin/bash
[root@f594e498e092 /]# ls #查看容器内的centos
bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr
[root@f594e498e092 /]# exit
exit
root@ubuntu:~#
列出所有的运行的容器
# 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
f594e498e092 centos "/bin/bash" 11 minutes ago Exited (0) 8 minutes ago jolly_snyder
7c47b6864168 centos "-it /bin/bash" 11 minutes ago Created modest_mestorf
24ea98923772 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago lucid_borg
be37dc8282d2 feb5d9fea6a5 "/hello" 18 hours ago Exited (0) 18 hours ago dazzling_mclaren
381ae3a97911 feb5d9fea6a5 "/hello" 18 hours ago Exited (0) 18 hours ago elegant_newton
root@ubuntu:~#
退出容器
exit #停止容器并退出
ctrl+P+Q #不停止容器退出
删除容器
docker rm 容器id #删除指定容器,不能删除正在运行的容器 rm -f
docker rm -f $(docker ps -aq) #删除所有
启动和停止容器的操作
docker start 容器id #启动
docker restart 容器id #重启
docker stop 容器id #停止
docker kill 容器id #强制停止
常见其他命令
后台启动容器
# docker run -d 镜像名
docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
查看日志
docker logs [容器id]
-tf #显示日志
--tail number #要显示的日志条数
查看容器中进程信息
docker top 容器id
查看镜像的元数据
# docker inspect 容器id
root@ubuntu:~# docker inspect 2d7bf76fcb29
[
{
"Id": "2d7bf76fcb2915ce3df97184b3ac764375303ff647a26c96a6dc62325d5815e6",
"Created": "2022-10-11T08:24:30.680741405Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-10-11T08:24:31.086725667Z",
"FinishedAt": "2022-10-11T08:24:31.098066466Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/2d7bf76fcb2915ce3df97184b3ac764375303ff647a26c96a6dc62325d5815e6/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/2d7bf76fcb2915ce3df97184b3ac764375303ff647a26c96a6dc62325d5815e6/hostname",
"HostsPath": "/var/lib/docker/containers/2d7bf76fcb2915ce3df97184b3ac764375303ff647a26c96a6dc62325d5815e6/hosts",
"LogPath": "/var/lib/docker/containers/2d7bf76fcb2915ce3df97184b3ac764375303ff647a26c96a6dc62325d5815e6/2d7bf76fcb2915ce3df97184b3ac764375303ff647a26c96a6dc62325d5815e6-json.log",
"Name": "/stoic_cray",
"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/3aabcb474746e46e12dcf420129112dc02b479ff06393df2476c23f5fd437318-init/diff:/var/lib/docker/overlay2/44ce5087b11bcfedaace183290812f0b6743bf77db48ef251093f4c4f1f9af71/diff",
"MergedDir": "/var/lib/docker/overlay2/3aabcb474746e46e12dcf420129112dc02b479ff06393df2476c23f5fd437318/merged",
"UpperDir": "/var/lib/docker/overlay2/3aabcb474746e46e12dcf420129112dc02b479ff06393df2476c23f5fd437318/diff",
"WorkDir": "/var/lib/docker/overlay2/3aabcb474746e46e12dcf420129112dc02b479ff06393df2476c23f5fd437318/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "2d7bf76fcb29",
"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"
],
"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": "4a87138782c1442c2b92f31be2b585b7008d3fccff5bd0fe350f435d8802e263",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4a87138782c1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f901df6fde0cdb5050d24bf103aa93704be9191ee04ddfaca069375d20bafb81",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
docker exec -it 容器id bashShell
# 测试
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0811b0ec33c0 centos "/bin/bash" 42 seconds ago Up 42 seconds dazzling_bell
root@ubuntu:~# docker exec -it 0811b0ec33c0 /bin/bash
[root@0811b0ec33c0 /]#
docker attach 容器id
# 测试
root@ubuntu:~# docker attach 0811b0ec33c0
[root@0811b0ec33c0 /]# exit
区别: docker exec 进入容器后开启一个新的终端
docker attach 进入容器正在执行的终端
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机的路径
# 测试
root@ubuntu:~# docker run -it centos /bin/bash
[root@07bb290d1543 /]# cd /home/
[root@07bb290d1543 home]# touch test.java
[root@07bb290d1543 home]# ls
test.java
[root@07bb290d1543 home]# exit
exit
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
07bb290d1543 centos "/bin/bash" 25 seconds ago Exited (0) 7 seconds ago naughty_heyrovsky
bbe902fb38e2 centos "/bin/bash" 2 minutes ago Exited (0) 31 seconds ago trusting_aryabhata
974031e1b6b1 centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago silly_cartwright
0811b0ec33c0 centos "/bin/bash" 15 minutes ago Exited (0) 10 minutes ago dazzling_bell
89a3ca78ead2 centos "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago tender_cori
2d7bf76fcb29 centos "/bin/bash" 45 minutes ago Exited (0) 45 minutes ago stoic_cray
f594e498e092 centos "/bin/bash" About an hour ago Exited (0) About an hour ago jolly_snyder
7c47b6864168 centos "-it /bin/bash" About an hour ago Created modest_mestorf
24ea98923772 feb5d9fea6a5 "/hello" 3 hours ago Exited (0) 3 hours ago lucid_borg
be37dc8282d2 feb5d9fea6a5 "/hello" 19 hours ago Exited (0) 19 hours ago dazzling_mclaren
root@ubuntu:~# docker cp 07bb290d1543:/home/test.java /home/
root@ubuntu:~# cd /home/
root@ubuntu:/home# ls
longdaiquan test.java
小结
常用命令
attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像
build Build an image from a Dockerfile # 通过 Dockerfile 定 制镜像
commit Create a new image from a container changes # 提交当前容器为新的镜像
cp Copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中
create Create a new container # 创建一个新的容器,同 run,但不启动容器
diff Inspect changes on a container's filesystem # 查看 docker 容器变化
events Get real time events from the server # 从 docker 服务获取容 器实时事件
exec Run a command in an existing container # 在已存在的容器上运行命令
export Stream the contents of a container as a tar archive # 导出容器的内 容流作为一个 tar 归档文件[对应 import ]
history Show the history of an image # 展示一个镜像形成历史
images List images # 列出系统当前镜像
import Create a new filesystem image from the contents of a tarball # 从 tar包中的内容创建一个新的文件系统映像[对应export]
info Display system-wide information # 显示系统相关信息
inspect Return low-level information on a container # 查看容器详细信息
kill Kill a running container # kill 指定 docker 容 器
load Load an image from a tar archive # 从一个 tar 包中加载一 个镜像[对应 save]
login Register or Login to the docker registry server # 注册或者登陆一个docker源服务器
logout Log out from a Docker registry server # 从当前 Docker registry 退出
logs Fetch the logs of a container # 输出当前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口
pause Pause all processes within a container # 暂停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server # 从docker镜像源服务器拉取指定镜像或者库镜像
push Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器
restart Restart a running container # 重启运行的容器
rm Remove one or more containers # 移除一个或者多个容器
rmi Remove one or more images # 移除一个或多个镜像[无容器使用该 镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container # 创建一个新的容器并运行 一个命令
save Save an image to a tar archive # 保存一个镜像为一个 tar 包[对应 load]
search Search for an image on the Docker Hub # 在 docker hub 中搜 索镜像
start Start a stopped containers # 启动容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 给源中镜像打标签
top Lookup the running processes of a container # 查看容器中运行的进程信息
unpause Unpause a paused container # 取消暂停容器
version Show the docker version information # 查看 docker 版本号
wait Block until a container stops, then print its exit code # 截取容 器停止时的退出状态值

微信支付

支付宝支付