Docker 的一些命令技术
Docker 的一些常用命令!
-i 交互,-t 终端
pi@raspberrypi:~ $ sudo docker run -i -t ubuntu /bin/bash root@593b84502134:/# pwd / root@593b84502134:/# df -h Filesystem Size Used Avail Use% Mounted on overlay 117G 58G 55G 52% / tmpfs 64M 0 64M 0% /dev tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup shm 64M 0 64M 0% /dev/shm /dev/root 117G 58G 55G 52% /etc/hosts tmpfs 3.9G 0 3.9G 0% /proc/asound tmpfs 3.9G 0 3.9G 0% /sys/firmware root@593b84502134:/# ls / bin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var root@593b84502134:/# du -sh /* 0 /bin 4.0K /boot 0 /dev 628K /etc 4.0K /home 0 /lib 4.0K /media 4.0K /mnt 4.0K /opt du: cannot access '/proc/11/task/11/fd/3': No such file or directory du: cannot access '/proc/11/task/11/fdinfo/3': No such file or directory du: cannot access '/proc/11/fd/3': No such file or directory du: cannot access '/proc/11/fdinfo/3': No such file or directory 0 /proc 12K /root 20K /run 0 /sbin 4.0K /srv 0 /sys 4.0K /tmp 66M /usr 4.5M /var
Ctrl+D exit
-d detach 后台运行,打印容器 ID。-P 高端口映射,-c 执行 shell。
pi@raspberrypi:~ $ sudo docker run -d ubuntu /bin/sh -C "while true; do echo hello world; sleep 1; done;" da85f479ec812ca85f8a5e3d5e0f060c54c14d1cab80e86a625e00e2c98b8db9 pi@raspberrypi:~ $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES pi@raspberrypi:~ $ sudo docker run -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done;" 3ff5841790ac6770df30326f12783453c22a87dc6b22f74dc313f376587ac668 pi@raspberrypi:~ $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3ff5841790ac ubuntu "/bin/sh -c 'while t…" 19 seconds ago Up 18 seconds upbeat_booth pi@raspberrypi:~ $ sudo docker logs 3ff584179 hello world hello world pi@raspberrypi:~ $ sudo docker stop 3ff584179 // or upbeat_booth 3ff584179 pi@raspberrypi:~ $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
运行一个 python 的 web 项目, -p 映射指定端口
pi@raspberrypi:~ $ sudo docker run -d -P training/webapp python app.py Unable to find image 'training/webapp:latest' locally latest: Pulling from training/webapp Image docker.io/training/webapp:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/ e190868d63f8: Pull complete 909cd34c6fd7: Pull complete 0b9bfabab7c1: Pull complete a3ed95caeb02: Pull complete 10bbbc0fc0ff: Pull complete fca59b508e9f: Pull complete e7ae2541b15b: Pull complete 9dd97ef58ce9: Pull complete a4c1b0cb7af7: Pull complete Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d Status: Downloaded newer image for training/webapp:latest WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested 27087a62a0b7e6cabe7173395abdb11cec1832f7181ee2c6a14e5b3825b1139b pi@raspberrypi:~ $ sudo docker run -d -p 5000:5000 training/webapp python app.py WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested d82207b57060044e1d8dd5e786494f67ed19ac359ab64972e79317a7217ad128 pi@raspberrypi:~ $ sudo docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d82207b57060 training/webapp "python app.py" 2 minutes ago Exited (1) 2 minutes ago musing_nightingale pi@raspberrypi:~ $ sudo docker logs d82207b57060 standard_init_linux.go:228: exec user process caused: exec format error pi@raspberrypi:~ $ sudo docker inspect d82207b57060 [ { "Id": "d82207b57060044e1d8dd5e786494f67ed19ac359ab64972e79317a7217ad128", "Created": "2021-09-09T11:28:48.201418872Z", "Path": "python", "Args": [ "app.py" ], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 1, "Error": "", "StartedAt": "2021-09-09T11:28:49.028064994Z", "FinishedAt": "2021-09-09T11:28:49.027072084Z" }, "Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557", "ResolvConfPath": "/var/lib/docker/containers/d82207b57060044e1d8dd5e786494f67ed19ac359ab64972e79317a7217ad128/resolv.conf", "HostnamePath": "/var/lib/docker/containers/d82207b57060044e1d8dd5e786494f67ed19ac359ab64972e79317a7217ad128/hostname", "HostsPath": "/var/lib/docker/containers/d82207b57060044e1d8dd5e786494f67ed19ac359ab64972e79317a7217ad128/hosts", "LogPath": "/var/lib/docker/containers/d82207b57060044e1d8dd5e786494f67ed19ac359ab64972e79317a7217ad128/d82207b57060044e1d8dd5e786494f67ed19ac359ab64972e79317a7217ad128-json.log", "Name": "/musing_nightingale", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "5000/tcp": [ { "HostIp": "", "HostPort": "5000" } ] }, "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": 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/6b82e7e73cb5b394c55809ce10dba977e2307f2d5fd1de0e95fabe5f1ba0fd45-init/diff:/var/lib/docker/overlay2/daae152b9f59893ffc4b556f16c28f5c3f9d0ecfae7572b11a187a31e8574eda/diff:/var/lib/docker/overlay2/b8a02a431e8fba0f44d28fb6bbfb040503bcab883a65315c749ba8a1bb7c63da/diff:/var/lib/docker/overlay2/d7554908d1c80b7bf5f8dbabe08191e04fa57f866a2c19378d8035aac51af8ab/diff:/var/lib/docker/overlay2/63f23880ff06be2bc8176efc0b4bd775719ff9e4b5869c97d3ec3ec6d7689a46/diff:/var/lib/docker/overlay2/fad6d577479805f85a28192e76468bbee208b1518076e24295f76f66e911bd1c/diff:/var/lib/docker/overlay2/cae5306b87c786aac4e5516f881179194b3ec78472b803803f9d665f6b10ae50/diff:/var/lib/docker/overlay2/7a64baad5f527f6a9f7ebf4e333b21919aa6a14972e0f528496063724811e602/diff:/var/lib/docker/overlay2/803bf7d43ac959f82a0639cbed438b2c952d2938c05cc8da222efeea839680c4/diff:/var/lib/docker/overlay2/337e86430d341682fce7c3c921f95aaa4a385c39da93f4b4929a1bf13f0466e8/diff:/var/lib/docker/overlay2/3290139e5a89ae05e42549752eebc49cfa8d3314785364fec4ecb879e2927065/diff:/var/lib/docker/overlay2/8ea1fb65309c8988c109f0a13ca49d9e56f64ebf21b99bc26029cf89a63e552d/diff:/var/lib/docker/overlay2/806df0566130797a8c7c581792b8df14d8413ae9a67aa2ad33f1197cf9373a86/diff:/var/lib/docker/overlay2/b5aa693bca54b8a8f371eb7ebfbd53b5f74c7bd78cbf5170021f2039c8cd2648/diff", "MergedDir": "/var/lib/docker/overlay2/6b82e7e73cb5b394c55809ce10dba977e2307f2d5fd1de0e95fabe5f1ba0fd45/merged", "UpperDir": "/var/lib/docker/overlay2/6b82e7e73cb5b394c55809ce10dba977e2307f2d5fd1de0e95fabe5f1ba0fd45/diff", "WorkDir": "/var/lib/docker/overlay2/6b82e7e73cb5b394c55809ce10dba977e2307f2d5fd1de0e95fabe5f1ba0fd45/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "d82207b57060", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "5000/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "python", "app.py" ], "Image": "training/webapp", "Volumes": null, "WorkingDir": "/opt/webapp", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "931d5b828b41778af2cd551998fc7c77a65d6ff4c786ebf6c464f139446a5f0a", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/931d5b828b41", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "21110aef31becaaf235f9a5dc158256af1ab70f2e98b169263dda3816d317036", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]
start stop rm
pi@raspberrypi:~ $ sudo docker start d82207b57060 d82207b57060 pi@raspberrypi:~ $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 54ab604fab8d 9 days ago 65.6MB hello-world latest bc11b176a293 2 months ago 9.14kB training/webapp latest 6fae60ef3446 6 years ago 349MB
docker search , docker pull
pi@raspberrypi:~ $ sudo docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 15443 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2063 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 818 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 240 linuxserver/nginx An Nginx container, brought to you by LinuxS… 152 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 141 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 135 [OK] alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 106 [OK] jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI… 92 [OK] nginxdemos/hello NGINX webserver that serves a simple page co… 72 [OK] privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 57 [OK] nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 55 nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 48 staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 24 [OK] nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 20 nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 19 schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK] centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15 centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13 flashspys/nginx-static Super Lightweight Nginx Image 10 [OK] mailu/nginx Mailu nginx frontend 9 [OK] sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK] wodby/nginx Generic nginx 1 [OK] arnau/nginx-gate Docker image with Nginx with Lua enabled on … 1 [OK] pi@raspberrypi:~ $ sudo docker pull nginx Using default tag: latest latest: Pulling from library/nginx d10c227306ce: Pull complete 3b3450d77825: Pull complete 976f19f897eb: Pull complete cf2a5bf96bb5: Pull complete 218cda107c30: Pull complete 49f49174f1f4: Pull complete Digest: sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
docker commit -m="xxx" -a="xxx" e218edb10161
docker build,Dockfile
FROM MAINTAINER RUN EXPOSE EXEC
docker tag 860c279d2fec author/ubuntu:dev。docker run --name 指定名字,-v 映射目录。docker attach id 进入一个运行中的容器。
i@raspberrypi:~ $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6edbfad99bce nginx "/docker-entrypoint.…" 14 minutes ago Exited (137) 11 minutes ago flamboyant_lamport ef9da757eaa3 nginx "/docker-entrypoint.…" 20 minutes ago Exited (0) 16 minutes ago determined_austin 6fd064ca83df nginx "/docker-entrypoint.…" 21 minutes ago Exited (127) 21 minutes ago hungry_faraday f7d2b883be0c nginx "/docker-entrypoint.…" 22 minutes ago Exited (0) 22 minutes ago vigilant_williamson 17af5dff3d4e nginx "/docker-entrypoint.…" 23 minutes ago Exited (0) 23 minutes ago happy_mestorf d82207b57060 training/webapp "python app.py" About an hour ago Exited (1) About an hour ago musing_nightingale 9b81d7a17a9d training/webapp "python app.py" About an hour ago Exited (1) About an hour ago exciting_leakey 27087a62a0b7 training/webapp "python app.py" About an hour ago Exited (1) About an hour ago affectionate_pike 3ff5841790ac ubuntu "/bin/sh -c 'while t…" 2 hours ago Exited (137) 2 hours ago upbeat_booth da85f479ec81 ubuntu "/bin/sh -C 'while t…" 2 hours ago Exited (127) 2 hours ago goofy_hodgkin 593b84502134 ubuntu "/bin/bash" 3 hours ago Exited (0) 3 hours ago strange_khayyam 8f7644cd9140 ubuntu "/bin/echo 'hello wo…" 3 hours ago Exited (0) 3 hours ago sad_blackburn c8c31313b3d3 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago competent_golick
docker rmi $(docker images | grep -v REPOSITORY | awk '{print $3}')
docker history 镜像ID。docker export 容器ID > xxx.tar。docker save ;docker load
Docker 命令大全 容器生命周期管理 run start/stop/restart kill rm pause/unpause create exec 容器操作 ps inspect top attach events logs wait export port 容器rootfs命令 commit cp diff 镜像仓库 login pull push search 本地镜像管理 images rmi tag build history save import info|version info version
备忘录
入门 在后台创建和运行容器 $ docker run -d -p 80:80 docker/getting-started -d - 在分离模式下运行容器 -p 80:80 - 将端口 80 映射到容器中的端口 80 docker/getting-started - 要使用的图像 在前台创建并运行容器 $ docker run -it -p 8001:8080 --name my-nginx nginx -it - 交互式 bash 模式 -p 8001:8080 - 将端口 8001 映射到容器中的端口 8080 --name my-nginx - 指定名称 nginx - 要使用的图像 一般命令 命令 描述 docker ps 列出正在运行的容器 docker ps -a 列出所有容器 docker ps -s 列出正在运行的容器 (带 CPU/内存) docker images 列出所有图像 docker exec -it <container> bash 连接到容器 docker logs <container> 显示容器的控制台日志 docker stop <container> 停止一个容器 docker restart <container> 重启一个容器 docker rm <container> 移除一个容器 docker port <container> 显示容器的端口映射 docker top <container> 列出进程 docker kill <container> 杀死一个容器 参数<container>可以是容器 ID 或名称 Docker 容器 启动和停止 命令 描述 docker start nginx-server 开始 docker stop nginx-server 停止 docker restart nginx-server 重启 docker pause nginx-server 暂停 docker unpause nginx-server 取消暂停 docker wait nginx-server 阻塞容器 docker kill nginx-server 发送 SIGKILL docker attach nginx-server 连接到现有容器 信息 命令 描述 docker ps 列出正在运行的容器 docker ps -a 列出所有容器 docker logs nginx-server 容器日志 docker inspect nginx-server 检查容器 docker events nginx-server 容器事件 docker port nginx-server 公共端口 docker top nginx-server 运行进程 docker stats nginx-server 容器资源使用 docker diff nginx-server 列出对容器所做的更改。 创建 docker create [options] IMAGE -a, --attach # attach stdout/err -i, --interactive # attach stdin (interactive) -t, --tty # pseudo-tty --name NAME # name your image -p, --publish 5000:5000 # port map (host:container) --expose 5432 # expose a port to containers -P, --publish-all # publish all ports --link container:alias # linking -v, --volume `pwd`:/app # mount (absolute paths needed) -e, --env NAME=hello # env vars 例子 $ docker create --name my_redis --expose 6379 redis:3.0.2 操作 重命名容器 docker rename my-nginx nginx-server 移除容器 docker rm nginx-server 更新容器 docker update --cpu-shares 512 -m 300M nginx-server Docker 镜像 操作 命令 描述 docker images 列出图像 docker rmi nginx 删除图像 docker load < ubuntu.tar.gz 加载 tarred 存储库 docker load --input ubuntu.tar 加载 tarred 存储库 docker save busybox > ubuntu.tar 将图像保存到 tar 存档 docker history 显示图像的历史 docker commit nginx 将容器另存为图像。 docker tag nginx eon01/nginx 标记图像 docker push eon01/nginx 推送图片 构建图像 $ docker build . $ docker build github.com/creack/docker-firefox $ docker build - < Dockerfile $ docker build - < context.tar.gz $ docker build -t eon/nginx-server . $ docker build -f myOtherDockerfile . $ curl example.com/remote/Dockerfile | docker build -f - . Docker 网络 操作 删除网络 docker network rm MyOverlayNetwork 列出网络 docker network ls 获取有关网络的信息 docker network inspect MyOverlayNetwork 将正在运行的容器连接到网络 docker network connect MyOverlayNetwork nginx 启动时将容器连接到网络 docker run -it -d --network=MyOverlayNetwork nginx 断开容器与网络的连接 docker network disconnect MyOverlayNetwork nginx 创建网络 docker network create -d overlay MyOverlayNetwork docker network create -d bridge MyBridgeNetwork docker network create -d overlay \ --subnet=192.168.0.0/16 \ --subnet=192.170.0.0/16 \ --gateway=192.168.0.100 \ --gateway=192.170.0.100 \ --ip-range=192.168.1.0/24 \ --aux-address="my-router=192.168.1.5" \ --aux-address="my-switch=192.168.1.6" \ --aux-address="my-printer=192.170.1.5" \ --aux-address="my-nas=192.170.1.6" \ MyOverlayNetwork 其他 码头工人中心 Docker 语法 描述 docker search search_word 在 docker hub 中搜索图像。 docker pull user/image 从 docker hub 下载镜像。 docker login 向 docker hub 进行身份验证 docker push user/image 将图像上传到 docker hub。 注册表命令 登录注册表 $ docker login $ docker login localhost:8080 从注册表注销 $ docker logout $ docker logout localhost:8080 搜索图像 $ docker search nginx $ docker search nginx --stars=3 --no-trunc busybox 拉取镜像 $ docker pull nginx $ docker pull eon01/nginx localhost:5000/myadmin/nginx 推送图像 $ docker push eon01/nginx $ docker push eon01/nginx localhost:5000/myadmin/nginx 批量清理 命令 描述 docker stop -f $(docker ps -a -q) 停止所有容器 docker rm -f $(docker ps -a -q) 删除所有容器 docker rmi -f $(docker images -q) 删除所有图像 容量 检查容量 $ docker volume ls 清理未使用的容量 $ docker volume prune
暂无