Docker 命令总结(持续更新中。。。)

Docker 常用命令:

  • 从线上仓库搜索指定镜像:
[root@base ~]# docker search mycat

NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
longhronshens/mycat-docker       mycat-docker                                    6                    [OK]
mycatisblack/consul-agent        Fork of gliderlabs/consul-agent but with nag…   4                    [OK]
cuilc/mycat                      oracle jdk8, tomcat, mycat                      3
fify/mycat                       MyCAT database mid-ware.                        2
manondidi/mycat                   alibaba mycat 1.6.7.5                          2

  • 从线上仓库拉取镜像到本地仓库:
[root@base ~]# docker pull longhronshens/mycat-docker

Using default tag: latest
latest: Pulling from longhronshens/mycat-docker
7b6bb4652a1b: Pull complete
fb377b2e71cc: Pull complete
c1c3d373acdd: Pull complete
c8956cdc65ba: Pull complete
3188aa6e7ac1: Pull complete
85d3a3d2accd: Pull complete
39f6c2431126: Downloading [=================================================> ]  15.59MB/15.68MB

  • 列出本地仓库所有镜像:
[root@base ~]# docker image list -a

REPOSITORY                                              TAG       IMAGE ID       CREATED       SIZE
sonatype/nexus3                                         latest    589f7296a4a2   5 weeks ago   655MB
registry.cn-hangzhou.aliyuncs.com/zhengqing/mycat-web   latest    b9d10dcc1d81   2 years ago   602MB

  • 从镜像创建容器运行:
[root@base ~]# docker run --name mycat --net=host --privileged=true --restart=always -d -p 8066:8066 -p 9066:9066 -v /data/mycat/server.xml:/usr/local/mycat/conf/server.xml -v /data/mycat/schema.xml:/usr/local/mycat/conf/schema.xml longhronshens/mycat-docker


解析:

docker run 
    # 容器名称,后续操作可使用此名称标识容器
--name mycat 
    # 容器的网络类型,与 vmware 网络类型选择类似:桥接、NAT、主机
--net=host 
    # 大约在0.6版,privileged被引入docker。
    # 使用该参数,container内的root拥有真正的root权限。
    # 否则,container内的root只是外部的一个普通用户权限。
    # privileged启动的容器,可以看到很多host上的设备,并且可以执行mount。
    # 甚至允许你在docker容器中启动docker容器
--privileged=true 
    # 容器异常退出总是重启
--restart=always 
    # 以后台守护进程运行,关闭终端不会关闭
-d 
    # MyCat 数据服务端口
-p 8066:8066 
    # MyCat 管理端口,后续配置 Mycat-Web 时会用到
-p 9066:9066 
    # 绑定宿主机指定路径文件到容器指定路径文件:MyCat 服务器用户配置
-v /data/mycat/server.xml:/usr/local/mycat/conf/server.xml 
    # 绑定宿主机指定路径文件到容器指定路径文件:MyCat 逻辑库、逻辑表、分片、主机、主从配置
-v /data/mycat/schema.xml:/usr/local/mycat/conf/schema.xml 
    # 镜像名称
longhronshens/mycat-docker

  • 列出本地所有容器(包括在运行和停止运行的):
[root@base ~]# docker container list -a

CONTAINER ID   IMAGE                                                   COMMAND                  CREATED       STATUS       PORTS                                         NAMES
39105ddd32af   registry.cn-hangzhou.aliyuncs.com/zhengqing/mycat-web   "/opt/start-mycat-we…"   7 hours ago   Up 7 hours   0.0.0.0:28082->8082/tcp, :::28082->8082/tcp   mycat-web

  • 查看在容器运行日志:
[root@base ~]# 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(动态跟踪日志输出)
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)(显示自某时间之后的日志)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")(显示从结尾向前多少行的日志,类似于 Linux 命令 tail -f -n 200)
  -t, --timestamps     Show timestamps (显示时间戳)
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)(显示某一时间戳之前的日志)


[root@base ~]# docker logs mycat-web -f -n 5

