Skip to content

[Bug]: docker_container.running does not re-create a continer when watched file changes #68958

Closed

Description

@mitar

What happened?

I have state like:

/srv/storage/example/config/auth:
  file.managed:
    - contents: |
        some config
    - user: 1000
    - group: root
    - mode: 600
    - makedirs: true
    - dir_mode: 755

example:
  docker_image.present:
    - name: myexampleimage
    - tag: latest
    - force: true
  docker_container.running:
    - hostname: example
    - image: myexampleimage:latest
    - restart_policy: unless-stopped
    - command:
      - --password=/config/auth
    - binds:
      - /srv/storage/config/config:/config
    - skip_translate: port_bindings
    - networks:
      - ipv4
    - require:
      - docker_image: config
      - docker_network: ipv4
    - watch:
      - file: /srv/storage/example/config/auth

The issue is that when I change auth contents this is correctly detected as a change:

          ID: /srv/storage/example/config/auth
    Function: file.managed
      Result: True
     Comment: File /srv/storage/example/config/auth updated
     Started: 08:34:42.215555
    Duration: 2.488 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1 +1 @@
                  -old config
                  +some config

But docker container is not recreated. There is unrelated change though, but there is no recreate:

          ID: example
    Function: docker_container.running
      Result: True
     Comment: Container 'example' is already configured as specified. Reconnected to network 'ipv4' with updated configuration.
     Started: 08:34:44.375705
    Duration: 549.806 ms
     Changes:   
              ----------
              container:
                  ----------
                  Networks:
                      ----------
                      ipv4:
                          ----------
                          Gateway:
                              ----------
                              new:
                                  172.18.0.1
                              old:
                                  None
                          IPAMConfig:
                              ----------
                              new:
                                  None
                              old:
                                  ----------
                                  IPv4Address:
                                      172.18.0.5
                                  IPv6Address:
                          IPAddress:
                              ----------
                              new:
                                  172.18.0.5
                              old:
                                  None

(It is a side question why it is detecting this networking change - it should not - but this issue is not about that. I think it is connected with #66408.)

Type of salt install

pip (pypi)

Major version

3007.x

What supported OS are you seeing the problem on? Can select multiple. (If bug appears on an unsupported OS, please open a GitHub Discussion instead)

ubuntu-24.04

salt --versions-report output

salt3new) mitar@gagea:~/Documents/Projects/plast8-servers$ salt --versions-report
Salt Version:
          Salt: 3007.9
 
Python Version:
        Python: 3.12.3 (main, Mar  3 2026, 12:15:18) [GCC 13.3.0]
 
Dependency Versions:
          cffi: 2.0.0
      cherrypy: 18.10.0
  cryptography: 46.0.3
      dateutil: 2.9.0.post0
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 3.1.6
       libgit2: Not Installed
  looseversion: 1.3.0
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.1.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 25.0
     pycparser: 2.23
      pycrypto: Not Installed
  pycryptodome: 3.23.0
        pygit2: Not Installed
  python-gnupg: 0.5.5
        PyYAML: 6.0.3
         PyZMQ: 27.1.0
        relenv: Not Installed
         smmap: Not Installed
       timelib: 0.3.0
       Tornado: 6.5.2
           ZMQ: 4.3.5
 
Salt Package Information:
  Package Type: Not Installed
 
System Versions:
          dist: ubuntu 24.04.4 noble
        locale: utf-8
       machine: x86_64
       release: 6.8.0-110-generic
        system: Linux
       version: Ubuntu 24.04.4 noble

