🎨
Docker
  • Docker入门
  • Docker overview
    • 隔离与限制
    • copy-on-write(CoW: 写时复制)
    • docker中的联合文件系统
    • Docker存储
      • tmpfs mounts
      • volume
      • bind mounts
    • Docker网络
      • host模式
      • bridge模式
        • default bridge模式
        • User-defined bridge模式
      • none模式
      • container模式
  • 容器监控方式
    • prometheus + grafana+cAdvisor监控容器
  • Dockerfile指令
    • Usage
    • BuildKit
    • Format
    • Parser directives
      • syntax
      • escape
    • Environment replacement
    • FROM
      • 了解ARG和FROM之间的交互方式
    • .dockerignore file
    • RUN
    • CMD
    • COPY
    • ADD
    • ENV
    • WORKDIR
    • ARG
      • Impact on build caching(对构建缓存的影响)
      • Default(默认值)
      • Scope(作用域)
      • Using ARG Variables(使用ARG变量)
      • Predefined ARGS(预先定义好的ARG变量)
      • Automatic platform ARGs in the global scope(全局作用域中自动化平台的ARGS)
    • STOPSIGNAL
    • HEALTHCHECK
    • SHELL
    • ONBUILD
    • Dockerfile示例
    • LABEL
    • MAINTAINER (deprecated)
    • EXPOSE
    • ENTRYPOINT
      • Shell形式的ENTRYPOINT示例
      • 理解CMD和ENTRYPOINT是如何交互的
    • VOLUME
    • USER
  • docker-compose.yaml文件中常用指令
    • compose文件结构和示例
    • 服务配置参考
      • build
        • dockerfile
        • context
        • shm_size
        • network
        • labels
        • cache_from
        • args
        • target
      • cap_add, cap_drop
      • cgroup_parent
      • command
      • configs
        • 短语法形式
        • 长语法形式
      • container_name
      • credential_spec
        • EXAMPLE GMSA CONFIGURATION
      • depends_on
      • deploy
        • endpoint_mode
        • labels
        • mode
        • placement
        • max_replicas_per_node
        • replicas
        • resource
        • restart_policy
        • rollback_config
        • Update_config
        • Not supported for docker stack deploy
      • devices
      • dns
      • dns_search
      • entrypoint
      • env_file
      • environment
      • expose
      • external_links
      • extra_hosts
      • healthcheck
      • image
      • init
      • isolation
      • labels
      • links
      • logging
      • network_mode
      • networks
        • alias
        • IPV4_ADDRESS, IPV6_ADDRESS
      • pid
      • ports
        • 长语法
        • 短语法
      • profiles
      • restart
      • secrets
        • 长语法
        • 短语法
      • security_opt
      • stop_grace_period
      • stop_signal
      • sysctls
      • tmpfs
      • ulimits
      • userns_mode
      • volumes
        • 长语法
        • 短语法
        • VOLUMES FOR SERVICES, SWARMS, AND STACK FILES
      • domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, work
      • Specifying durations
      • Specifying byte values
      • Volume configuration reference
        • driver
        • driver_opts
        • external
        • labels
        • name
      • Network configuration reference
        • name
        • external
        • labels
        • internal
        • ipam
        • enable_ipv6
        • driver
          • overlay
          • bridge
          • host或者none
        • driver_opts
        • attachable
      • configs configuration reference
      • secrets configuration reference
      • Variable substitution
      • Extension fields
  • docker-compose示例
    • WordPress
    • PostgreSQL
    • Django和PostgreSQL
    • Rails和PostgreSQL
  • Dockerfile最佳实践
Powered by GitBook
On this page

Was this helpful?

  1. Dockerfile指令
  2. ENTRYPOINT

Shell形式的ENTRYPOINT示例

可以使用ENTRYPOINT指定一个纯字符串形式的命令,它会在/bin/sh -c中执行。这样形式将使用shell处理来进行环境变量的替换,并且它会忽视任何在CMD或docker run命令行中指定的参数。为了确保docker stop可以给长期运行的ENTRYPOINT可执行程序正确发送信号,我们需要记住通过exec来执行程序:

FROM ubuntu
ENTRYPOINT exec top -b

当运行这个镜像时,我们将看到只存一个PID 1进程。

$ docker run -it --rm --name test top

Mem: 1704520K used, 352148K free, 0K shrd, 0K buff, 140368121167873K cached
CPU:   5% usr   0% sys   0% nic  94% idle   0% io   0% irq   0% sirq
Load average: 0.08 0.03 0.05 2/98 6
  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
    1     0 root     R     3164   0%   0% top -b

通过docker stop来退出运行的进程:

$ /usr/bin/time docker stop test

test
real    0m 0.20s
user    0m 0.02s
sys    0m 0.04s

如果我们忘记在shell形式的ENTRYPOINT中添加exec来执行程序:

FROM ubuntu
ENTRYPOINT top -b
CMD --ignored-param1

我们提供一个参数来运行这个镜像:

$ docker run -it --name test top --ignored-param2

Mem: 1704184K used, 352484K free, 0K shrd, 0K buff, 140621524238337K cached
CPU:   9% usr   2% sys   0% nic  88% idle   0% io   0% irq   0% sirq
Load average: 0.01 0.02 0.05 2/101 7
  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
    1     0 root     S     3168   0%   0% /bin/sh -c top -b cmd cmd2
    7     1 root     R     3164   0%   0% top -b

我们从输出可以看到top进程并不是PID 1。

如果我们运行docker stop test,容器并不会干净地退出。当退出超时以后,stop命令将强制发送一个SIGKILL信号来终止进程:

docker exec -it test ps aux

PID   USER     COMMAND
    1 root     /bin/sh -c top -b cmd cmd2
    7 root     top -b
    8 root     ps aux

$ /usr/bin/time docker stop test

test
real    0m 10.19s
user    0m 0.04s
sys    0m 0.03s
PreviousENTRYPOINTNext理解CMD和ENTRYPOINT是如何交互的

Last updated 4 years ago

Was this helpful?