Docker Run
基于镜像创建并运行新的容器
代码语言:shell复制$ docker run -p 80:80 -d --restart=always --name nginx nginx:1.19.10
Unable to find image 'nginx:1.19.10' locally
1.19.10: Pulling from library/nginx
fcad0c936ea5: Pull complete
d5c473a8294d: Pull complete
8e31422295d0: Pull complete
209e8d4861a8: Pull complete
3314b286b1b3: Pull complete
574020532afe: Pull complete
Digest: sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
Status: Downloaded newer image for nginx:1.19.10
c1e3fca4ca4a37c83b56164bb48c2704b79c3a9f6362d541dbb6768980c78cb0
参数:
- -p:端口映射,宿主机端口:容器内部端口
- -d:后台运行
- -- restart:容器重启策略,包含3个选项:no,on-failure:times,always,unless-stopped
- -- name:容器名称
- nginx:1.19.10:镜像名称:命令版本号
- -v:本参数用于指定容器卷
Docker Ps
查看正在运行的容器
代码语言:shell复制$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1e3fca4ca4a nginx:1.19.10 "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp nginx
参数:
- -a:命令是用来展示所有所有的容器,包括未运行的容器
Docker Images
查看可用的镜像
代码语言:shell复制$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.19.10 5aa5901ac9c7 2 years ago 126MB
Docker Pull
下载容器镜像
代码语言:shell复制$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
1817bc1e6309: Pull complete
740bd54462bc: Pull complete
a7e5ed4e69b3: Pull complete
8fdf88d7bbb7: Pull complete
a1a5f8560950: Pull complete
6d2014e71aa1: Pull complete
a65eef362898: Pull complete
1086465c0f61: Pull complete
6d718a732c3a: Pull complete
c724fd4b412f: Pull complete
74fcbdd515b3: Pull complete
Digest: sha256:51c4dc55d3abf4517a5a652794d1f0adb2f2ed1d1bedc847d6132d91cdb2ebbf
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
Docker Build
基于Dockerfile构建镜像
代码语言:shell复制$ docker build -t < image_name > < path_to_dockerfile >
Docker Stop
停止正在运行的容器
代码语言:shell复制$ docker stop nginx
nginx
Docker Start
启用一个停止的容器
代码语言:shell复制$ docker start nginx
nginx
Docker Stop
重新启动正在运行的容器
代码语言:shell复制$ docker restart nginx
nginx
Docker Rm
删除已停止的容器
代码语言:shell复制$ docker rm nginx
nginx
Docker Rmi
删除镜像
代码语言:shell复制$ docker rmi 5aa5901ac9c7
Untagged: nginx:1.19.10
Untagged: nginx@sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
Deleted: sha256:5aa5901ac9c75b399796aacfd0113e2f04c0d58b87ba5057da539d5db91eb52b
Deleted: sha256:e48a726b96dc3a89a57cd69e34b102c0327040dec7c53c6e294d7c17945ebe6c
Deleted: sha256:bc4342d853f3c5272a21e8cdfc2490a35fc9e32dc858a656ee903392d4783e4e
Deleted: sha256:002a6c87f9c8025acaac9a303dcdfc77b1d4b4a41510f93da1aa9b7b2910218d
Deleted: sha256:d9f0cadb7a4d62e901ebd720a75cd2ba7abaacfe7d6131a7103e0f6e59998610
Deleted: sha256:afbad4369b35c33f8f12849ebdddc9eb040be25d4997d8094f0d49c20668bc2c
Deleted: sha256:388ba420b88d4b92c45f4f822e152056f8df29f17937b54f52abfd652563df6b
参数:
- 5aa5901ac9c7:镜像ID
Docker Exec
在运行中的容器内执行命令
代码语言:shell复制$ docker exec nginx pwd
/
参数:
- nginx:容器名称
- pwd:命令Docker Cp
在容器与宿主机之间复制文件与目录
代码语言:shell复制# 从容器Copy到宿主机
$ docker cp nginx:/etc/nginx/nginx.conf .
Successfully copied 2.56kB to /Users/chuanzhang/.
$ ls | grep nginx
nginx.conf
# 从宿主机Copy到容器
$ touch test # 创建test文件
$ docker cp test nginx:/opt
Successfully copied 1.54kB to nginx:/opt
$ docker exec nginx ls -l /opt/ # 通过命令确认copy到test文件
total 0
-rw-r--r-- 1 501 dialout 0 Jul 30 11:12 test
Docker logs
查看容器的日志
代码语言:shell复制$ docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
Docker Inspect
查看容器详情
代码语言:shell复制$ docker inspect nginx
[
{
"Id": "1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0",
"Created": "2023-07-30T11:09:17.459773916Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11471,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-07-30T11:09:17.679558583Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5aa5901ac9c75b399796aacfd0113e2f04c0d58b87ba5057da539d5db91eb52b",
"ResolvConfPath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/hostname",
"HostsPath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/hosts",
"LogPath": "/var/lib/docker/containers/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0/1b18695a0cba1e9b868e8d00d9bed6159575fd9aa572fafc8efcb94ef03b2ad0-json.log",
"Name": "/nginx",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"RestartPolicy": {
"Name": "always",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
36,
200
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": null,
"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/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b-init/diff:/var/lib/docker/overlay2/b63891a8d0cd62734f73a35d761ddd3393cb7d1a2ff9e4a3d4374861af3bc8c2/diff:/var/lib/docker/overlay2/dd62dde50a162103d7086505dc8840ddae32e619b85ee94fbaa50ec74bee8690/diff:/var/lib/docker/overlay2/9a38961a92062a9266fd00d3f547374e3fed7e930de9ac1206409216e425928f/diff:/var/lib/docker/overlay2/478a51c0e9b4049e18b87a6149ac552233d7bd3508da6b302edb607c6ddbd1b0/diff:/var/lib/docker/overlay2/e4a629b33969b511c39de7795af158de44b4a478d3cfbcd5e0bf1d71affa3f59/diff:/var/lib/docker/overlay2/27d9fd58215a2d235aeaaf2eab6124b871ac8871b0b45efd529dbf3f425d40b5/diff",
"MergedDir": "/var/lib/docker/overlay2/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b/merged",
"UpperDir": "/var/lib/docker/overlay2/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b/diff",
"WorkDir": "/var/lib/docker/overlay2/89dd504f61449497682358a404f447c3a822532a22a02bcdabafe7316d6c690b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "1b18695a0cba",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.19.10",
"NJS_VERSION=0.5.3",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx:1.19.10",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers u003cdocker-maint@nginx.comu003e"
},
"StopSignal": "SIGQUIT"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "5b98bf99514ab4c13d6bc93ba1d0710079ad3e0be8b982fa13bae6624e3de094",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/5b98bf99514a",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "96b8755912b23ea85ab28a6abe8a7141aca735bb7a1215bf8e85c0cd1852f369",
"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": "f72918adb5183fc2895dee8c4d8cb5bdbedaa29df3426464cd2889fd993fe1c1",
"EndpointID": "96b8755912b23ea85ab28a6abe8a7141aca735bb7a1215bf8e85c0cd1852f369",
"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 Network Create
创建Docker网络
代码语言:shell复制$ docker network create <网络名称>
Docker Network Connect
将容器链接到指定的网络
代码语言:shell复制$ docker network connect <网络名称> <容器>