brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: post_start runs a command in a Compose service right after it starts

Docker
~/tips/tip-docker-compose-post-start-hook

services:

Fix permissions or seed a file each time the container starts, without a custom image.

Plenty of images run as a normal user but expect a data folder they can write to. Compose lifecycle hooks let you run a command right after start, optionally as root, while the app itself keeps running as that normal user.

services:
  app:
    image: example/app:1.0
    user: "1000:1000"
    volumes:
      - data:/data
    post_start:
      - command: chown -R 1000:1000 /data
        user: root
volumes:
  data:

The hook runs after the container has started. The docs list command as required, plus optional user, privileged and environment. There is a matching pre_stop hook for something that should happen before the container stops, like flushing a cache.

Hooks arrived in Compose 2.30, so on older versions the file is rejected. I would check with docker compose config first.

The hook and the app start at about the same time, so do not count on the command finishing before the app touches /data. For setup that must be done first, a one-shot init service with depends_on and service_completed_successfully is the safer pattern. A chown -R on a huge volume on every start can also take a while.

Further reading: Use lifecycle hooks, Compose file reference: post_start

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link