Activity

  1. added
    bug
    on Apr 17, 2026
  2. mitar commented on Apr 17, 2026

    @mitar
    ContributorAuthor

    Claude says the issue is in:

    It's in salt/state.py, not in docker_container.py — it's a watch-mechanism design flaw that got hit because your run had unrelated
    changes on the same state.

    salt/state.py:3386-3394 (the core):

      elif status == "change" and not low.get("__prereq__"):
          ret = self.call(low, chunks, running)          # ← normal run first
          if not ret["changes"] and not ret.get("skip_watch", False):
              low = low.copy()
              low["sfun"] = low["fun"]
              low["fun"] = "mod_watch"
              low["__reqs__"] = reqs
              ret = self.call(low, chunks, running)      # ← mod_watch only if no changes above
          running[tag] = ret
    

    Read that carefully: mod_watch is called only if the normal run returned no changes. If the normal run already produced any changes, mod_watch is silently skipped — the watched state's restart trigger is thrown away.

    Now match that against your run:

    1. file: /srv/storage/example/config/auth reported a real diff — this is what flipped the example chunk's status to "change".
    2. Salt calls docker_container.running normally (force=False), per line 3387.
    3. Inside running() (docker_container.py), because the ipv4 network was recreated earlier in the same highstate, the existing container's network attachment disagrees with the temp container, and the block at docker_container.py:1935-1999 performs a disconnect/reconnect and fills ret["changes"]["container"]["Networks"] — that's the "Reconnected to network 'ipv4' with updated configuration" you saw. The comment "Container 'example' is already configured as specified" comes from line 1932, which only runs inside the not skip_comparison branch, confirming force was False on this call.
    4. Back in state.py:3388, ret["changes"] is now non-empty (the network reconnect). The guard if not ret["changes"]: is false. mod_watch is never called.
    5. The state is recorded with result=True and non-empty changes — Salt prints "Changed" in the summary, giving the false impression that the watch fired.

    If mod_watch had been called, it would have gone to docker_container.py:2517-2524, which sets watch_kwargs["force"] = True and re-invokes running(). With force=True, line 1724 sets skip_comparison=True, bypassing the comparison branch, and lines 2004-2011 call _replace(name, temp_container), which stops/removes the old container and renames the temp container in. That's the actual restart — and it never ran.

    So the headline:

    watch on docker_container.running silently becomes a no-op whenever the normal run of the same state produces any changes of its own (network reconnects being the most common case). The file change is the trigger, but the reconnect swallows the restart.

  3. mitar commented on Apr 17, 2026

    @mitar
    ContributorAuthor

    I made PR to fix this: #68960

  4. whytewolf commented on Apr 20, 2026

    @whytewolf
    Collaborator

    and this is what we get when we believe AI. a stupid answer.

    The issue is the logic getting to this point. not the BS claude fed you. it hallucinated and you fell for it. and decided to wag the dog based on its BS.

    the actual answer is that you should have used prereq to call docker_container.absent https://docs.saltproject.io/en/3006/ref/states/requisites.html#prereq if the file changed. since your config is unstable and will have changes that do not reflect the environment.

    you don't need to wag the dog.

  5. mitar commented on Apr 20, 2026

    @mitar
    ContributorAuthor

    Thanks for this interesting info. I did investigate the output from Claude and also the PR I made which changes this behavior works great for me: I tested it against our Docker container which requires restart/recreation after file change and it does exactly what expected. I looked now at prereq but it looks to me a very heavy hammer for this problem. To me it is pretty clear that there is something wrong with how "file.watch" interacts with "docker_container.running". My problem is not that container should not be running with there is a new config file. But that Docker is not restarted when a new config file is deployed IF at the same time some other container's config changes. If nothing else changes, then Docker container is restarted on new config file. But for example, if Salt recreates the network (#66408) and then reconnects the container (which Salt can do without restarting) then new config file is not picked up. This behavior is unexpected and I think wrong. Now, if Claude found the right source of this problem or not, I do not know. But it does not look to me like the state configuration I am describing with "file.watch" + "docker_container.running" is ill defined, it is pretty clear that container should be restarted if file changes.

  6. mitar commented on Apr 23, 2026

    @mitar
    ContributorAuthor

    #68960 contains now an updated approach which I think addresses issues raised above.

  7. added this to the Potassium v3009.0 milestone on Jun 26, 2026
  8. added
    bug
    and removed
    bug
    on Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions