Docker
01day1.启动
sudo systemctl start docker
2.第一个运行hello-world
docker run hello-world
3.常用命令
docker version #显示dockered版本信息
docker info 显示docker的系统信息,包括镜像和容器
docker 命令 --help 帮助命令
镜像命令
** docker images** 查看本所有本地的主机上的镜像
```shell
-a 只显示镜像
-q 只显示镜像的ID
```
docker search 搜索镜像
root@fanwd-virtual-machine:/home/fanw# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10661 [OK]
mariadb MariaDB Server is a high performing open sou… 3997 [OK]
#可选项 通过收藏来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
root@fanwd-virtual-machine:/home/fanw # docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10661 [OK]
mariadb MariaDB Server is a high performing open sou… 3997 [OK]
docker pull 下载镜像
docker pull 镜像名字 [:tag] #:tag = :版本
#如果不写tag 就会默认为最新版本
docker rmi 删除镜像
root@fanwd-virtual-machine:/home/fanw# docker rmi -f 容器的id # 删除指定的容器
Untagged: hello-world:latest
Untagged: hello-world@sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
docker rmi -f $(docker images -aq) #删除全部的容器
容器的命令
安装centos
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数选项
--name="name" #容器名字 tomcat01
-d #后台方式运行
-it # 使用交互方式运行 进入容器查看内容
-p 指定容器的端口 -p 8080
# 测试启动centos
root@fanwd-virtual-machine:/home/fanw# docker run -it centos /bin/bash
[root@3a1371091a03 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@3a1371091a03 /]# exit 退出容器 并停止
exit
列出所有运行的容器
#docker ps
-n 显示最近创建的容器
root@fanwd-virtual-machine:/home/fanw# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@fanwd-virtual-machine:/home/fanw# docker ps -a 列出历史运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a1371091a03 centos "/bin/bash" 7 minutes ago Exited (0) About a minute ago funny_kirch
cc051cbed1b3 d1165f221234 "/hello" 2 hours ago Exited (0) 2 hours ago hungry_johnson
4e65b9117bfc d1165f221234 "/hello" 4 days ago Exited (0) 4 days ago nostalgic_wozniak
root@fanwd-virtual-machine:/home/fanw# docker ps -a -n=1 -n 显示最近创建的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a1371091a03 centos "/bin/bash" 9 minutes ago Exited (0) 3 minutes ago funny_kirch
删除容器
docker rmi +容器id # 删除指定的容器 不能删除正在运行的容器
docker rmi -f $(docker ps -aq) #删除所有运行的容器 强制删除
常用的其他命令
后台启动
#命令 docker run -d 镜像名
# 问题 docker ps 发现 docker 停止了
#常见的坑 :docker 容器使用后台运行 就必须 要有一个前台进程 ;docker 发现没有应用 就会自动停止
# nginx 容器启动后 发现自己没有提供服务 就会立刻停止 就是没有程序了
查看日志命令
docker logs -tf --tail 容器 #显示日志
查看容器中进程的信息
docker top 容器id
查看镜像的原数据
#docker inspect 容器id
#测试
root@fanwd-virtual-machine:/home/fanw# 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@fanwd-virtual-machine:/home# docker inspect 5ec3ed21d177
[
{
"Id": "5ec3ed21d177b26a713902970204e082ffcf0fabbcaa6281f300792e2dbad8d8",
"Created": "2021-03-25T12:49:51.518746453Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-25T12:49:51.954669921Z",
"FinishedAt": "2021-03-25T12:51:49.077982598Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/5ec3ed21d177b26a713902970204e082ffcf0fabbcaa6281f300792e2dbad8d8/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/5ec3ed21d177b26a713902970204e082ffcf0fabbcaa6281f300792e2dbad8d8/hostname",
"HostsPath": "/var/lib/docker/containers/5ec3ed21d177b26a713902970204e082ffcf0fabbcaa6281f300792e2dbad8d8/hosts",
"LogPath": "/var/lib/docker/containers/5ec3ed21d177b26a713902970204e082ffcf0fabbcaa6281f300792e2dbad8d8/5ec3ed21d177b26a713902970204e082ffcf0fabbcaa6281f300792e2dbad8d8-json.log",
"Name": "/dazzling_lamarr",
"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/7ce90c3c85533b247d0b2229fc965a8b45e921a2ced919675ec9c6621ae3eaf2-init/diff:/var/lib/docker/overlay2/2a92ef8a435ae42330c51806c81cb77c698e6864bd9dd1c261a6699e85aeb079/diff",
"MergedDir": "/var/lib/docker/overlay2/7ce90c3c85533b247d0b2229fc965a8b45e921a2ced919675ec9c6621ae3eaf2/merged",
"UpperDir": "/var/lib/docker/overlay2/7ce90c3c85533b247d0b2229fc965a8b45e921a2ced919675ec9c6621ae3eaf2/diff",
"WorkDir": "/var/lib/docker/overlay2/7ce90c3c85533b247d0b2229fc965a8b45e921a2ced919675ec9c6621ae3eaf2/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "5ec3ed21d177",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"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": "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": "dcf1f6b3ff09a12a8482acb3a1bac172858e2558662d8819f50c54678eb1c983",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/dcf1f6b3ff09",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "63084a70520b288a9a0aa1d220d739fc172e6314fdc45d15e6af7eba61622c2c",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
#容器通常都是使用后台方式运行的 需要进入容器 修改一些配置
# 命令
docker exec -it 容器id bashshell #进入容器后 可以开启一个新的终端 在里边操作(常用)
docker attach 容器id #进入容器正在执行的终端 不会启动新的终端
从容器内拷贝文件到主机上
docker cp 容器id :容器内路径 目的的主机路径
root@fanwd-virtual-machine:/home/fanw# docker run -it centos
[root@5ec3ed21d177 /]# root@fanwd-virtual-machine:/home/fanw# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ec3ed21d177 centos "/bin/bash" 20 seconds ago Up 19 seconds dazzling_lamarr
root@fanwd-virtual-machine:/home/fanw# cd /home
root@fanwd-virtual-machine:/home# touch xiaohei.java
root@fanwd-virtual-machine:/home# ls
fanw fanwd xiaohei.java
root@fanwd-virtual-machine:/home# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ec3ed21d177 centos "/bin/bash" About a minute ago Up About a minute dazzling_lamarr
root@fanwd-virtual-machine:/home# docker attach 5ec3ed21d177
[root@5ec3ed21d177 /]# cd /home
[root@5ec3ed21d177 home]# ls
[root@5ec3ed21d177 home]# touch test.java
[root@5ec3ed21d177 home]# exit
exit
root@fanwd-virtual-machine:/home# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ec3ed21d177 centos "/bin/bash" 2 minutes ago Exited (0) 7 seconds ago dazzling_lamarr
3a1371091a03 centos "/bin/bash" 52 minutes ago Exited (0) 45 minutes ago funny_kirch
cc051cbed1b3 d1165f221234 "/hello" 3 hours ago Exited (0) 3 hours ago hungry_johnson
4e65b9117bfc d1165f221234 "/hello" 4 days ago Exited (0) 4 days ago nostalgic_wozniak
#拷贝文件到目录
root@fanwd-virtual-machine:/home# docker cp 5ec3ed21d177:/home/test.java /home 容器内路径 目的的主机路径
root@fanwd-virtual-machine:/home# ls 查看当前主机目录
fanw fanwd test.java xiaohei.java