Docker 常用命令汇总
启动、停止、重启[root@localhost ~]# systemctl start docker[root@localhost ~]# systemctl restart docker[root@localhost ~]# systemctl stop dockerDocker 启动进程之后,才能进行容器操作。# 查看docker命令参数[root@localhost ~]# docker -
·
启动、停止、重启
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl stop docker
Docker 启动进程之后,才能进行容器操作。
# 查看docker命令参数
[root@localhost ~]# docker --help
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-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:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach 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 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.
举例常用命令:
run 启动一个新容器的命令
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
#查看更多参数
[root@localhost ~]# docker run --help
docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx
docker run --name some-nginx -d some-content-nginx
docker run --name some-nginx -d -p 8080:80 some-content-nginx
kill 杀掉一个运行中的容器
[root@localhost ~]# docker kill --help
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options:
--help Print usage
-s, --signal string Signal to send to the container (default "KILL")
[root@localhost ~]# docker kill -s KILL 515d0dc31187
515d0dc31187
rm 删除一个或多个容器
[root@localhost ~]# 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)
--help Print usage
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
[root@localhost ~]# docker run --name some-nginx -d -p 8080:80 some-content-nginx
3ca143a6d94d1b0d1cf236b5ad05947b9c893d20b64a7d5e35944973d7158512
[root@localhost ~]# docker rm -f 3ca143a6d94
3ca143a6d94
删除所有已经停止的容器(正在运行的容器时无法删除掉的)
[root@localhost ~]# docker rm $(docker ps -a -q)
515d0dc31187
docker pause :暂停容器中所有的进程。docker unpause :恢复容器中所有的进程。
[root@localhost ~]# docker pause --help
Usage: docker pause CONTAINER [CONTAINER...]
[root@localhost ~]# docker unpause --help
Usage: docker unpause CONTAINER [CONTAINER...]
Unpause all processes within one or more containers
exec 在运行的容器中执行命令
[root@localhost ~]# 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 (default [])
--help Print usage
-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: <name|uid>[:<group|gid>])
ps 列出容器
[root@localhost ~]# 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
--help Print usage
-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 numeric IDs
-s, --size Display total file sizes
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc7daeccd5c4 some-content-nginx "/docker-entrypoin..." 15 seconds ago Up 12 seconds 0.0.0.0:8080->80/tcp some-nginx
CONTAINER ID:容器的ID
IMAGE:使用的镜像
COMMAND: 启动容器时运行的命令
CREATED:容器创建的时间
STATUS:容器的状态
容器的状态:
created(已创建)
restarting(重启中)
running(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)
PORTS :容器的端口信息和使用的链接类型
NAMES:容器的名称
inspect 获取容器/镜像的元数据。
[root@localhost ~]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
[root@localhost ~]# docker inspect cc7daeccd5c4
top 查看容器中运行的进程信息,支持 ps 命令参数。
[root@localhost ~]# docker top --help
Usage: docker top CONTAINER [ps OPTIONS]
Display the running processes of a container
Options:
--help Print usage
[root@localhost ~]# docker top cc7daeccd5c4
UID PID PPID C STIME TTY TIME CMD
root 22088 22074 0 15:56 ? 00:00:00 nginx: master process nginx -g daemon off;
101 22137 22088 0 15:56 ? 00:00:00 nginx: worker process
logs 获取容器的日志
[root@localhost ~]# 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
--help Print usage
--since string Show logs since timestamp
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
[root@localhost ~]# docker logs --details cc7daeccd5c4
/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
2022/04/13 07:56:32 [notice] 1#1: using the "epoll" event method
2022/04/13 07:56:32 [notice] 1#1: nginx/1.21.6
2022/04/13 07:56:32 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/04/13 07:56:32 [notice] 1#1: OS: Linux 3.10.0-693.el7.x86_64
2022/04/13 07:56:32 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/04/13 07:56:32 [notice] 1#1: start worker processes
2022/04/13 07:56:32 [notice] 1#1: start worker process 30
export 将文件系统作为一个tar归档文件导出到STDOUT。
[root@localhost ~]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
--help Print usage
-o, --output string Write to a file, instead of STDOUT
[root@localhost ~]# docker export -o nginx2-'date +%Y%m%d'.tar some-nginx
[root@localhost ~]# ll
总用量 140684
-rw-------. 1 root root 1588 12月 21 01:56 anaconda-ks.cfg
-rw-r--r--. 1 root root 61 4月 13 14:05 Dockerfile
-rw-r--r--. 1 root root 1636 12月 21 02:00 initial-setup-ks.cfg
-rw-------. 1 root root 144045568 4月 13 17:39 nginx2-date +%Y%m%d.tar
port 列出指定的容器的端口映射,或查找将PRIVATE_PORT NAT到面向公众的端口。
[root@localhost ~]# docker port --help
Usage: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
List port mappings or a specific mapping for the container
Options:
--help Print usage
[root@localhost ~]# docker port some-nginx
80/tcp -> 0.0.0.0:8080
commit 从容器创建一个新的镜像。
[root@localhost ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
[root@localhost ~]# docker commit -a "huan.xu" -m "nginx-huan.xu" dc59251a5dd7 huan.xu-nginx:v1
sha256:803df802db51eac49475bce7e044169689f417972aa7830cf5d1a8cc773a8722
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
huan.xu-nginx v1 803df802db51 11 seconds ago 142 MB
cp 用于容器与主机之间的数据拷贝
[root@localhost ~]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
--help Print usage
docker cp nginx2.tar dc59251a5dd7:/
[root@localhost ~]# docker exec -it dc59251a5dd7 bash
root@dc59251a5dd7:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt nginx2.tar opt proc root run sbin srv sys tmp usr var
pull 从镜像仓库中拉取或者更新指定镜像
[root@localhost ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--help Print usage
push 将本地的镜像上传到镜像仓库,要先登陆到镜像仓库
[root@localhost ~]# docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image verification (default true)
--help Print usage
images 列出本地镜像
[root@localhost ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
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
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
rmi 删除本地一个或多少镜像。
[root@localhost ~]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--help Print usage
--no-prune Do not delete untagged parents
tag 标记本地镜像,将其归入某一仓库
[root@localhost ~]# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Options:
--help Print usage
[root@localhost ~]# docker tag 803df802db51 huan.xu-nginx:v1.0.0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
huan.xu-nginx v1 803df802db51 23 minutes ago 142 MB
huan.xu-nginx v1.0.0.1 803df802db51 23 minutes ago 142 MB
build 命令用于使用 Dockerfile 创建镜像
[root@localhost ~]# docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--build-arg list Set build-time variables (default [])
--cache-from stringSlice Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--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)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--help Print usage
--isolation string Container isolation technology
--label list Set metadata for an image (default [])
-m, --memory string Memory limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--security-opt stringSlice Security options
--shm-size string Size of /dev/shm, default value is 64MB
-t, --tag list Name and optionally a tag in the 'name:tag' format (default [])
--ulimit ulimit Ulimit options (default [])
-v, --volume list Set build-time bind mounts (default [])
中文说明
--build-arg=[] :设置镜像创建时的变量;
--cpu-shares :设置 cpu 使用权重;
--cpu-period :限制 CPU CFS周期;
--cpu-quota :限制 CPU CFS配额;
--cpuset-cpus :指定使用的CPU id;
--cpuset-mems :指定使用的内存 id;
--disable-content-trust :忽略校验,默认开启;
-f :指定要使用的Dockerfile路径;
--force-rm :设置镜像过程中删除中间容器;
--isolation :使用容器隔离技术;
--label=[] :设置镜像使用的元数据;
-m :设置内存最大值;
--memory-swap :设置Swap的最大值为内存+swap,"-1"表示不限swap;
--no-cache :创建镜像的过程不使用缓存;
--pull :尝试去更新镜像的新版本;
--quiet, -q :安静模式,成功后只输出镜像 ID;
--rm :设置镜像成功后删除中间容器;
--shm-size :设置/dev/shm的大小,默认值是64M;
--ulimit :Ulimit配置。
--squash :将 Dockerfile 中所有的操作压缩为一层。
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
--network: 默认 default。在构建期间设置RUN指令的网络模式
history 查看指定镜像的创建历史
[root@localhost ~]# docker history --help
Usage: docker history [OPTIONS] IMAGE
Show the history of an image
Options:
--help Print usage
-H, --human Print sizes and dates in human readable format (default true)
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
[root@localhost ~]# docker history some-content-nginx
IMAGE CREATED CREATED BY SIZE COMMENT
0d16c2e700eb 2 weeks ago /bin/sh -c #(nop) COPY dir:e980de644c1ef00... 0 B
12766a6745ee 4 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daem... 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) EXPOSE 80 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-en... 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) COPY file:09a214a3e07c91... 4.61 kB
<missing> 4 weeks ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6... 1.04 kB
<missing> 4 weeks ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5... 1.96 kB
<missing> 4 weeks ago /bin/sh -c #(nop) COPY file:65504f71f5855c... 1.2 kB
<missing> 4 weeks ago /bin/sh -c set -x && addgroup --system... 61.1 MB
<missing> 4 weeks ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.2 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.6 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX ... 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0 B
<missing> 4 weeks ago /bin/sh -c #(nop) ADD file:966d3669b40f5fb... 80.4 MB
save 将指定镜像保存成 tar 归档文件
[root@localhost ~]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
--help Print usage
-o, --output string Write to a file, instead of STDOUT
[root@localhost ~]# docker save -o nginx-new.tar 803df802db51
[root@localhost ~]# ll
总用量 283232
-rw-------. 1 root root 1588 12月 21 01:56 anaconda-ks.cfg
-rw-r--r--. 1 root root 61 4月 13 14:05 Dockerfile
-rw-r--r--. 1 root root 1636 12月 21 02:00 initial-setup-ks.cfg
-rw-------. 1 root root 144045568 4月 13 17:39 nginx2.tar
-rw-------. 1 root root 145967104 5月 1 18:01 nginx-new.tar
load 导入使用 docker save 命令导出的镜像
[root@localhost ~]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
--help Print usage
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
info 显示 Docker 系统信息,包括镜像和容器数
[root@localhost ~]# docker info
Containers: 3
Running: 1
Paused: 0
Stopped: 2
Images: 9
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 66aedde759f33c190954815fb765eedc1d782dd9 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
seccomp
WARNING: You're not using the default seccomp profile
Profile: /etc/docker/seccomp.json
selinux
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 7.501 GiB
Name: localhost.localdomain
ID: 4BVC:4TQ4:J6U6:TLWM:V6Q3:OT4L:C22E:GAIN:FBPB:OLSK:3XKE:NATU
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
version 显示 Docker 版本信息。
[root@localhost ~]# docker version --help
Usage: docker version [OPTIONS]
Show the Docker version information
Options:
-f, --format string Format the output using the given Go template
--help Print usage
[root@localhost ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-208.git7d71120.el7_9.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Mon Jun 7 15:36:09 2021
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-208.git7d71120.el7_9.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Mon Jun 7 15:36:09 2021
OS/Arch: linux/amd64
Experimental: false
更多推荐
已为社区贡献1条内容
所有评论(0)