<p>An approach I use in both prod and dev is to run a webhook server ( this one is excellent <a href="https://github.com/adnanh/webhook" rel="nofollow noreferrer">https://github.com/adnanh/webhook</a> ) which is subscribed to listen to the github.com  <code>git push</code> events which are published by github </p>

<p>... then when someone does a push to github.com the webhook launches a rebuild script to issue <code>git pull</code> then recomple the code and issue <code>docker build xxx</code> and <code>docker push yyy</code>  and <code>docker-compose -f my-file-docker-compose.yaml up</code>   ...  this all happens on auto pilot ...  where I have a running webhook server on a vps box for a given git branch to rebuild its toy domain</p>

<p>this took time to craft however its a life saver</p>

CC BY-SA 4.0
2019-08-07T00:54:46.393Z
2019-08-07T00:54:46.393Z
Scott Stensland
<p>For a local development using docker-compose it depends on the language you use to code. But almost all languages have some kind of watch mode to take care when the code changes and reload your service or application.</p>

<p>No matter what language you use for coding if you want to have an auto-refresh feature you need to use volumes on your docker-compose.yaml file to mount your local folder as a volume inside the container.</p>

<p>With that said one example of that would be using <a href="https://nodemon.io/" rel="nofollow noreferrer">nodemon</a> on a <a href="https://nodejs.org/" rel="nofollow noreferrer">nodeJS</a> project, but you have o take care not to use nodemon on production by using the <code>CMD</code> on the Dockerfile and docker-compose.yaml properly.</p>

<p>An example of Dockerfile:</p>

<pre><code>FROM node:12

WORKDIR /app

RUN npm i -g nodemon

COPY . /app

CMD node app.js
</code></pre>

<p>And on the docker-compose.yaml:</p>

<pre><code>version: 3
services:
  backend:
    build: .
    image: local-development
    volumes:
      - .:/app
      - /app/node_modules
    command: ['nodemon', 'app.js']
</code></pre>

<p>Look at the two volumes, the first one is for mounting your local directory on <code>/app</code>, but you need the second one to avoid mounting <code>/app/node_modules</code> because you can have problems with modules you have on local that are not the right architecture inside the container, for example, a MacOS compiled module inside a Linux container.</p>

<p>When you use the image on a production environment it will use <code>node app.js</code> as a command but on the <code>docker-compose.yaml</code> file is overridden by <code>nodemon app.js</code>.</p>

CC BY-SA 4.0
2019-08-06T00:55:09.813Z
2019-08-06T00:55:09.813Z
wolmi
<p>What kind of technology do you use? Are you speaking about local deployment, during development phase? If you have some kind of "ng serve" feature like in Angular you can probably use volumes (<a href="https://docs.docker.com/storage/bind-mounts/" rel="nofollow noreferrer">bind mounts</a>, to be specific) to bind your code on your host to the code inside your container. Then, every time you do a modification on your code, it will be updated automatically. </p>

CC BY-SA 4.0
2019-08-05T18:39:51.027Z
2019-08-05T18:39:51.027Z
Schisme
<p>What is the best way to use Docker in development so that a container is automatically updated each time you make a change to the codebase?</p>

CC BY-SA 4.0
2019-08-05T10:46:51.973Z
Hi Leander! It would help a bit if you could provide some context to the question. what kind of application are you talking about here? What does the pipeline look like? thanks!
Bruce Becker
CC BY-SA 4.0
2019-08-05T15:22:09.483Z
Hi Bruce! I'm only learning Docker - something really simple like a docker-compose with say three services: a db image, a server container and a client container. What I'm trying to do is update the containers automatically as I work on the code in the server or the client. Thank you :)
leandre
CC BY-SA 4.0
2019-08-05T15:37:13.733Z
Thanks for the clarification :) This seems like a question that can have a very long answer - it's at the heart of continuous integration. Give us some time to think over what would be the best way to answer.
Bruce Becker
<docker><docker-compose><dockerfile>
How to automatically update Docker containers in development?
leandre
# How to automatically update Docker containers in development?

(Asked by: username_0 on 2019-08-05)

What is the best way to use Docker in development so that a container is automatically updated each time you make a change to the codebase?

**Tags:** docker, docker-compose, dockerfile

> **Comments:**
> **username_1**: Hi Leander! It would help a bit if you could provide some context to the question. what kind of application are you talking about here? What does the pipeline look like? thanks!  
> **username_0**: Hi Bruce! I'm only learning Docker - something really simple like a docker-compose with say three services: a db image, a server container and a client container. What I'm trying to do is update the containers automatically as I work on the code in the server or the client. Thank you :)  
> **username_1**: Thanks for the clarification :) This seems like a question that can have a very long answer - it's at the heart of continuous integration. Give us some time to think over what would be the best way to answer.  

---

## 3 answers

### Answer by username_2 on 2019-08-07. Score: 3

An approach I use in both prod and dev is to run a webhook server ( this one is excellent <https://github.com/adnanh/webhook> ) which is subscribed to listen to the github.com `git push` events which are published by github

... then when someone does a push to github.com the webhook launches a rebuild script to issue `git pull` then recomple the code and issue `docker build xxx` and `docker push yyy` and `docker-compose -f my-file-docker-compose.yaml up` ... this all happens on auto pilot ... where I have a running webhook server on a vps box for a given git branch to rebuild its toy domain

this took time to craft however its a life saver


---

### Answer by username_4 on 2019-08-05. Score: 1

What kind of technology do you use? Are you speaking about local deployment, during development phase? If you have some kind of "ng serve" feature like in Angular you can probably use volumes ([bind mounts](https://docs.docker.com/storage/bind-mounts/), to be specific) to bind your code on your host to the code inside your container. Then, every time you do a modification on your code, it will be updated automatically.


---

### Answer by username_3 on 2019-08-06. Score: 0

For a local development using docker-compose it depends on the language you use to code. But almost all languages have some kind of watch mode to take care when the code changes and reload your service or application.
No matter what language you use for coding if you want to have an auto-refresh feature you need to use volumes on your docker-compose.yaml file to mount your local folder as a volume inside the container.
With that said one example of that would be using nodemon on a nodeJS project, but you have o take care not to use nodemon on production by using the CMD on the Dockerfile and docker-compose.yaml properly.
An example of Dockerfile:
FROM node:12

WORKDIR /app

RUN npm i -g nodemon

COPY . /app

CMD node app.js

And on the docker-compose.yaml:
version: 3
services:
  backend:
    build: .
    image: local-development
    volumes:
      - .:/app
      - /app/node_modules
    command: ['nodemon', 'app.js']

Look at the two volumes, the first one is for mounting your local directory on /app, but you need the second one to avoid mounting /app/node_modules because you can have problems with modules you have on local that are not the right architecture inside the container, for example, a MacOS compiled module inside a Linux container.
When you use the image on a production environment it will use node app.js as a command but on the docker-compose.yaml file is overridden by nodemon app.js.


---

