Skip to content

Watch code and automatically rebuild when something changes #184

Closed

Description

@bfirsh

Compose could watch your code and automatically kick off builds when something changes. This would mean:

  • Code can be reloaded automatically regardless of language. This would also make code reloading work on compiled languages if that was part of the build process!
  • Volumes are no longer needed in development
  • Production server can be used in development (e.g. gunicorn can be used to serve Django apps in development)

It could either be an option to up (docker-compose up --watch), an option in the config file specifying directories to watch/ignore, or perhaps a separate command (docker-compose watch).

Thanks to @samalba for the suggestion!

Activity

  1. samalba commented on Apr 6, 2014

    @samalba

    👍

  2. marksteve commented on Apr 22, 2014

    @marksteve

    👍

  3. sherzberg commented on Apr 22, 2014

    @sherzberg

    +1. If this is to be part of fig up, how about an opt in flag?

    web:
      watch: 
       - *.py
       - *.css
       - *.coffee
  4. cameron commented on May 2, 2014

    @cameron

    👍

    Where this gets documented, it's probably worth noting the following pattern that helps avoiding unnecessary package manager (pip/npm/bower/et al) action:

    # adding your language deps before adding the rest of your source....
    ADD requirements.txt /requirements.txt
    RUN pip install -r requirements.txt
    
    # ... will prevent changes to source incurring a re-install of language deps
    ADD . /src
    
  5. KyleAMathews commented on Sep 6, 2014

    @KyleAMathews

    💯

  6. luebken commented on Oct 7, 2014

    @luebken

    +1

  7. mdaffin commented on Oct 28, 2014

    @mdaffin

    👍

  8. tptee commented on Nov 17, 2014

    @tptee

    👍

  9. davidpaulhunt commented on Jan 13, 2015

    @davidpaulhunt

    It's been a few months, and I couldn't tell from the merge referenced above, was a "watch" ability integrated?

  10. aanand commented on Jan 13, 2015

    @aanand

    No, fig watch has not been implemented.

  11. aanand commented on Jan 13, 2015

    @aanand

    It's still on the table though - see ROADMAP.md

  12. bfirsh commented on Jan 26, 2015

    @bfirsh
    Author

    An idea from @fxdgear: instead of rebuilding, it could also be useful to just restart the container when the code changes. E.g. when you've mounted your code with a volume.

  13. aanand commented on Jan 27, 2015

    @aanand

    Now that we have a --no-build flag, perhaps the logic should be:

    $ fig up                      # current behaviour
    $ fig up --watch              # watch and rebuild + restart
    $ fig up --watch --no-build   # watch and restart
    

    That said, if we go down the #693 path, we'll have to revisit this.

  14. amjibaly commented on Jan 28, 2015

    @amjibaly

    👍

    For this to be really awesome, there's got to be a way for docker build to accept or pass through some type of credentials without storing it in the image. I'm thinking private github modules but it really applies to any environment.

    The following is a NodeJS-based example using my current version of "fig up --watch" which is just a nodemon-based script:

    I run fig up --watch and the first time npm install runs because the package.json hasn't been cached yet. This needs to fetch private modules from github so it passes those through (maybe this is setup in my ~/.figconfig or something similar?) My .dockerignore has node_modules in it so those are never included in the build context.

    Now, I still want to do things like npm link where I can change a common module and fig up --watch knows to reload. I currently do this by adding a service to my fig.yml and mounting it to my common "code" volume. I can't think of a better way to do this at the moment.

  15. docteurklein commented on Jan 28, 2015

    @docteurklein

    what about the right tool for the right job ?

    wouldn't guard fit perfectly here ?

    guard :shell do
      watch(/Dockerfile$/) { `fig build` }
      watch(/fig.yml$/) { `fig up -d` }
    end

    Anyway, it would be a nice addition.

  16. 53 remaining items

  17. Lukortech commented on Oct 13, 2020

    @Lukortech

    6 years into the request and I still cannot easily rebuild my docker without creating freaking volumes.
    What is this.

  18. facundopadilla commented on Jan 31, 2021

    @facundopadilla

    6 años después de la solicitud y todavía no puedo reconstruir fácilmente mi ventana acoplable sin crear malditos volúmenes.
    Qué es esto.

    2021 lol

  19. DarthGoon commented on Mar 26, 2021

    @DarthGoon

    I found a way to do this, that I like.

    I'm using inotifywait to watch the file system, and nohup to start the processes; In a dev tooling "layer" in my linux docker container.

    I put this in my Dockerfile-

    # DEV TOOLING
    RUN apt-get update && apt-get install -y inotify-tools
    COPY code_reloader.sh /
    RUN chmod +x /code_reloader.sh
    ENTRYPOINT ["/code_reloader.sh"]
    #############
    

    This is code_reloader.sh-

    #!/bin/bash
    set -x
    
    reload() {
      nohup python /opt/manage.py runserver 0.0.0.0:5000 > /nohup.out &
      DEV_SERVER_PID=$!
      PROCESS_RELOADING=0
    }
    
    reload
    
    inotifywait -m -r -e modify /opt | while read CHANGE_EVENT;
    do
      if [ $PROCESS_RELOADING -ne 1 ];
        then
          PROCESS_RELOADING=1
          echo "#### $CHANGE_EVENT DETECTED, Reloading PID:$DEV_SERVER_PID... ####"
          kill $DEV_SERVER_PID
          reload
        fi
    done
    

    The nohup command in reload() is what would be the ENTRYPOINT in the production container's Dockerfile. I happen to be running a python service in this example.
    BE SURE TO redirect the nohup output file if it's in the same directory as the one you're monitoring; or nohup writing to it's out file will trigger a reloading loop. I'm redirecting it to / in this example.

    With this setup-- I mount the code directory into /opt in the docker-compose.yml; run docker-compose up; and just write code. The service reloads with changes.

    I like this approach because I don't have to modify code, or install framework specific packages/CLI tools, to be able to live reload.
    The docker-compose.yml parts-

    version: "3.9"
    services:
      sandbox:
        build: .
        volumes:
          - ./code:/opt
        ports:
          - "5000:5000"
    
  20. fiq commented on Sep 17, 2021

    @fiq

    A watch and rebuild would be a really handy feature. As per the previous comment, a common work around is mounting one's app over the immutable version you have baked into your image. I see this often and it defeats the intent of containerisation + introduces false confidence as the running image is not representative of a particular build.

  21. Mahoney commented on Nov 30, 2021

    @Mahoney

    Started playing with a wrapper script to add --watch here:
    https://github.com/Mahoney/docker-compose-watch/blob/main/docker-compose

    Immediate weaknesses:

    • It just uses pwd rather than parsing the yaml to decide which contexts to use
    • It’s pretty inefficient because it doesn’t know about .dockerignore - so a change to an ignored file results in the entire context being calculated and sent to the docker daemon, before docker then decides there’s nothing to do.
  22. patientplatypus6 commented on Mar 11, 2022

    @patientplatypus6

    I'm surprised this is still not a feature. ndeloof is working on this for Docker v2, but this should be in v3 itself as well.
    Here's the problem that I'm currently facing.
    I'm developing an application using docker-compose v3.9 on mac - which means that I can't use inotif-tools, but have to use fswatch. Which is a bit frustrating, because if I eventually want to port this over to a linux container that means that I'll have to then switch to inotify-tools.
    OK fine, minor issue.
    However
    Watching for file changes means that I spawn a new process - so if I docker-compose up and <watch for file changes> in the same bash file then a new process is spawned, meaning I can't restart the container in a process that doesn't have the container running! So, that leaves me with the option of writing entrypoint scripts for each of the containers - but look at this example

    This is the entrypoint.sh file for a backend Django/Python application -

    #!/bin/bash
    
    echo "inside entrypoint"
    echo "deleting prior static images directory"
    rm -rf ./static_images
    echo "makemigrations"
    python3 manage.py makemigrations
    python3 manage.py makemigrations lightone
    echo "migrate"
    python3 manage.py migrate
    python3 manage.py migrate lightone
    
    echo "manage.py createsuperuser"
    python3 manage.py createsuperuser \
      --noinput \
      --username pixel \
      --email [email protected]
    # --database lightchan
    
    echo "now running the server"
    daphne -b 0.0.0.0 -p 8000 lightchan.asgi:application
    echo "now running the server"
    

    The container itself is alpine linux so I could use a file watch like inotify-tools. The issue now is that inotify-tools is blocking! So how do I simultaneously run Daphne while watching for file changes? Maybe I could run Daphne as a background process using a trailing &, but again, does that mean that Daphne will pick up file changes if inotify-tools kills the container and then the script restarts?

    This is a major major issue. Will somebody fix this?

  23. ndeloof commented on Mar 11, 2022

    @ndeloof
    Contributor

    ndeloof is working on this for Docker v2, but this should be in v3 itself as well.
    There's a (common) confusion here between the file format (which is now obsolete with Compose Specification) and the tool version. "Docker Compose v2" is docker compose command developed on this repository as v2 branch.

    I expected inotify was working as expected under Docker Desktop, will need to change. A potential implementation of watch command for application stacks which support reloading could be for docker compose watch command to send a SIGHUP signal.

  24. aradalvand commented on Apr 11, 2022

    @aradalvand

    No updates on this after 8 years?!

  25. Lukortech commented on Apr 12, 2022

    @Lukortech

    Sadly, no.
    I've given up on docker desktop for now.

  26. ydennisy commented on Jul 30, 2022

    @ydennisy

    @ndeloof any news on your PR?

  27. nbx0 commented on Mar 4, 2023

    @nbx0

    +1!

  28. ndeloof commented on Mar 6, 2023

    @ndeloof
    Contributor

    The experimental watch command has been released, closing this issue now

  29. ramnes commented on Mar 6, 2023

    @ramnes
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions