You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Watch code and automatically rebuild when something changes #184
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).
Reacted by Ben John, Leo Correa, Giovanni Bassi, Dmitry Anoshin, Michał Frąckiewicz, Mike Cutalo, Pavel Kornev, laggingreflex, Sumit Chachra, Sung and 232 more
Reacted by Eddie Herbert
Reacted by Zoltán Szőgyényi, sbudaraj, Sk Imtiaz Ahmed, Mohamed Moursi, Sławomir Kołodziej, Josh Dovishaw, Tom Clark, Guillaume Chervet, Abouzar Kamaee, Paolo Felice Galizzi and 4 more
Reacted by Jason Haas, Chase Pierce, tcme, Wes McNamee, Maxima, Mateusz, Ben Reed, Serhii Holinei, Brooke Holmes, Brady Holt and 28 more
+1. If this is to be part of fig up, how about an opt in flag?
web:
watch:
- *.py- *.css- *.coffee
Reacted by David, Joe Esposito, rekodr, Zoltán Szőgyényi, luo jiyin, Markus Ritschel, Nripendra Nath Newa, Raju Rimal, Josh Clowater, Uzziah Eyee and 9 more
Reacted by Mohi Hassani, Nico Swiatecki and Luis E. Banegas Saybe
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
Reacted by Caleb Meyer, Dara Dermody, carl-fredrik grimberg, digbijay1, Uzziah Eyee, davoc and Amrit
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.
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.
6 years into the request and I still cannot easily rebuild my docker without creating freaking volumes.
What is this.
Reacted by mustafaelagamey, Konstantin Le, Ismael Bokhari, thomaskr, Dawid, Elad Avron, Marcus Ilgner, gustavohornig, Ati Ok, forrestlunasec and 8 more
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-
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.
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.
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 -
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?
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.
Activity
samalba commentedon Apr 6, 2014
👍
marksteve commentedon Apr 22, 2014
👍
sherzberg commentedon Apr 22, 2014
+1. If this is to be part of fig up, how about an opt in flag?
cameron commentedon May 2, 2014
👍
Where this gets documented, it's probably worth noting the following pattern that helps avoiding unnecessary package manager (pip/npm/bower/et al) action:
KyleAMathews commentedon Sep 6, 2014
💯
luebken commentedon Oct 7, 2014
+1
mdaffin commentedon Oct 28, 2014
👍
tptee commentedon Nov 17, 2014
👍
davidpaulhunt commentedon Jan 13, 2015
It's been a few months, and I couldn't tell from the merge referenced above, was a "watch" ability integrated?
aanand commentedon Jan 13, 2015
No,
fig watchhas not been implemented.aanand commentedon Jan 13, 2015
It's still on the table though - see ROADMAP.md
bfirsh commentedon Jan 26, 2015
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.
aanand commentedon Jan 27, 2015
Now that we have a
--no-buildflag, perhaps the logic should be:That said, if we go down the #693 path, we'll have to revisit this.
amjibaly commentedon Jan 28, 2015
👍
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 --watchand the first timenpm installruns because thepackage.jsonhasn'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 hasnode_modulesin it so those are never included in the build context.Now, I still want to do things like
npm linkwhere I can change a common module andfig up --watchknows to reload. I currently do this by adding a service to myfig.ymland mounting it to my common "code" volume. I can't think of a better way to do this at the moment.docteurklein commentedon Jan 28, 2015
what about the right tool for the right job ?
wouldn't
guardfit perfectly here ?Anyway, it would be a nice addition.
53 remaining items
Lukortech commentedon Oct 13, 2020
6 years into the request and I still cannot easily rebuild my docker without creating freaking volumes.
What is this.
facundopadilla commentedon Jan 31, 2021
2021 lol
DarthGoon commentedon Mar 26, 2021
I found a way to do this, that I like.
I'm using
inotifywaitto watch the file system, andnohupto start the processes; In a dev tooling "layer" in my linux docker container.I put this in my Dockerfile-
This is
code_reloader.sh-The
nohupcommand inreload()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
nohupoutput file if it's in the same directory as the one you're monitoring; ornohupwriting 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
/optin thedocker-compose.yml; rundocker-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-
fiq commentedon Sep 17, 2021
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.
Mahoney commentedon Nov 30, 2021
Started playing with a wrapper script to add
--watchhere:https://github.com/Mahoney/docker-compose-watch/blob/main/docker-compose
Immediate weaknesses:
patientplatypus6 commentedon Mar 11, 2022
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 upand<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 exampleThis is the entrypoint.sh file for a backend Django/Python application -
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?
ndeloof commentedon Mar 11, 2022
I expected inotify was working as expected under Docker Desktop, will need to change. A potential implementation of
watchcommand for application stacks which support reloading could be fordocker compose watchcommand to send aSIGHUPsignal.aradalvand commentedon Apr 11, 2022
No updates on this after 8 years?!
Lukortech commentedon Apr 12, 2022
Sadly, no.
I've given up on docker desktop for now.
ydennisy commentedon Jul 30, 2022
@ndeloof any news on your PR?
nbx0 commentedon Mar 4, 2023
+1!
ndeloof commentedon Mar 6, 2023
The experimental
watchcommand has been released, closing this issue nowramnes commentedon Mar 6, 2023
Here is more context for those wondering:
TL;DR:
docker compose alpha watchon HEAD