Search code examples
node.jsdockerworkflow

How should I Accomplish a Better Docker Workflow?


Everytime I change a file in the nodejs app I have to rebuild the docker image.
This feels redundant and slows my workflow. Is there a proper way to sync the nodejs app files without rebuilding the whole image again, or is this a normal usage?


Solution

  • What I ended up doing was:

    1) Using volumes with the docker run command - so I could change the code without rebuilding the docker image every time.

    2) I had an issue with node_modules being overwritten because a volume acts like a mount - fixed it with node's PATH traversal.

    Dockerfile:

    FROM node:5.2
    
    # Create our app directories
    RUN mkdir -p /usr/src/app
    WORKDIR /usr/src/app
    
    RUN npm install -g nodemon
    
    # This will cache npm install
    # And presist the node_modules
    # Even after we are using the volume (overwrites)
    COPY package.json /usr/src/
    RUN cd /usr/src && npm install 
    
    #Expose node's port
    EXPOSE 3000
    
    # Run the app
    CMD nodemon server.js
    

    Command-line:

    to build:

    docker build -t web-image
    

    to run:

    docker run --rm -v $(pwd):/usr/src/app -p 3000:3000 --name web web-image