Skip to content

Instantly share code, notes, and snippets.

@chroming
Created March 31, 2021 04:02
Show Gist options
  • Save chroming/b84a764184990dd0239c66eadeb54d2b to your computer and use it in GitHub Desktop.
Save chroming/b84a764184990dd0239c66eadeb54d2b to your computer and use it in GitHub Desktop.
docker-compose启动后执行命令

如果想在已有的image运行container后执行命令,又不想自己写一个dockerfile重新打包镜像,可以用healthcheck实现。

比如基于官方salt-master镜像,在启动后自动sync_all:

    healthcheck:
      # Auto sync_all every 10s
      test: salt "*" saltutil.sync_all
      interval: 10s
      start_period: 10s

存在的问题是healthcheck会定时执行,如果只想执行一次可以参考 https://stackoverflow.com/questions/63112803/how-to-execute-docker-health-check-only-one-time

参考资料:

https://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck docker/compose#1809

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment