Docker是一个容器化系统,它将应用程序及其依赖项打包并运行在容器内。使用 Docker时,你必须了解几个 Docker 命令。这篇文章会介绍几个常用的命令。
查看版本信息
此命令查看你安装的Docker的版本信息
PS C:\Users\yellowbean> docker version
Client:
Cloud integration: v1.0.24
Version: 20.10.17
...
Server: Docker Desktop 4.10.1 (82475)
Engine:
Version: 20.10.17
...
下载镜像
此命令默认从Dockerhub下载镜像到本地。
PS C:\Users\yellowbean> docker pull redis
Using default tag: latest
latest: Pulling from library/redis
b85a868b505f: Pull complete
b09642bd3b88: Pull complete
e0678a951c8d: Pull complete
d5d7c0a1681b: Pull complete
954286b64dd1: Pull complete
58024fcab1ef: Pull complete
Digest: sha256:d581aded52343c461f32e4a48125879ed2596291f4ea4baa7e3af0ad1e56feed
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
如果你想指定你自己的仓库则可以在镜像名前拼接你自己的仓库地址, 例如:
// 这条命令会去 myregistry.local:5000仓库下载nginx-testing/nginx这个镜像
// 注意不要在仓库地址前加protocol (https://)
// Docker默认会给你加上https://, 如果你的仓库不是https的你需要加--insecure-registry, 或者去配置daemon.json
docker pull myregistry.local:5000/nginx-testing/nginx
查看镜像
此命令列出所有当前系统中有的docker镜像,包括一些基本信息。
PS C:\Users\yellowbean> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 2e50d70ba706 2 weeks ago 117MB
nginx latest 0e901e68141f 6 weeks ago 142MB
上面显示, 当前机器上有redis和nginx镜像
运行镜像
此命令会运行指定的镜像并且为其创建和启动一个容器
PS C:\Users\yellowbean> docker run -d redis
40891e8d563800ead5451dd870c33dc5e972f60725312c6e8e5de3710cff6fad
-d参数指定容器后台运行。docker的命令都是可以带上很多参数的, 具体某个命令有哪些参数, 是干什么的, 可以在命令后加上- -help参数来查看, 例如:
PS C:\Users\yellowbean> docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping
(host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight),
between 10 and 1000, or 0 to
disable (default 0)
--blkio-weight-device list Block IO weight (relative device
weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the
container
--cgroupns string Cgroup namespace to use
(host|private)
'host': Run the container in
the Docker host's cgroup namespace
'private': Run the container in
its own private cgroup namespace
'': Use the cgroup
namespace as configured by the
default-cgroupns-mode
option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair
Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair
Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in
microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in
microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution
(0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution
(0-3, 0,1)
-d, --detach Run container in background and
print container ID
--detach-keys string Override the key sequence for
detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed
devices list
--device-read-bps list Limit read rate (bytes per second)
from a device (default [])
--device-read-iops list Limit read rate (IO per second)
from a device (default [])
--device-write-bps list Limit write rate (bytes per
second) to a device (default [])
--device-write-iops list Limit write rate (IO per second)
to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT
of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the
container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check
(ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to
report unhealthy
--health-start-period duration Start period for the container to
initialize before starting
health-retries countdown
(ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to
run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container
that forwards signals and reaps
processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local
addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g.,
92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus
swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness
(0 to 100) (default -1)
--mount mount Attach a filesystem mount to the
container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the
container
--no-healthcheck Disable any container-specified
HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000
to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1
for unlimited)
--platform string Set platform if server is
multi-platform capable
--privileged Give extended privileges to this
container
-p, --publish list Publish a container's port(s) to
the host
-P, --publish-all Publish all exposed ports to
random ports
--pull string Pull image before running
("always"|"missing"|"never")
(default "missing")
--read-only Mount the container's root
filesystem as read only
--restart string Restart policy to apply when a
container exits (default "no")
--rm Automatically remove the container
when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the
process (default true)
--stop-signal string Signal to stop a container
(default "15")
--stop-timeout int Timeout (in seconds) to stop a
container
--storage-opt list Storage driver options for the
container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format:
<name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the
container
--volumes-from list Mount volumes from the specified
container(s)
-w, --workdir string Working directory inside the container
查看当前运行的容器
此命令会列出所有正在运行着的容器和他们的一些基本信息
PS C:\Users\yellowbean> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40891e8d5638 redis "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 6379/tcp fervent_kilby
查看所有容器
此命令会列出所有容器 (running/exited/stopped)
PS C:\Users\yellowbean> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40891e8d5638 redis "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 6379/tcp fervent_kilby
5cc88d80ff0e redis "docker-entrypoint.s…" 8 minutes ago Exited (1) 8 minutes ago interesting_chatelet
4b71770d534e nginx:latest "/docker-entrypoint.…" 5 weeks ago Exited (0) 5 weeks ago yellowbean-nginx
进入容器内部执行命令
此命令会进入到一个正在运行的容器内部, 然后你可以在容器内部执行一些bash命令
PS C:\Users\yellowbean> docker exec -it 40891e8d5638 bash
root@40891e8d5638:/data#
其中40891e8d5638是正在运行的redis container的容器ID, 也可以使用容器名字代替。如果你试图进入一个没有运行的容器会出现如下错误:
PS C:\Users\yellowbean> docker exec -it 5cc88d80ff0e bash
Error response from daemon: Container 5cc88d80ff0eb6dde5aa554776be20cba67afe62afd92b9c1b482fd3c5735d0d is not running
在bash中输入exit可以退出当前容器, 但不会停止容器:
root@40891e8d5638:/data# exit
exit
删除容器
此命令删除指定的容器, 同样, 可以指定容器ID或容器名
PS C:\Users\yellowbean> docker rm 5cc88d80ff0e
5cc88d80ff0e
如果你用上面的这个方法试图删除一个正在运行的容器, 则会抛出以下错误:
PS C:\Users\yellowbean> docker rm 40891e8d5638
Error response from daemon: You cannot remove a running container 40891e8d563800ead5451dd870c33dc5e972f60725312c6e8e5de3710cff6fad. Stop the container before attempting removal or force remove
你可以指定-f参数或是先停止容器再删除
PS C:\Users\yellowbean> docker rm 40891e8d5638 -f
40891e8d5638
可以看到已经没有redis的容器了:
PS C:\Users\yellowbean> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b71770d534e nginx:latest "/docker-entrypoint.…" 5 weeks ago Exited (0) 5 weeks ago yellowbean-nginx
删除镜像
此命令会删除指定的镜像, 可以指定镜像ID或镜像名
PS C:\Users\yellowbean> docker rmi redis
Untagged: redis:latest
Untagged: redis@sha256:d581aded52343c461f32e4a48125879ed2596291f4ea4baa7e3af0ad1e56feed
Deleted: sha256:2e50d70ba706ed644d475612b998641687dfb2415016b803ca3eb811c7096714
Deleted: sha256:bf318baa6aa554c3aea31b9c91c986cf7b33be4afd5ac5562fc94e88fceb9ac8
Deleted: sha256:e585188120ef8e14abfd097ebceba61cecdf56a85296a67e632fe17f9eed41d1
Deleted: sha256:51d7aab829870a1ea24dfc3cce7bec89d35d9a43c225370e5401d662837b9c80
Deleted: sha256:92fd29f93c5ed2985ef47d721abfd85917369726779e7fe91cd3e24d07ca1021
Deleted: sha256:1cc9e0fe288a20fc9a4c0972353bc4323714b98cb280c7f30a88330fd2e0c1af
Deleted: sha256:08249ce7456a1c0613eafe868aed936a284ed9f1d6144f7d2d08c514974a2af9
如果你试图删除一个已经有容器的镜像则会报如下错误:
PS C:\Users\yellowbean> docker rmi redis
Error response from daemon: conflict: unable to remove repository reference "redis" (must force) - container 506fddb045a0 is using its referenced image 2e50d70ba706
你需要先删除用这个镜像创建出来的所有容器, 然后才能删除镜像, 或者加-f参数。
重启容器
此命令会重启指定的docker容器, 可以指定容器ID或容器名
PS C:\Users\yellowbean> docker restart yellowbean-nginx
yellowbean-nginx
可以看到nginx容器已在运行:
PS C:\Users\yellowbean> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b71770d534e nginx:latest "/docker-entrypoint.…" 5 weeks ago Up 37 seconds 0.0.0.0:80->80/tcp yellowbean-nginx
停止容器
此命令可以停止指定的容器, 可以指定容器ID或名字
PS C:\Users\yellowbean> docker stop yellowbean-nginx
yellowbean-nginx
可以看到已经没有正在运行的容器了:
PS C:\Users\yellowbean> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
如果想停止所有容器, 则可以这样:
docker stop $(docker ps -a -q)
同理删除所有也可以:
docker rm $(docker ps -a -q)
启动容器
此命令启动指定的docker容器, 可以指定ID或名字
PS C:\Users\yellowbean> docker start yellowbean-nginx
yellowbean-nginx
可以看到nginx正在运行:
PS C:\Users\yellowbean> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b71770d534e nginx:latest "/docker-entrypoint.…" 5 weeks ago Up 45 seconds 0.0.0.0:80->80/tcp yellowbean-nginx
杀死容器
此命令会立刻停止一个正在运行的容器, 不管容器内是不是有没有执行完的任务, 对比docker stop命令,docker kill 会发出一个SIGKILL信号, 它会立刻停止容器, 而docker stop发出一个SIGTERM信号, 它会优雅的停止容器 (进程可以处理,忽略或阻止这个信号, 而且子进程或父进程可以向其他进程发送信息), 所以建议使用docker stop.
PS C:\Users\yellowbean> docker kill yellowbean-nginx
yellowbean-nginx
可以看到nginx已经停止了:
PS C:\Users\yellowbean> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
提交容器
此命令会根据指定的容器生成一个新的镜像
PS C:\Users\yellowbean> docker commit 4b71770d534e yellowbean/yellowbean-nginx-1
sha256:df0c3013140e57f95d1c508b9150e0bfedd6e38ca6a2fc3092f7bce3ba8bbe45
最后一部分中, yellowbean是用户名, yellowbean-nginx-1是新镜像的名字, 可以看到生成了一个新的镜像:
PS C:\Users\yellowbean> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yellowbean/yellowbean-nginx-1 latest df0c3013140e 34 seconds ago 142MB
nginx latest 0e901e68141f 6 weeks ago 142MB
登录Docker账号
此命令会登录你的Dockerhub账号, 如果没有账号可以到这里注册一个.
由于我之前已经登陆了所以会显示如下信息:
PS C:\Users\yellowbean> docker login
Authenticating with existing credentials...
Login Succeeded
登陆后你就可以把你自己的docker镜像推送到docker的官方仓库了。
推送镜像
此命令默认将镜像推送到你的Dockerhub账户里的仓库中, 当然你也可以指定你自己私有仓库的地址进行推送
PS C:\Users\yellowbean> docker push yellowbean/yellowbean-nginx-1
The push refers to repository [docker.io/yellowbean/yellowbean-nginx-1]
734d9104a6a2: Pushed
635721fc6973: Mounted from library/nginx
bea448567d6c: Mounted from library/nginx
bfaa5f9c3b51: Mounted from library/nginx
9d542ac296cc: Mounted from library/nginx
d8a33133e477: Mounted from library/nginx
latest: digest: sha256:3904662761df9d76ef04ddfa5cfab764b85e3eedaf10071cfbe2bf77254679ac size: 1574
Docker网络
此命令会列出集群中所有的网络。
PS C:\Users\yellowbean> docker network ls
NETWORK ID NAME DRIVER SCOPE
51a0bc80cef6 bridge bridge local
7a9ce81bd75d host host local
be28b76169be none null local
8a695a204693 shared bridge local
最后一个shared网络是我自己创建的, 你可以查看docker network可以使用的命令:
PS C:\Users\yellowbean> docker network
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
docker网络的通常用途就是将容器放到相同的网络中(例如放到shared中), 这样容器之间就可以不需要用ip来进行通信, 而是直接使用容器名,这样即使容器重启ip变了, 也不会有影响。
查看Docker的信息
此命令会列出当前系统中安装的Docker的详细信息,例如内核信息,安装的容器个数, 镜像个数等。
PS C:\Users\yellowbean> docker info
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.8.2)
compose: Docker Compose (Docker Inc., v2.6.1)
extension: Manages Docker extensions (Docker Inc., v0.2.7)
sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 2
Server Version: 20.10.17
...
复制文件
此命令可以将文件从容器复制到宿主机, 或从宿主机复制到容器中, 在Docker1.8以前只能将文件从容器复制到宿主机。
将文件从宿主机复制到容器:
docker cp /var/www/html/yelloweban.png container_id:/usr/share/nginx/html/yelloweban.png
将文件从容器复制到宿主机:
docker cp container_id:/usr/share/nginx/html/yelloweban.png /var/www/html/yelloweban.png
查看历史记录
此命令查看指定镜像的历史操作记录:
PS C:\Users\yellowbean> docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
0e901e68141f 6 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 6 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 6 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 6 weeks ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
<missing> 6 weeks ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 6 weeks ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 6 weeks ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
<missing> 6 weeks ago /bin/sh -c set -x && addgroup --system -… 61.1MB
<missing> 6 weeks ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.3 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.6 0B
<missing> 6 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 6 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ADD file:134f25aec8adf83cb… 80.4MB
查看日志
此命令查看指定容器的日志
PS C:\Users\yellowbean> docker logs yellowbean-redis
1:C 09 Jul 2022 14:40:01.336 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 09 Jul 2022 14:40:01.336 # Redis version=7.0.2, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 09 Jul 2022 14:40:01.336 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 09 Jul 2022 14:40:01.336 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 7.0.2 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 09 Jul 2022 14:40:01.337 # Server initialized
1:M 09 Jul 2022 14:40:01.337 * Ready to accept connections
1:signal-handler (1657377618) Received SIGTERM scheduling shutdown...
1:M 09 Jul 2022 14:40:18.993 # User requested shutdown...
1:M 09 Jul 2022 14:40:18.993 * Saving the final RDB snapshot before exiting.
1:M 09 Jul 2022 14:40:18.995 * DB saved on disk
1:M 09 Jul 2022 14:40:18.995 # Redis is now ready to exit, bye bye...
查找镜像
此命令默认从Dockerhub查找镜像
PS C:\Users\yellowbean> docker search rabbitmq
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
rabbitmq RabbitMQ is an open source multi-protocol me… 4368 [OK]
bitnami/rabbitmq Bitnami Docker Image for RabbitMQ 87 [OK]
kbudde/rabbitmq-exporter rabbitmq_exporter for prometheus 16 [OK]
nasqueron/rabbitmqadmin RabbitMQ management plugin CLI tool Lightwei… 1 [OK]
circleci/rabbitmq-delayed https://github.com/circleci/rabbitmq-delayed… 1
rabbitmqoperator/cluster-operator The RabbitMQ Cluster Operator Docker Image 1
bitnami/rabbitmq-exporter 1
circleci/rabbitmq This image is for internal use 0
clearlinux/rabbitmq RabbitMQ multi-protocol messaging broker wit… 0
brightercommand/rabbitmq RabbitMQ management with delay plugin enabled 0
drud/rabbitmq rabbitmq 0 [OK]
nasqueron/rabbitmq RabbitMQ wth management, MQTT and STOMP plug… 0 [OK]
itisfoundation/rabbitmq 0
newrelic/k8s-nri-rabbitmq New Relic Infrastructure RabbitMQ Integratio… 0
ibmcom/rabbitmq-java-client 0
ibmcom/rabbitmq-server-ppc64le 0
ibmcom/rabbitmq-java-client-ppc64le 0
bitnami/rabbitmq-cluster-operator 0
rabbitmqoperator/cluster-operator-dev 0
rabbitmqoperator/messaging-topology-operator 0
rabbitmqoperator/messaging-topology-operator-dev 0
rabbitmqoperator/default-user-credential-updater-dev 0
rabbitmqoperator/default-user-credential-updater 0
rabbitmqoperator/single-active-consumer-operator-dev 0
ibmcom/rabbitmq-exporter-ppc64le 0
如果你想从自己的私有仓库查找可以发送一个http GET请求, docker的registry镜像默认是起在5000端口:
// 列出所有镜像
curl -X GET https://myregistry:5000/v2/_catalog
> {"repositories":["redis","nginx"]}
// 列出某个镜像的所有版本号
curl -X GET https://myregistry:5000/v2/redis/tags/list
> {"name":"redis","tags":["latest"]}
// 如果你的私有仓库需要认证, 则可以指定用户名密码
curl -X GET -u <user>:<pass> https://myregistry:5000/v2/_catalog
curl -X GET -u <user>:<pass> https://myregistry:5000/v2/redis/tags/list
更新容器配置
此命令更新容器的配置, 可以看一看有什么选项:
PS C:\Users\yellowbean> docker update --help
Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--blkio-weight uint16 Block IO (relative weight), between 10
and 1000, or 0 to disable (default 0)
--cpu-period int Limit CPU CFS (Completely Fair
Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair
Scheduler) quota
--cpu-rt-period int Limit the CPU real-time period in
microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in
microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap:
'-1' to enable unlimited swap
--pids-limit int Tune container pids limit (set -1 for
unlimited)
--restart string Restart policy to apply when a
container exits
创建数据卷
此命令会创建数据卷。
docker volume create --driver local \
--opt type=none \
--opt device=/var/www/html \
--opt o=bind \
yellowbean-nginx-volume
容器数据卷是很有必要的, 比如你有一个nginx, 你需要用它host静态网页, 通常我们不用docker安装nginx时, 会使用服务器的/var/www/html文件夹作为静态资源文件夹, 但如果使用docker安装nginx, 则静态文件目录是在容器内的/usr/share/nginx/html下, 此时我们就可以创建一个容器卷yellowbean-nginx-volume对应路径为宿主机的/var/www/html然后在运行nginx镜像时使用-v参数指定容器卷映射例如:
PS C:\Users\yellowbean> docker run -d -v yellowbean-nginx-volume:/usr/share/nginx/html -p 80:80 nginx
4f18b5187ba8189b459d6459b400c289b9200ec56b2cad5313ba1bce80ddfc2c
这样一来我们只需要在宿主机的/var/www/html下放置文件, docker就会自动将其同步到容器内的/usr/share/nginx/html下。
当然, 在运行镜像时做容器数据卷映射还有其他作用, 比如你有一个MongoDB数据库, 如果你没做数据库data的数据卷映射则, 则一旦这个容器被删除, 之前存的数据就都没了, 但如果做了映射, 相当于数据"备份"在宿主机, 下次再运行镜像生成新容器时只要-v指定之前的数据卷, 之前数据库的数据就会同步到新的容器中。
安装插件
此命令为Docker安装插件, 例如安装一个vieux/sshfs插件, 这个插件让你能够通过sshfs将远程的文件夹目录作为容器数据卷的目录:
PS C:\Users\yellowbean> docker plugin install vieux/sshfs DEBUG=1
Plugin "vieux/sshfs" is requesting the following privileges:
- network: [host]
- mount: [/var/lib/docker/plugins/]
- mount: []
- device: [/dev/fuse]
- capabilities: [CAP_SYS_ADMIN]
Do you grant the above permissions? [y/N] y
latest: Pulling from vieux/sshfs
52d435ada6a4: Download complete
Digest: sha256:1d3c3e42c12138da5ef7873b97f7f32cf99fb6edde75fa4f0bcf9ed277855811
Status: Downloaded newer image for vieux/sshfs:latest
Installed plugin vieux/sshfs
查看Docker插件:
PS C:\Users\yellowbean> docker plugin ls
ID NAME DESCRIPTION ENABLED
2a32d1fb95af vieux/sshfs:latest sshFS plugin for Docker true
登出Docker账号
此命令会退出当前登录的Dockerhub账号
PS C:\Users\yellowbean> docker logout
Removing login credentials for https://index.docker.io/v1/
总结
这篇文章列举了一些常用的Docker命令, 希望可以在工作或学习中帮助到你🤠