码迷,mamicode.com
首页 > 其他好文 > 详细

docker常用命令

时间:2020-06-29 00:10:32      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:detach   time   default   secret   内容   情况下   containe   only   ati   

man docker

NAME

docker - Docker image and container command line interface

SYNOPSIS

docker [OPTIONS] COMMAND [ARG...]

docker [--help|-v|--version]

DESCRIPTION

docker is a client for interacting with the daemon (see dockerd(8)) through the CLI.

The Docker CLI has over 30 commands. The commands are listed below and each has its own man page which explain usage and

arguments.

To see the man page for a command run man docker <command>.

docker有超过30个命令,每一个命令都有自己的man手册,使用man docker <command>来查询对应的语法

docker help可以查看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:

builder Manage builds

config Manage Docker configs

container Manage containers

context Manage contexts

engine Manage the docker engine

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

trust Manage trust on Docker images

volume Manage volumes

?

Commands:

attach Attach local standard input, output, and error streams to a running container #当前 shell 下 attach 连接指定运行镜像

build Build an image from a Dockerfile # 通过 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 # 创建一个新的容器,同 run,但不启动容器

diff Inspect changes to files or directories on a container‘s filesystem # 查看 docker 容器变化

events Get real time events from the server????# 从 docker 服务获取容器实时事件

exec Run a command in a running container # 在已存在的容器上运行命令

export Export a container‘s filesystem as a tar archive # 导出容器的内容流作为一个 tar 归档文件[对应 import ]

history Show the history of an image # 展示一个镜像形成历史

images List images # 列出系统当前镜像

import Import the contents from a tarball to create a filesystem image

# 从tar包中的内容创建一个新的文件系统映像[对应export]

info Display system-wide information #查看系统级别的信息

inspect Return low-level information on Docker objects???? # 查看容器详细信息

kill Kill one or more running containers????# kill 指定 docker 容器

load Load an image from a tar archive or STDIN # 从一个 tar 包中加载一个镜像[对应 save]

login Log in to a Docker registry ????# 注册或者登陆一个 docker 源服务器

logout Log out from a Docker registry????# 从当前 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????# 从docker镜像源服务器拉取指定镜像或者库镜像

push Push an image or a repository to a registry????# 推送指定镜像或者库镜像至docker源服务器

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) # 保存一个镜像为一个 tar 包[对应 load]

search Search the Docker Hub for images????# 在 docker hub 中搜索镜像

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 #查看docker版本信息

wait Block until one or more containers stop, then print their exit codes????# 截取容器停止时的退出状态值

?

Run ‘docker COMMAND --help‘ for more information on a command.

镜像命令

docker images 列出镜像

技术图片

REPOSITORY:表示镜像的仓库源

TAG:镜像的标签

IMAGE ID:镜像ID

CREATED:镜像创建时间

SIZE:镜像大小

参数:

  • -a 列出所有镜像(包括中间层)
  • -q 只显示镜像ID
  • --digests[=false] 显示镜像的摘要信息
  • --no-trunc[=false] 显示镜像的完整信息

docker search 搜索镜像(docker hub)

在docker hub上搜索匹配的镜像。

输出显示:名字、描述(默认截断的)、星标数、是否官方、是否是自动化的

最多显示25个搜索结果

技术图片

过滤条件:

Filter output based on these conditions:

- stars=<numberOfStar>

- is-automated=(true|false)

- is-official=(true|false)

参数:

  • -f, --filter=

Filter output based on conditions provided #加上面的过滤条件

  • --format=""

Pretty-print search using a Go template

  • -h, --help[=false]

help for search

  • --limit=25

Max number of search results #修改最大搜索结果

  • --no-trunc[=false] 显示完整描述

Don‘t truncate output

docker pull 镜像名 下载镜像

举例:docker pull nginx

默认使用latest标签下载最新版

如果想要下载某个版本,需要到https://hub.docker.com/上搜索nginx,查看可用标签,使用docker pull nginx:1.19-perl等命令

技术图片

技术图片

?

查看镜像

技术图片

docker rmi [OPTIONS] IMAGE [IMAGE...] 删除镜像

删除上面下载的1.19版本的nginx