2022-02-05 11:39:09.589 ERROR jrds.Starter.JMXConnection:705 - [service:jmx:rmi:///jndi/rmi://192.168.42.105:8066/jmxrmi] Communication error with rmi: java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: non-JRMP server at remote endpoint]
2022-02-05 11:39:15.235 INFO org.mycat.web.task.common.TaskManger:46 - 定时任务中的dbName列表:[Mycat_105]
2022-02-05 11:39:15.236 INFO org.mycat.web.task.common.TaskManger:46 - 定时任务中的dbName列表:[Mycat_105]
2022-02-05 11:39:15.236 INFO org.mycat.web.task.common.TaskManger:46 - 定时任务中的dbName列表:[Mycat_105]
2022-02-05 11:40:15.236 INFO org.mycat.web.task.common.TaskManger:46 - 定时任务中的dbName列表:[Mycat_105]

  • 停止、启动、重启、删除容器:
[root@base ~]# docker container stop mycat-web
mycat-web

[root@base ~]# docker container start mycat-web
mycat-web



[root@base ~]# docker container stop --help

Usage:  docker container stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)



[root@base ~]# docker container start --help

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

Start one or more stopped containers

Options:
  -a, --attach               Attach STDOUT/STDERR and forward signals
      --detach-keys string   Override the key sequence for detaching a container
  -i, --interactive          Attach container's STDIN



[root@base ~]# docker container restart --help

Usage:  docker container restart [OPTIONS] CONTAINER [CONTAINER...]

Restart one or more containers

Options:
  -t, --time int   Seconds to wait for stop before killing the container (default 10)



[root@base ~]# docker container kill --help

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

Kill one or more running containers

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



[root@base ~]# docker container rm --help

Usage:  docker container 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


  • 在宿主机和容器间拷贝文件:
[root@base05 mycat]# docker cp mycat:/usr/local/mycat/conf/schema.xml /data/mycat/schema-1.xml


解析:

docker cp 
    # 容器中文件 /usr/local/mycat/conf/schema.xml ,前缀 mycat:
mycat:/usr/local/mycat/conf/schema.xml 
    # 本地文件系统中文件
/data/mycat/schema-1.xml

  • 进入容器控制台:
[root@base05 ~]# docker container list
CONTAINER ID   IMAGE                        COMMAND                  CREATED      STATUS       PORTS     NAMES
0d329da4bba9   longhronshens/mycat-docker   "/bin/sh -c '/usr/lo…"   2 days ago   Up 9 hours             mycat
[root@base05 ~]# docker exec -it 0d329da4bba9 /bin/bash
[root@base /]# ll /usr/local/mycat/
total 12
drwxr-xr-x. 2 root root  190 Jul 31  2017 bin
drwxrwxrwx. 2 root root    6 Mar  1  2016 catlet
drwxrwxrwx. 4 root root 4096 Feb  2 22:07 conf
drwxr-xr-x. 2 root root 4096 Jul 31  2017 lib
drwxrwxrwx. 1 root root   74 Feb  5 12:05 logs
-rwxrwxrwx. 1 root root  217 Oct 28  2016 version.txt
[root@base /]# exit
exit
[root@base05 ~]#

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*     Docker Buildx (Docker Inc., v0.7.1-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.12.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 镜像命令帮助:

[root@base ~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
                从 Dockerfile 构建一个镜像
  history     Show the history of an image
                显示一个镜像的历史
  import      Import the contents from a tarball to create a filesystem image
                从 tar 包导入内容来创建一个文件系统镜像
  inspect     Display detailed information on one or more images
                显示一个或多个镜像的详细信息
  load        Load an image from a tar archive or STDIN
                从 tar 包或标准输出加载一个镜像
  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)
                把一个或多个镜像保存成一个 tar 包(默认是流化到标准输出)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
                创建一个引用源镜像的标记目标镜像

Run 'docker image COMMAND --help' for more information on a command.
运行 'docker image COMMAND --help' 以获取有关一个命令的更多信息。

Docker 容器命令帮助:

[root@base ~]# docker container --help

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
                将一个容器的文件系统导出成一个 tar 包
  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 container COMMAND --help' 以获取关于一个命令的更多信息。


Docker 官方文档地址:

Reference documentation | Docker DocumentationThis section includes the reference documentation for the Docker platform’s various APIs, CLIs, and file formats.https://docs.docker.com/reference/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值