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?
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
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, 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.
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