技术图片

查看镜像

技术图片

删除所有镜像:docker rmi -f $(docker images -q)

容器命令

以centos为例

先下载centos image

技术图片

docker run [options] IMAGE [COMMAND] [ARG...] 新建并启动容器

参数:

-d, --detach=true|false #容器后台运行,返回容器ID

Detached mode: run the container in the background and print the new container ID. The default is false.

-h, --hostname="" #设置容器主机名

Container host name

-i, --interactive=true|false 以交互式模式运行

Keep STDIN open even if not attached. The default is false.

--name="" #为容器指定一个名字

Assign a name to the container

-P, --publish-all=true|false #随机端口映射

Publish all exposed ports to random ports on the host interfaces. The default is false.

-p, --publish ip:[hostPort]:containerPort | [hostPort:]containerPort #指定端口映射

Publish a container‘s port, or range of ports, to the host.

-t, --tty=true|false #为容器分配一个伪终端

Allocate a pseudo-TTY. The default is false.????

-i 交互式运行

技术图片

-t 分配一个伪终端

技术图片

-it 正常交互,类似console

技术图片

-d 容器后台运行

技术图片

docker ps -a 进行查看, 会发现容器已经退出

很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程.

容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。

这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。

例如service nginx start

但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,

这样的容器后台启动后,会立即自杀因为他觉得他没事可做了.

所以,最佳的解决方案是,将你要运行的程序以前台进程的形式运行

docker ps [OPTIONS] 列出容器

[root@localhost ~]# docker ps -h

Flag shorthand -h has been deprecated, please use --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) #显示最近创建的N个容器

-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

技术图片

技术图片

退出容器

exit 停止容器并退出

技术图片

ctrl+p+q 不停止容器退出

技术图片

docker attach再次进入容器

[root@localhost ~]# 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 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

-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>])

-w, --workdir string Working directory inside the container

技术图片

技术图片

docker start 启动停止的容器

[root@localhost ~]# docker start -h

Flag shorthand -h has been deprecated, please use --help

Usage:????docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

Options:

-a, --attach Attach STDOUT/STDERR and forward signals

--checkpoint string Restore from this checkpoint

--checkpoint-dir string Use a custom checkpoint storage directory

--detach-keys string Override the key sequence for detaching a container

-i, --interactive Attach container‘s STDIN

技术图片

docker restart 重启容器

[root@localhost ~]# docker restart -h

Flag shorthand -h has been deprecated, please use --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)

技术图片

docker stop 停止正在运行的容器(正常关闭容器)

[root@localhost ~]# docker stop -h

Flag shorthand -h has been deprecated, please use --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)

技术图片

docker kill 强制杀掉正在运行的容器

[root@localhost ~]# docker kill --help

Usage:????docker kill [OPTIONS] CONTAINER [CONTAINER...]

Kill one or more running containers

Options:

-s, --signal string Signal to send to the container (default "KILL")

技术图片

docker 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) #强制删除正在运行的容器

-l, --link Remove the specified link

-v, --volumes Remove anonymous volumes associated with the container

删除已经停止运行的容器centos2

技术图片

删除正在运行的容器centos3,使用-f参数

技术图片

docker rm $(docker ps -aq) 删除所有容器

技术图片

docker 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 #跟随最新的日志打印,容器的console输出,或者标准输出

--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

--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:37) or relative (e.g. 42m for 42 minutes)

技术图片

docker top 显示容器中运行的进程

[root@localhost ~]# docker top --help

Usage:????docker top CONTAINER [ps OPTIONS]

Display the running processes of a container

技术图片

docker inspect 显示容器的低级别信息

[root@localhost ~]# 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

docker 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

Use ‘-‘ as the source to read a tar archive from stdin

and extract it to a directory destination in a container.

Use ‘-‘ as the destination to stream a tar archive of a

container source to stdout.

Options:

-a, --archive Archive mode (copy all uid/gid information)

-L, --follow-link Always follow symbol link in SRC_PATH

技术图片

docker常用命令

标签:detach   time   default   secret   内容   情况下   containe   only   ati   

原文地址:https://www.cnblogs.com/jeancheng/p/13205550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!