compose icon indicating copy to clipboard operation
compose copied to clipboard

Watch code and automatically rebuild when something changes

Open bfirsh opened this issue 12 years ago • 69 comments

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!

bfirsh avatar Apr 06 '14 15:04 bfirsh

:+1:

samalba avatar Apr 06 '14 20:04 samalba

:+1:

marksteve avatar Apr 22 '14 00:04 marksteve

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

web:
  watch: 
   - *.py
   - *.css
   - *.coffee

sherzberg avatar Apr 22 '14 16:04 sherzberg

:thumbsup:

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

cameron avatar May 02 '14 03:05 cameron

:100:

KyleAMathews avatar Sep 06 '14 23:09 KyleAMathews

+1

luebken avatar Oct 07 '14 14:10 luebken

:+1:

mdaffin avatar Oct 28 '14 14:10 mdaffin

:+1:

tptee avatar Nov 17 '14 19:11 tptee

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

davidpaulhunt avatar Jan 13 '15 16:01 davidpaulhunt

No, fig watch has not been implemented.

aanand avatar Jan 13 '15 17:01 aanand

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

aanand avatar Jan 13 '15 17:01 aanand

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.

bfirsh avatar Jan 26 '15 23:01 bfirsh

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.

aanand avatar Jan 27 '15 16:01 aanand

:+1:

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.

amjibaly avatar Jan 28 '15 06:01 amjibaly

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.

docteurklein avatar Jan 28 '15 08:01 docteurklein

@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. ... 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?)

This sounds out of scope for fig or docker build to me. Like in this example, if you're referencing private Git repos as NPM dependencies, you could use the [email protected]:me/myrepo.git style of remote and then Git would just defer to SSH for key authorization without NPM, docker build, or fig needing to know anything about it. Most SCMs work similarly with SSH, or at worst other underlying tools should likely check an environment variable or config file of their own. But maybe there's something else you had in mind that I'm not considering?

ches avatar Jan 28 '15 09:01 ches

@ches I didn't realize docker build already supports this. I just gave it a shot and it doesn't seem to work, can you point me to the documentation? Here's the error I see:

npm ERR! git clone [email protected]:myorg/myproj.git Cloning into bare repository '/root/.npm/_git-remotes/git-github-com-myorg-myproj-git-9e8969c5'...
npm ERR! git clone [email protected]:myorg/myproj.git error: cannot run ssh: No such file or directory
npm ERR! git clone [email protected]:myorg/myproj.git fatal: unable to fork

In package.json I have:

"dependencies": { "myproj": "git+ssh://[email protected]:myorg/myproj.git#master" }

amjibaly avatar Jan 28 '15 15:01 amjibaly

@amjibaly looks like you need to install ssh in your container

@docteurklein I tend to agree, there are existing tools which would handle this pretty easily (the python watchdog package includes a cli called watchmedo which would also work).

dnephin avatar Jan 28 '15 15:01 dnephin

@dnephin I installed ssh, now I'm getting this error:

Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

Running npm install locally works of course.

amjibaly avatar Jan 28 '15 16:01 amjibaly

@amjibaly you're totally off-topic, no?

docteurklein avatar Jan 28 '15 17:01 docteurklein