Watch code and automatically rebuild when something changes
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!
:+1:
:+1:
+1. If this is to be part of fig up, how about an opt in flag?
web:
watch:
- *.py
- *.css
- *.coffee
: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
:100:
+1
:+1:
:+1:
It's been a few months, and I couldn't tell from the merge referenced above, was a "watch" ability integrated?
No, fig watch has not been implemented.
It's still on the table though - see ROADMAP.md
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.
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.
:+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.
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.
@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 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 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 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 you're totally off-topic, no?