Docker概述
hub.docker.io
docs.docker.io
Docker安装
Docker
aldsfjasfkjsd
Docker的常用命令
帮助命令
镜像命令
容器命令
docker pull centos
新建容器并启动
docker run [args] image
--name='Name' 容器名字 tomcat01, tomcat02...
-d 后台方式运行
-it 使用交互方式运行,并进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p 主机端口:容器端口(常用)
-p 容器端口
-p ip:主机端口:容器端口
-P 随机指定端口
[will@will ~]
列出所有的运行的容器
停止容器
exit
ctrl + p + q
删除容器
docker rm [container_id]
docker rm -f $(docker ps -aq)
docker ps -aq | xargs docker rm
启动和停止容器
docker start container_id
docker restart container_id
docker stop container_id
docker kill container_id
常用其他命令
后台启动容器
docker run -d centos
查看日志
docker logs
--tail
docker run -d centos -c "while true;do echo 'hello world'; sleep 2; done"
[will@will ~]$ docker run -d centos /bin/sh -c "while true;do echo "hello world" ;sleep 1;done"
ab5e232e8ac9e05ee46945da5a4a2d2055b306953653a3fa44558c90f6d39450
[will@will ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7d1d775a89f centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds pensive_agnesi
[will@will ~]$ docker logs -f -t --tail 10 a7d1d775a89f
2021-09-12T14:06:53.978067719Z hello world
2021-09-12T14:06:54.979931412Z hello world
2021-09-12T14:06:55.981915303Z hello world
2021-09-12T14:06:56.984199095Z hello world
2021-09-12T14:06:57.986561111Z hello world
2021-09-12T14:06:58.987869560Z hello world
2021-09-12T14:06:59.989511714Z hello world
2021-09-12T14:07:00.990942246Z hello world
2021-09-12T14:07:01.992878156Z hello world
2021-09-12T14:07:02.994963108Z hello world
2021-09-12T14:07:03.997359975Z hello world
2021-09-12T14:07:04.999525036Z hello world
2021-09-12T14:07:06.001806035Z hello world
查看容器中进程信息ps
docker top container_id
[will@will ~]$ docker start a7d1d775a89f
a7d1d775a89f
[will@will ~]$
[will@will ~]$
[will@will ~]$ docker top a7d1d775a89f
UID PID PPID C STIME TTY TIME
root 44527 44505 1 22:10 ? 00:00:00
root 44591 44527 0 22:10 ? 00:00:00
[will@will ~]$
查看镜像元数据
[will@will ~]$ docker inspect a7d1d775a89f
[
{
"Id": "a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33",
"Created": "2021-09-12T14:06:39.641849451Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo 'hello world' ;sleep 1;done"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2021-09-12T14:10:39.613653276Z",
"FinishedAt": "2021-09-12T14:11:54.47701037Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/hostname",
"HostsPath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/hosts",
"LogPath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33-json.log",
"Name": "/pensive_agnesi",
"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/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0-init/diff:/var/lib/docker/overlay2/e88c6a2fc23c05ec9c082eaee933e458bcc746d1db9820739e790862372c71b9/diff",
"MergedDir": "/var/lib/docker/overlay2/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0/merged",
"UpperDir": "/var/lib/docker/overlay2/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0/diff",
"WorkDir": "/var/lib/docker/overlay2/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "a7d1d775a89f",
"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 world' ;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": "3ad38981c09954a7773da16ef2c3c4c1aa08a65dbb25a44fc0e4ca239982ceab",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3ad38981c099",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "71a309b12f1d882f7a0077b54a2893de036d1bc97519bdb707ad1505585d0ce1",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的命令
docker exec container_id
[will@will ~]$ docker exec -it a7d1d775a89f /bin/bash
[root@a7d1d775a89f /]
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@a7d1d775a89f /]
docker attach container_id
docker exec container_id
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
小结
[will@will ~]$ docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/home/will/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/home/will/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/will/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/will/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.6.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.8.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes # 提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem # 从容器和宿主机相互拷贝指定文件或者目录
create Create a new container # 创建一个新的容器,同run,但不启动容器
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers # 停止容器
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE # 给源中的镜像打标签
top Display the running processes of a container # 查看容器中
unpause Unpause all processes within one or more containers # 取消暂停容器
update Update configuration of one or more containers
version Show the Docker version information # 查看docker版本号
wait Block until one or more containers stop, then print their exit codes # 截取容器停止时的退出状态值
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
作业练习
docker安装nginx
docker pull nginx
docker run -d --name nginx01 -p 3344:80 nginx
docker安装tomcat
docker pull tomcat:9.0.52-jdk8-corretto
docker run -it -rm tomcat:9.0.52-jdk16-corretto
docker run -d -p 8080:8080 --name tomat01 tomcat
# 进入容器
docker exec -it tomcat01 /bin/bash
部署es+kibana
docker run -d --name elasticsearch -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
|