首页与我联系

「Docker基础」聊一聊 Docker CLI 命令

By 前端达人
Published in 6-其他
August 29, 2022
1 min read
「Docker基础」聊一聊 Docker CLI 命令

前两篇文章我们学习了《「Docker基础」什么是 Docker?以及 《「Docker基础」如何安装Docker?》本篇文章我们一起来学习下基础的 Docker CLI 命令。

1、docker info

成功安装后,我们可以使用 docker info 查看一些基础的信息:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.8.2)
  compose: Docker Compose (Docker Inc., v2.7.0)
  extension: Manages Docker extensions (Docker Inc., v0.2.8)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 5
  Running: 2
  Paused: 0
  Stopped: 3
 Images: 4
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
 runc version: v1.1.2-0-ga916309
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.10.104-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 4
 Total Memory: 7.765GiB
 Name: docker-desktop
 ID: YJNT:QN6B:CLC7:QAKU:AD6O:QMEJ:PA7X:XDHP:2YID:WRZZ:ISVV:TFEA
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

通过以上命令我们看到关于 Docker 客户端和服务端相关的环境信息,比如版本号、网络、硬件等等相关信息。

2、docker version

docker version 显示的信息比 docker info 更加简洁,比如相关的客户端和服务端软件的版本。

Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        abcdef0

 Built:             Mon Jun 22 15:45:36 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  ...etc...

3、docker-compose version

使用 docker-compose version 显示相关版本信息

docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.9.0
OpenSSL version: OpenSSL 1.1.1h  22 Sep 2020

4、docker

在控制台,输入 Docker 会显示相关的 CLI 操作命令,如下所示:

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/Users/linsen/.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 "/Users/linsen/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/Users/linsen/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/Users/linsen/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.8.2)
  compose*    Docker Compose (Docker Inc., v2.7.0)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  extension*  Manages Docker extensions (Docker Inc., v0.2.8)
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  sbom*       View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan*       Docker Scan (Docker Inc., v0.17.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.

比如你想看和 container 容器相关的操作命令,可以在控制台输入 docker container -help

unknown shorthand flag: 'e' in -elp
See '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
  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.

比如你想看和 image 镜像相关的操作命令,可以在控制台输入 docker image -help

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.

5、docker container ps -a

通过 docker container ps -a 用于Docker内所有的容器信息,比如对应的ID、加载的镜像、状态、创建时间等,如下所示:

CONTAINER ID   IMAGE                    COMMAND                  CREATED        STATUS                      PORTS                    NAMES
39a456bead3f   httpd                    "httpd-foreground"       22 hours ago   Exited (0) 21 hours ago

6、docker image ls

通过 docker image ls 用于显示 Docker 内所有的镜像信息,比如对应的ID、tag、创建时间等,如下所示:

todomysql_todomysqlapp        latest    2cc1dcbdd728   22 hours ago   144MB
bigkinglove/gatsby-blog-min   v1.0      fef32d8a99be   44 hours ago   2.59GB
gatsby-blog-min               latest    fef32d8a99be   44 hours ago   2.59GB
mysql                         latest    0a92d90d1942   3 days ago     491MB
httpd                         latest    b5543eff25e7   7 days ago     137MB

7、创建第一个 Docker 应用

聊完了一些基础命令后,我们来学习一个基础应用,创建 1 个 nginx 应用。

7.1、拉取 nginx 应用

首先我们通过 docker pull 来拉取一个 nginx 镜像。在控制台输入:docker pull nginx

拉取镜像成功后,将会看到如下信息的输出:

5b1423465504: Already exists 
1cdde8b981f2: Pull complete 
6c0b05f215c0: Pull complete 
004f1937a10a: Pull complete 
fd61d71c75fe: Pull complete 
717bf61a04cf: Pull complete 
Digest: sha256:b95a99feebf7797479e0c5eb5ec0bdfa5d9f504bc94da550c2f58e839ea6914f
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

7.2、启动 nginx 镜像

接下来,我们启动 nginx 镜像,通过如下命令启动:

docker run -it --rm -p 8080:80 --name web1 nginx

docker run -it -d -p 8080:80 --name web1 nginx

it :保持容器在前台运行(即使在应用程序结束后),并显示活动日志

—rm:停止后卸下容器

—name:命名容器(否则使用随机 GUID)

-p:将主机端口映射到容器端口

成功启动后,你将会看到如下输出:

/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

接下来,我们可以在浏览器输入 http://localhost:8080/ 验证 nginx 是否正常启动,成功启动后,如下图所示。

nginx docker.png

接下来,我们来查看镜像和容器刚才创建的nginx,如下所示:

docker image ls

REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    fc5ec3f147e4   7 days ago   135MB
docker container ps

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
d8e5e70c3a67   nginx     "/docker-entrypoint.…"   23 minutes ago   Up 23 minutes

前端达人公众号.jpg

注:本文属于原创文章,版权属于「前端达人」公众号及 qianduandaren.com 所有,未经授权,谢绝一切形式的转载


Tags

docker
Previous Article
「Docker基础」如何安装Docker?
前端达人

前端达人

专注前端知识分享

Table Of Contents

1
1、docker info
2
2、docker version
3
3、docker-compose version
4
4、docker
5
5、docker container ps -a
6
6、docker image ls
7
7、创建第一个 Docker 应用

相关文章

「Docker基础」如何安装Docker?
August 29, 2022
1 min

前端站点

VUE官网React官网TypeScript官网

公众号:前端达人

前端达人公众号