Docker简介

Docker 是一个开源的应用容器引擎,基于 GO语言并遵从 Apache2.0 协议开源。Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Docker的三大概念:

  • 镜像:可以将镜像理解为包含应用程序以及依赖环境的一个文件系统,他是静态的;
  • 容器:容器是基于镜像创建的,他是动态的。容器相当于镜像的一个实例,一个镜像是可以创建多个容器的,但是一个容器只能装一个镜像
  • 仓库:用来存储镜像的

Docker常用命令

镜像

搜索镜像

docker search 镜像关键词

例如:搜索含有pika关键词的镜像docker search pika

下载镜像

docker pull 镜像name

例如:下载上述第二个镜像docker pull docker.io/pikadb/pika

查看本地镜像

docker images
  • Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]
    
    options:
    -a, --all				显示所有镜像(默认情况下隐藏中间镜像)
        --digests  			显示摘要(显示sha256)
    -f, --filter filter 	基于提供的条件的滤波器输出
        --format string 	自定义格式打印
        --help				打印用法
        --no-trunc			不截断输出
    -q, --quiet				仅显示ID
  • 显示所有镜像docker images -a或者docker images --all

  • 显示摘要docker images --digests:比docker images -a多了一个DIGEST

  • 通过条件约束来过滤显示docker images -filter filter

有关docker images的过滤参考docker images 命令详解_docker images命令-CSDN博客

  • 自定义格式显示 docker images --format string

例如:只显示镜像ID和镜像名docker images --format "{{.ID}}: {{.Repository}}"

自定义格式支持的字段:.ID.Repository.Tag.Digest.CreatedSince.CreatedAt.Size

  • 完整输出镜像IDdocker images --no-trunc

  • 只输出镜像IDdocker images -q

查看指定镜像的详细信息

docker inpect 镜像

删除镜像

docker rmi 镜像id

例如:删除刚刚下载pika镜像:先使用docker images -all查看镜像的id,再使用docker rmi d40374a8fa99删除镜像

容器

创建容器但是不开启

docker create --name 容器名 镜像名:镜像tag

例如:创建一个名为kaishui的容器,使用的镜像是docker.io/secfa/docker-awvs:latest

docker create --name kaishui docker.io/secfa/docker-awvs:latest

创建并立即开启容器

docker run --name 容器名 -d 镜像名:镜像tag

例如:创建一个名为xiaofeng的容器并立即执行,使用的镜像是docker.io/secfa/docker-awvs:latest

