常见docker操作命令一
以 $ 开头表示命令输入
1. docker ps --help 查看命令的帮忙信息,用法如下
docker ps [OPTIONS] 查看容器
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 使用Go模板美化打印容器信息
-n, --last int Show n last created containers (includes all states) (default -1) 显示 n 个最后创建的容器
-l, --latest Show the latest created container (includes all states) 显示最后创建的容器
--no-trunc Don't truncate output 不进行截断输出
-q, --quiet Only display container IDs 只显示容器的ID
-s, --size Display total file sizes 显示容器文件的总大小
eg:
$docker ps --format "{{.ID}}\t {{.Names}}\t{{.Ports}}\t{{.Status}}"
bd601805c6e1 nginxweb 0.0.0.0:80->80/tcp, :::80->80/tcp Up 46 hours
62719709132c mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up 2 weeks
2. docker search --help
docker search [OPTIONS] TERM 搜索指定的镜像
Options:
-f, --filter filter Filter output based on conditions provided 根据提供的条件过滤输出
--format string Pretty-print search using a Go template 使用Go模板美化打印容器信息
--limit int Max number of search results (default 25) 显示最大的搜索结果(默认显示25条记录)
--no-trunc Don't truncate output 不进行截断输出
eg:
$docker search tomcat --limit 5
tomcat Apache Tomcat is an open source implementati… 3186 [OK]
rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 7 [OK]
amd64/tomcat Apache Tomcat is an open source implementati… 3
jelastic/tomcat An image of the Tomcat Java application serv… 2
cfje/tomcat-resource Tomcat Concourse Resource 2
3. docker pull --help
docker pull [OPTIONS] NAME[:TAG|@DIGEST] 拉取指定的镜像
Options:
-a, --all-tags Download all tagged images in the repository 下载存储库中的所有标记镜像
--disable-content-trust Skip image verification (default true) 跳过镜像验证
--platform string Set platform if server is multi-platform capable 如果服务器支持多平台,则设置平台
-q, --quiet Suppress verbose output 禁止详情输出
eg:
$docker pull hello-world -q
docker.io/library/hello-world:latest
4. docker rmi --help
docker rmi [OPTIONS] IMAGE [IMAGE...] 删除镜像(可以同时删除一个或多个)
Options:
-f, --force Force removal of the image 强制删除镜像
--no-prune Do not delete untagged parents 不要删除未标记的父镜像
eg:
$docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
5. docker images --help
docker images [OPTIONS] [REPOSITORY[:TAG]] 查看镜像信息
Options:
-a, --all Show all images (default hides intermediate images) 显示所有镜像(默认隐藏中间镜像)
--digests Show digests 显示摘要
-f, --filter filter Filter output based on conditions provided 根据提供的条件过滤输出
--format string Pretty-print images using a Go template 使用Go模板美化打印镜像信息
--no-trunc Don't truncate output 不进行截断输出
-q, --quiet Only show image IDs 只显示镜像ID
eg:
$docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
nginx latest sha256:fd63aac07effae8efcc4677b6197783b11a674b6664ba86e76884b1af3ffe345 e9ce56a96f8e 2 weeks ago 141MB
mysql 5.7 sha256:2db8bfd2656b51ded5d938abcded8d32ec6181a9eae8dfc7ddf87a656ef97e97 938b57d64674 6 weeks ago 448MB
6. 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 删除与容器关联的匿名卷
eg:
$docker rm da782063fbf6
da782063fbf6
7. 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 在后台运行容器并打印容器 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 即使未连接,也要保持 STDIN 打开(通过与-t一起使用)
--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 "SIGTERM")
--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
eg:
$docker run --name centos_test -d centos (在后台 -d 运行容器,为容器命名:centos_test)
6e04f003091cd05f202d323c5dcffb0d0ea7ce4b05544f17223222c8d20ec5a6 容器ID
8. 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 即使未连接,也要保持 STDIN 打开
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY 分配一个虚拟终端,一般与 -i 一起使用
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
快捷键 :ctrl + q + p 可退出容器
eg:
$docker exec -it centos_test /bin/bash 进入正在运行的容器,使用/bin/bash命令终端,如下显示容器命令行
[root@a4c79d6915a4 /]#
9. docker network --help
Usage: docker network COMMAND docker网络
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 删除一个或多个网络
拉取一个镜像(不加tag标签,默认拉取最新的镜像)
docker pull busybox
###基于busybox镜像创建一个名为server1的容器,以交互的形式,并指定在后台运行
docker run -it -d --name server1 busybox /bin/sh
创建一个名为network-one类型为bridge的网络
docker network create network-one
查看网络详情
docker network inspect network-one
把server容器加入到network-one的网络
docker network connect network-one server1
查看network-one网络详情
docker network inspect network-one
...
"Containers": {
"6160db71a76c9877e08a1ab04a8cde388f5b5bbaff9613582eaf879b700354c7": {
"Name": "server1", # 容器名
"EndpointID": "37e8a0f4e4ca1d126b7142eb99a5c3c928065c5dc3b7bba76753db25783e867c",
"MacAddress": "02:42:ac:14:00:02",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
}
},
...
查看server1容器的详情
docker inspect server1
...
"network-one": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"6160db71a76c"
],
"NetworkID": "5ededaf30ce95026a24844b458445dbb88b1bc4a5cbbc319c6892d8daaee9a0b",
"EndpointID": "37e8a0f4e4ca1d126b7142eb99a5c3c928065c5dc3b7bba76753db25783e867c",
"Gateway": "172.20.0.1", # 网关地址
"IPAddress": "172.20.0.2", # 本容器的ipv4地址
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:14:00:02",
"DriverOpts": {}
}
...
所有docker命令,后面有时间会继续更新,欢迎各位码友收藏点赞
docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.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 "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.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.3-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.9.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
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
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容器命令<<<<<<
Usage: docker container COMMAND
Manage containers 管理容器
Commands:
attach Attach local standard input, output, and error streams to a running container
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
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
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
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
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
>>>>>>docker镜像命令<<<<<<<
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.