2

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?

3
  • 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! Commented Aug 5, 2019 at 10:46
  • 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
    Commented Aug 5, 2019 at 15:22
  • 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. Commented Aug 5, 2019 at 15:37

3 Answers 3

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

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.