docker,需要明确的几个概念:
- container 容器
- image 镜像
- repository 仓库
repository 仓库,用于存储 image 镜像,container 容器是 image 的一个具体的实例化对象。
可以类比 java, docker repository 类似 maven repository; 镜像就是一个 java 类;容器就是java 类的一个具体对象(new object) ;
命令:docker container 管理容器命令
Usage: docker container COMMAND
PS C:\Users\DELL> docker container --helpUsage: docker container COMMANDManage containersCommands:attach Attach local standard input, output, and error streams to a running containercommit Create a new image from a container's changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemexec Run a command in a running containerexport Export a container's filesystem as a tar archiveinspect Display detailed information on one or more containerskill Kill one or more running containerslogs Fetch the logs of a containerls List containerspause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerprune Remove all stopped containersrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrun Run a command in a new containerstart Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containerswait Block until one or more containers stop, then print their exit codesRun 'docker container COMMAND --help' for more information on a command.
举例:docker container ls -a
PS C:\Users\DELL> docker container ls --helpUsage: docker container ls [OPTIONS]List containersAliases:ls, ps, listOptions:-a, --all Show all containers (default shows just running)-f, --filter filter Filter output based on conditions provided--format string Pretty-print containers using a Go template-n, --last int Show n last created containers (includes allstates) (default -1)-l, --latest Show the latest created container (includes allstates)--no-trunc Don't truncate output-q, --quiet Only display container IDs-s, --size Display total file sizesPS C:\Users\DELL> docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf5eb97c3c28b minio/minio "/usr/bin/docker-ent…" 3 hours ago Up 2 hours 0.0.0.0:9000->9000/tcp my_minio288ec942f1843 minio/minio "/usr/bin/docker-ent…" 3 hours ago Exited (0) 3 hours ago my_minio049142538c92 minio/minio "/usr/bin/docker-ent…" 4 hours ago Exited (2) 3 hours ago friendly_cori5c1f963fae6f minio/minio "/usr/bin/docker-ent…" 4 hours ago Created eloquent_nightingaled5c091af8e87 minio/minio "/usr/bin/docker-ent…" 4 hours ago Exited (2) 3 hours ago suspicious_mayer26dc2e74cb73 minio/minio "/usr/bin/docker-ent…" 5 hours ago Exited (2) 3 hours ago priceless_allen1b26c714f77e lejmr/iredmail:mysql-latest "/entrypoint.sh supe…" 8 days ago Exited (255) 7 days ago 25/tcp, 110/tcp, 143/tcp, 587/tcp, 993/tcp, 995/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:8081->80/tcp iredmail26ea26ee07cf docker/getting-started "/docker-entrypoint.…" 9 days ago Exited (255) 7 days ago 0.0.0.0:80->80/tcp friendly_solomonPS C:\Users\DELL>
容器使用
运行容器 docker run -p 9000:9000 minio/minio server /data
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
docker run -p 9000:9000 minio/minio server /data
xiaohui@220200700182:/mnt/c/Users/DELL$ docker run -p 9000:9000 minio/minio server /dataUnable to find image 'minio/minio:latest' locallylatest: Pulling from minio/minio8f403cb21126: Pull complete65c0f2178ac8: Pull complete5893cea211f4: Pull complete5e44fe8dedd0: Pull completef8b0948ba4c8: Pull complete5d10352aca08: Pull complete93b8cfd9b941: Pull completeDigest: sha256:c2fe16f1e8f1c342521bc7403ea6cd39ff2dc269433d85b2eec73184420e632fStatus: Downloaded newer image for minio/minio:latestYou are running an older version of MinIO released 1 day agoUpdate: Run `mc admin update`Endpoint: http://172.17.0.2:9000 http://127.0.0.1:9000Browser Access:http://172.17.0.2:9000 http://127.0.0.1:9000Object API (Amazon S3 compatible):Go: https://docs.min.io/docs/golang-client-quickstart-guideJava: https://docs.min.io/docs/java-client-quickstart-guidePython: https://docs.min.io/docs/python-client-quickstart-guideJavaScript: https://docs.min.io/docs/javascript-client-quickstart-guide.NET: https://docs.min.io/docs/dotnet-client-quickstart-guideDetected default credentials 'minioadmin:minioadmin', please change the credentials immediately using 'MINIO_ROOT_USER' and 'MINIO_ROOT_PASSWORD'IAM initialization complete
docker run -p 9000:9000 minio/minio server /data
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
- docker run 运行容器命令
- 常用 OPTIONS:
- -p 端口映射 host port:docker host port
- -h 指定容器的 hostname 名称
- -d 以后台的方式运行(容器启动后,回到 cli 界面),不加-d 不能关闭(退出)界面
- -i 允许标准输入(stdin)的方式交互
- -t 在新容器内指定一个伪终端或终端
- —name 指定容器的名称
- IMAGE 镜像,先检查本地是否存在镜像,如果镜像不存在,则会从 repository 拉取,然后启动;存在,启动
确认存在的容器 docker ps
``` PS C:\Users\DELL> docker ps —help
Usage: docker ps [OPTIONS]
List containers
Options: -a, —all Show all containers (default shows just running) -f, —filter filter Filter output based on conditions provided —format string Pretty-print containers using a Go template -n, —last int Show n last created containers (includes all states) (default -1) -l, —latest Show the latest created container (includes all states) —no-trunc Don’t truncate output -q, —quiet Only display container IDs -s, —size Display total file sizes
常见 OPTIONS:1. -a 显示所有的容器(包括已停止的,启动中的,死亡的……),不加该参数只会显示运行的<a name="el1Bx"></a>## 停止容器 docker stopdocker stop [OPTIONS] CONTAINER [CONTAINER...]
PS C:\Users\DELL> docker stop —help
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER…]
Stop one or more running containers
Options: -t, —time int Seconds to wait for stop before killing it (default 10)
<a name="s3yi8"></a>## 启动容器 docker startdocker start [OPTIONS] CONTAINER [CONTAINER...]
PS C:\Users\DELL> docker start —help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER…]
Start one or more stopped containers
Options: -a, —attach Attach STDOUT/STDERR and forward signals —detach-keys string Override the key sequence for detaching a container -i, —interactive Attach container’s STDIN
<a name="DjX67"></a>## 重启容器 docker restartUsage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
PS C:\Users\DELL> docker restart —help
Usage: docker restart [OPTIONS] CONTAINER [CONTAINER…]
Restart one or more containers
Options: -t, —time int Seconds to wait for stop before killing the container (default 10)
<a name="jkJDK"></a>## 移除容器 docker rmUsage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
PS C:\Users\DELL> docker rm —help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER…]
Remove one or more containers
Options: -f, —force Force the removal of a running container (uses SIGKILL) -l, —link Remove the specified link -v, —volumes Remove anonymous volumes associated with the container
<a name="a0291"></a>## 查看容器的日志 docker logsUsage: docker logs [OPTIONS] CONTAINER
xiaohui@220200700182:/mnt/c/Users/DELL$ docker logs —help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options: —details Show extra details provided to logs -f, —follow Follow log output —since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) -n, —tail string Number of lines to show from the end of the logs (default “all”) -t, —timestamps Show timestamps —until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
参数 CONTAINER,可以是容器的名称或者容器的ID。<a name="I2OEN"></a>## 重命名容器名称 docker renameUsage: docker rename CONTAINER NEW_NAME
PS C:\Users\DELL> docker rename —help
Usage: docker rename CONTAINER NEW_NAME
Rename a container
<a name="xyCam"></a>## 查看容器状态(cpu,内存,io等) docker statsUsage: docker stats [OPTIONS] [CONTAINER...]
PS C:\Users\DELL> docker stats —help
Usage: docker stats [OPTIONS] [CONTAINER…]
Display a live stream of container(s) resource usage statistics
Options: -a, —all Show all containers (default shows just running) —format string Pretty-print images using a Go template —no-stream Disable streaming stats and only pull the first result —no-trunc Do not truncate output
<a name="fn8uo"></a>## 进入容器 docker attach 和 docker execUsage: docker attach [OPTIONS] CONTAINER
PS C:\Users\DELL> 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)
**docker attach 退出是会终止容器!!!**Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
PS C:\Users\DELL> docker exec —help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
Run a command in a running container
Options:
-d, —detach Detached mode: run command in the background
—detach-keys string Override the key sequence for detaching a
container
-e, —env list Set environment variables
—env-file list Read in a file of environment variables
-i, —interactive Keep STDIN open even if not attached
—privileged Give extended privileges to the command
-t, —tty Allocate a pseudo-TTY
-u, —user string Username or UID (format:
例子: docker exec -it my_minio2 /bin/bash<br /><br />docker exec 退出容器终端,不会导致容器的停止<a name="mHbVQ"></a>## 导入导出容器 docker export 和 docker import参考一下:[https://www.runoob.com/docker/docker-container-usage.html](https://www.runoob.com/docker/docker-container-usage.html) 中的导入导出容器<br />Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
xiaohui@220200700182:/mnt/c/Users/DELL$ docker import —help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options: -c, —change list Apply Dockerfile instruction to the created image -m, —message string Set commit message for imported image —platform string Set platform if server is multi-platform capable
Usage: docker export [OPTIONS] CONTAINER
xiaohui@220200700182:/mnt/c/Users/DELL$ docker export —help
Usage: docker export [OPTIONS] CONTAINER
Export a container’s filesystem as a tar archive
Options: -o, —output string Write to a file, instead of STDOUT
<a name="X1CLJ"></a>## 查看容器的端口 docker portUsage: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
xiaohui@220200700182:/mnt/c/Users/DELL$ docker port —help
Usage: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
List port mappings or a specific mapping for the container
xiaohui@220200700182:/mnt/c/Users/DELL$ docker port my_minio2 9000/tcp -> 0.0.0.0:9000
<a name="kCsVu"></a>## 查看容器中运行的进程 docker topUsage: docker top CONTAINER [ps OPTIONS]
xiaohui@220200700182:/mnt/c/Users/DELL$ docker top —help
Usage: docker top CONTAINER [ps OPTIONS]
Display the running processes of a container xiaohui@220200700182:/mnt/c/Users/DELL$ docker top my_minio2 UID PID PPID C STIME TTY TIME CMD root 2143 2121 0 03:39 ? 00:00:01 minio server /data root 2201 2121 0 03:40 ? 00:00:00 /bin/bash
<a name="HZZB5"></a>## 检查容器配置与状态 docker inspectUsage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
xiaohui@220200700182:/mnt/c/Users/DELL$ 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 xiaohui@220200700182:/mnt/c/Users/DELL$ docker inspect my_minio2 [ { “Id”: “f5eb97c3c28bfe0127b91130ef5c1b40b6572809c66c8a2149e101bf798f5a02”, “Created”: “2021-05-18T02:43:29.3242297Z”, “Path”: “/usr/bin/docker-entrypoint.sh”, “Args”: [ “server”, “/data” ], “State”: { “Status”: “running”, “Running”: true, “Paused”: false, “Restarting”: false, “OOMKilled”: false, “Dead”: false, “Pid”: 2143, “ExitCode”: 0, “Error”: “”, “StartedAt”: “2021-05-18T03:39:26.0400179Z”, “FinishedAt”: “2021-05-18T03:38:57.4935156Z” }, “Image”: “sha256:f55b2545c435dc5ae9acca0865506902dc98451c1ffc688f8761829f74a54391”, “ResolvConfPath”: “/var/lib/docker/containers/f5eb97c3c28bfe0127b91130ef5c1b40b6572809c66c8a2149e101bf798f5a02/resolv.conf”, “HostnamePath”: “/var/lib/docker/containers/f5eb97c3c28bfe0127b91130ef5c1b40b6572809c66c8a2149e101bf798f5a02/hostname”, “HostsPath”: “/var/lib/docker/containers/f5eb97c3c28bfe0127b91130ef5c1b40b6572809c66c8a2149e101bf798f5a02/hosts”, “LogPath”: “/var/lib/docker/containers/f5eb97c3c28bfe0127b91130ef5c1b40b6572809c66c8a2149e101bf798f5a02/f5eb97c3c28bfe0127b91130ef5c1b40b6572809c66c8a2149e101bf798f5a02-json.log”, “Name”: “/my_minio2”, “RestartCount”: 0, “Driver”: “overlay2”, “Platform”: “linux”, “MountLabel”: “”, “ProcessLabel”: “”, “AppArmorProfile”: “”, “ExecIDs”: [ “77d90ab16289e578c09cd666f24e1c989b1b04c66e9bdf457a17d38b9671436f” ], “HostConfig”: { “Binds”: null, “ContainerIDFile”: “”, “LogConfig”: { “Type”: “json-file”, “Config”: {} }, “NetworkMode”: “default”, “PortBindings”: { “9000/tcp”: [ { “HostIp”: “”, “HostPort”: “9000” } ] }, “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/0cddaf8b255916225162882f9aa05a6904b2f7aacf382686f43f230779a24d41-init/diff:/var/lib/docker/overlay2/c44d63e47ca4c7cb2dcde1a3b5c905829027fd612e9f6916f594e97f3dd6981f/diff:/var/lib/docker/overlay2/76fb442194f4b1f9ca4a1ab45ad38321cbe6aace3d9c9aafa6234c41b3c7b479/diff:/var/lib/docker/overlay2/a291f41a089567c9467c0cc244587e1fd01351c830b09569299f21de12a8dcf3/diff:/var/lib/docker/overlay2/48f65c8cabbb5c494436e950823a1999da1afb258d641eb427e55d05434db972/diff:/var/lib/docker/overlay2/89c3e5e96251cd34679a7a7aef863fcdbbef02916fcf1d3e624640b720368632/diff:/var/lib/docker/overlay2/9a3327443f509af7625296312304a6ca4086640e87757ba8addf5e523b2d10d5/diff:/var/lib/docker/overlay2/3f164a48d37afc80214231a1bf0bdcb2119cfd42ed14c5ec168762bb6eda8374/diff”, “MergedDir”: “/var/lib/docker/overlay2/0cddaf8b255916225162882f9aa05a6904b2f7aacf382686f43f230779a24d41/merged”, “UpperDir”: “/var/lib/docker/overlay2/0cddaf8b255916225162882f9aa05a6904b2f7aacf382686f43f230779a24d41/diff”, “WorkDir”: “/var/lib/docker/overlay2/0cddaf8b255916225162882f9aa05a6904b2f7aacf382686f43f230779a24d41/work” }, “Name”: “overlay2” }, “Mounts”: [ { “Type”: “volume”, “Name”: “7eba38882110fe6ea8ee8c55bb3c3277f6add025fc9577b91b6ea1e5608f529a”, “Source”: “/var/lib/docker/volumes/7eba38882110fe6ea8ee8c55bb3c3277f6add025fc9577b91b6ea1e5608f529a/_data”, “Destination”: “/data”, “Driver”: “local”, “Mode”: “”, “RW”: true, “Propagation”: “” } ], “Config”: { “Hostname”: “f5eb97c3c28b”, “Domainname”: “”, “User”: “”, “AttachStdin”: false, “AttachStdout”: false, “AttachStderr”: false, “ExposedPorts”: { “9000/tcp”: {} }, “Tty”: true, “OpenStdin”: true, “StdinOnce”: false, “Env”: [ “PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”, “container=oci”, “MINIO_ACCESS_KEY_FILE=access_key”, “MINIO_SECRET_KEY_FILE=secret_key”, “MINIO_ROOT_USER_FILE=access_key”, “MINIO_ROOT_PASSWORD_FILE=secret_key”, “MINIO_KMS_SECRET_KEY_FILE=kms_master_key”, “MINIO_UPDATE_MINISIGN_PUBKEY=RWTx5Zr1tiHQLwG9keckT0c45M3AGeHD6IvimQHpyRywVWGbP1aVSGav” ], “Cmd”: [ “server”, “/data” ], “Image”: “minio/minio”, “Volumes”: { “/data”: {} }, “WorkingDir”: “”, “Entrypoint”: [ “/usr/bin/docker-entrypoint.sh” ], “OnBuild”: null, “Labels”: { “architecture”: “x86_64”, “build-date”: “2021-04-14T21:02:42.814986”, “com.redhat.build-host”: “cpt-1002.osbs.prod.upshift.rdu2.redhat.com”, “com.redhat.component”: “ubi8-minimal-container”, “com.redhat.license_terms”: “https://www.redhat.com/en/about/red-hat-end-user-license-agreements#UBI“, “description”: “MinIO object storage is fundamentally different. Designed for performance and the S3 API, it is 100% open-source. MinIO is ideal for large, private cloud environments with stringent security requirements and delivers mission-critical availability across a diverse range of workloads.”, “desktop.docker.io/wsl-distro”: “Ubuntu-20.04”, “distribution-scope”: “public”, “io.k8s.description”: “The Universal Base Image Minimal is a stripped down image that uses microdnf as a package manager. This base image is freely redistributable, but Red Hat only supports Red Hat technologies through subscriptions for Red Hat products. This image is maintained by Red Hat and updated regularly.”, “io.k8s.display-name”: “Red Hat Universal Base Image 8 Minimal”, “io.openshift.expose-services”: “”, “io.openshift.tags”: “minimal rhel8”, “maintainer”: “MinIO Inc \u003cdev@min.io\u003e”, “name”: “MinIO”, “release”: “RELEASE.2021-05-16T05-32-34Z”, “summary”: “MinIO is a High Performance Object Storage, API compatible with Amazon S3 cloud storage service.”, “url”: “https://access.redhat.com/containers/#/registry.access.redhat.com/ubi8-minimal/images/8.3-298.1618432845“, “vcs-ref”: “f53dab37c7541dd0080f410727c5886e85c09ee7”, “vcs-type”: “git”, “vendor”: “MinIO Inc \u003cdev@min.io\u003e”, “version”: “RELEASE.2021-05-16T05-32-34Z” } }, “NetworkSettings”: { “Bridge”: “”, “SandboxID”: “3a1deb155e9618be9a0f97158d27e6c90a6803fe1b01565a05d63418a4552a9c”, “HairpinMode”: false, “LinkLocalIPv6Address”: “”, “LinkLocalIPv6PrefixLen”: 0, “Ports”: { “9000/tcp”: [ { “HostIp”: “0.0.0.0”, “HostPort”: “9000” } ] }, “SandboxKey”: “/var/run/docker/netns/3a1deb155e96”, “SecondaryIPAddresses”: null, “SecondaryIPv6Addresses”: null, “EndpointID”: “241683b5df711ab2e0c7dcce361c20b753fdc45342e72848832c0d84c633f4f6”, “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”: “1c0da5163cc5f6d57844e98fb9640ac05a446a0915f7ee826f1f20a96fd39fc4”, “EndpointID”: “241683b5df711ab2e0c7dcce361c20b753fdc45342e72848832c0d84c633f4f6”, “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 } } } } ] xiaohui@220200700182:/mnt/c/Users/DELL$ ``` docker inspect 返回一个 json 数据,查看容器的底层配置等。