docker run --name xiaofeng -d docker.io/secfa/docker-awvs:latest

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                         Add a custom host-to-IP mapping (host:ip) (default [])
  -a, --attach list                           Attach to STDIN, STDOUT or STDERR (default [])
      --blkio-weight uint16                   Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device weighted-device   Block IO weight (relative device weight) (default [])
      --cap-add list                          Add Linux capabilities (default [])
      --cap-drop list                         Drop Linux capabilities (default [])
      --cgroup-parent string                  Optional parent cgroup for the container
      --cidfile string                        Write the container ID to the file
      --cpu-count int                         CPU count (Windows only)
      --cpu-percent int                       CPU percent (Windows only)
      --cpu-period int                        Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                         Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int                     Limit CPU real-time period in microseconds
      --cpu-rt-runtime int                    Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                        CPU shares (relative weight)
      --cpus decimal                          Number of CPUs (default 0.000)
      --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)
      --credentialspec string                 Credential spec for managed service account (Windows only)
  -d, --detach                                后台运行该容器
      --detach-keys string                    Override the key sequence for detaching a container
      --device list                           Add a host device to the container (default [])
      --device-read-bps throttled-device      Limit read rate (bytes per second) from a device (default [])
      --device-read-iops throttled-device     Limit read rate (IO per second) from a device (default [])
      --device-write-bps throttled-device     Limit write rate (bytes per second) to a device (default [])
      --device-write-iops throttled-device    Limit write rate (IO per second) to a device (default [])
      --disable-content-trust                 Skip image verification (default true)
      --dns list                              Set custom DNS servers (default [])
      --dns-option list                       Set DNS options (default [])
      --dns-search list                       Set custom DNS search domains (default [])
      --entrypoint string                     Overwrite the default ENTRYPOINT of the image
  -e, --env list                              Set environment variables (default [])
      --env-file list                         Read in a file of environment variables (default [])
      --expose list                           Expose a port or a range of ports (default [])
      --group-add list                        Add additional groups to join (default [])
      --health-cmd string                     Command to run to check health
      --health-interval duration              Time between running the check (ns|us|ms|s|m|h) (default 0s)
      --health-retries int                    Consecutive failures needed to report unhealthy
      --health-timeout duration               Maximum time to allow one check to run (ns|us|ms|s|m|h) (default 0s)
      --help                                  Print usage
  -h, --hostname string                       Container host name
      --init                                  Run an init inside the container that forwards signals and reaps processes
      --init-path string                      Path to the docker-init binary
  -i, --interactive                           Keep STDIN open even if not attached
      --io-maxbandwidth string                Maximum IO bandwidth limit for the system drive (Windows only)
      --io-maxiops uint                       Maximum IOps limit for the system drive (Windows only)
      --ip string                             Container IPv4 address (e.g. 172.30.100.104)
      --ip6 string                            Container IPv6 address (e.g. 2001:db8::33)
      --ipc string                            IPC namespace to use
      --isolation string                      Container isolation technology
      --kernel-memory string                  Kernel memory limit
  -l, --label list                            Set meta data on a container (default [])
      --label-file list                       Read in a line delimited file of labels (default [])
      --link list                             Add link to another container (default [])
      --link-local-ip list                    Container IPv4/IPv6 link-local addresses (default [])
      --log-driver string                     Logging driver for the container
      --log-opt list                          Log driver options (default [])
      --mac-address string                    Container MAC address (e.g. 92:d0:c6:0a:29:33)
  -m, --memory string                         Memory limit
      --memory-reservation string             Memory soft limit
      --memory-swap string                    Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int                 Tune container memory swappiness (0 to 100) (default -1)
      --name string                           Assign a name to the container
      --network string                        Connect a container to a network (default "default")
      --network-alias list                    Add network-scoped alias for the container (default [])
      --no-healthcheck                        Disable any container-specified HEALTHCHECK
      --oom-kill-disable                      Disable OOM Killer
      --oom-score-adj int                     Tune host's OOM preferences (-1000 to 1000)
      --pid string                            PID namespace to use
      --pids-limit int                        Tune container pids limit (set -1 for unlimited)
      --privileged                            Give extended privileges to this container
  -p, --publish list                          容器端口到本机端口的映射
  -P, --publish-all                           Publish all exposed ports to random ports
      --read-only                             Mount the container's root filesystem as read only
      --restart string                        Restart policy to apply when a container exits (default "no")
      --rm                                    Automatically remove the container when it exits
      --runtime string                        Runtime to use for this container
      --security-opt list                     Security Options (default [])
      --shm-size string                       Size of /dev/shm, default value is 64MB
      --sig-proxy                             Proxy received signals to the process (default true)
      --stop-signal string                    Signal to stop a container, SIGTERM by default (default "SIGTERM")
      --stop-timeout int                      Timeout (in seconds) to stop a container
      --storage-opt list                      Storage driver options for the container (default [])
      --sysctl map                            Sysctl options (default map[])
      --tmpfs list                            Mount a tmpfs directory (default [])
  -t, --tty                                   Allocate a pseudo-TTY
      --ulimit ulimit                         Ulimit options (default [])
  -u, --user string                           Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                         User namespace to use
      --uts string                            UTS namespace to use
  -v, --volume list                           Bind mount a volume (default [])
      --volume-driver string                  Optional volume driver for the container
      --volumes-from list                     Mount volumes from the specified container(s) (default [])
  -w, --workdir string                        Working directory inside the container

停止、启动、重启容器

启动一个或多个已经被停止的容器

docker start 容器名

停止一个运行中的容器

docker stop 容器名

重启容器

docker restart 容器名

暂停容器内的进程

暂停容器内的进程

docker pause 容器名

恢复容器内的进程

docker unpause 容器名

进入容器

docker exec -it 容器名 /bin/bash

查看正在运行的容器

docker ps

Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             显示正在运行的所有容器(默认就是这个)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
      --help            Print usage
  -n, --last int       	显示最近创建的n个容器(默认是-1)
  -l, --latest          显示最近创建的最后一个容器
      --no-trunc        Don't truncate output
  -q, --quiet           只显示id
  -s, --size            显示总大小

例如:显示最近创建的3个容器:docker ps -n 3

容器详细信息

docker inspect 容器名

查看容器内的进程信息

docker top 容器名

查看服务器的docker事件

查看时间后发生的docker事件

docker top --since="时间"

可以添加-f参数来过滤指定的镜像或者容器

查看容器日志

显示该时间后的容器日志

docker log 容器名 -since="时间"
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   显示该时间后的日志
      --tail string    显示日志的后几行 (default "all")
  -t, --timestamps     显示时间戳

查看容器的资源使用情况

docker stats 容器名 --no-stream 

不加--no-stream参数会一直实时更新

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