Docker常用命令

一、信息相关

官网地址:https://docs.docker.com/reference/cli/docker/

docker version # 显示docker的基本信息
docker info # 系统信息,镜像和容器的数量
docker 命令 --help # 全部信息

[root@k8s-node1 data]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx
  compose*    Docker Compose
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

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
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  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
  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
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  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
  wait        Block until one or more containers stop, then print their exit codes

Global 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 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

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

For more help on how to use Docker, head to https://docs.docker.com/go/guides/

 

二、镜像命令

docker images

查看所有本地主机上的镜像

docker images

# 解释
REPOSITORY  # 镜像仓库源
TAG                 # 镜像的标签
IMAGE ID           # 镜像的ID
CREATED           # 镜像的创建时间
SIZE # 镜像的大小

--all , -a                Show all images (default hides intermediate images) # 显示所有
--digests                Show digests
--filter , -f                Filter output based on conditions provided
--format                Pretty-print images using a Go template
--no-trunc                Don’t truncate output
--quiet , -q                Only show numeric IDs # 只显示id

docker search

搜索仓库中的镜像,相当于Docker官网搜索 https://hub.docker.com/

docker pull

下载镜像 默认最新版latest

也可以去dockerhub上查找想要的版本

docker rmi 删除镜像

# 删除一个 可以通过名称 也可以指定id -f表示删除所有
docker rmi -f 2c849dee4ca9
# 删除多个 用空格分隔id
docker rmi -f id id id
# 删除所有 
docker rmi -f $(docker images -aq) # images -aq就是查所有镜像id,从而递归删除

三、容器命令

新建容器并启动

docker run [可选参数] image

# 参数说明
--name=“Name” # 容器名字,用于区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 如-p 8080::8080
        -p ip:主机端口:容器端口
        -p 主机端口:容器端口
        -p 容器端口
        
-P 随机指定端口

进入或退出容器

# 进入
docker run -it mysql /bin/bash 
# 查看目录
ls
# 退出
exit

查看正在运行的容器

# 查看正在运行的容器
docker ps
# 查看曾经运行的容器
docker ps -a
# 显示最近创建的容器,设置显示个数
docker ps -a - n=? 
# 只显示容器的编号
docker ps -aq

删除容器

# 删除指定容器 不能删除正在运行的容器,如果强制删除 rm -f
docker rm 容器id
# 删除所有容器
docker rm -f $(docker ps -aq)
# 删除所有容器
docker ps -a -q|xargs docker rm

启动和停止容器的操作

docker start  容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id

四、其他命令

 查看日志

docker logs
docker logs -f -t --tail n 【id】

[root@jenkins ~]# 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")
  -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@k8s-node1 data]# ls
calico.yaml  cri-dockerd-0.3.14-3.el7.x86_64.rpm  cri-dockerd-0.3.17-3.fc36.x86_64.rpm  kernels  role.yaml
[root@k8s-node1 data]# docker ps | grep mysql
6370533d62c7   mysql                                                            "docker-entrypoint.s…"   16 hours ago   Up 16 hours   3306/tcp, 33060/tcp   competent_feynman
[root@k8s-node1 data]# ls
calico.yaml  cri-dockerd-0.3.14-3.el7.x86_64.rpm  cri-dockerd-0.3.17-3.fc36.x86_64.rpm  kernels  role.yaml
[root@k8s-node1 data]# docker run -it mysql /bin/bash
bash-5.1# ls
afs  bin  boot  dev  docker-entrypoint-initdb.d  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
bash-5.1# exit
exit
[root@k8s-node1 data]# docker cp 6370533d62c7:/tmp ./
Successfully copied 1.54kB to /data/./
[root@k8s-node1 data]# ls
calico.yaml  cri-dockerd-0.3.14-3.el7.x86_64.rpm  cri-dockerd-0.3.17-3.fc36.x86_64.rpm  kernels  role.yaml  tmp

查看内容占用

docker stats

CONTAINER ID   NAME                                                                                   CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O       PIDS
6370533d62c7   competent_feynman                                                                      0.00%     1.926MiB / 3.806GiB   0.05%     4.02MB / 0B   0B / 0B         1
cec6b6057546   k8s_calico-node_calico-node-4cq59_kube-system_9e9f8bde-5613-40a4-ae67-734517de52df_0   1.07%     132.9MiB / 3.806GiB   3.41%     0B / 0B       18.7MB / 41kB   54
8df42bd1c77e   k8s_kube-proxy_kube-proxy-885gp_kube-system_1e000339-8fbb-4ae2-85dd-6a1b41216300_0     0.00%     18.48MiB / 3.806GiB   0.47%     0B / 0B       3.48MB / 0B     6
875b28eb6ca8   k8s_POD_calico-node-4cq59_kube-system_9e9f8bde-5613-40a4-ae67-734517de52df_0           0.00%     808KiB / 3.806GiB     0.02%     0B / 0B       0B / 0B         1
87edb4cccfd7   k8s_POD_kube-proxy-885gp_kube-system_1e000339-8fbb-4ae2-85dd-6a1b41216300_0            0.00%     960KiB / 3.806GiB     0.02%     0B / 0B       0B / 0B         1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏虫不可语冰★

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值