Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container not updating on code change

I have a Dockerfile to build my node container, it looks as follows:

FROM node:12.14.0

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 4500

CMD ["npm", "start"]

based on this docker file, I am using docker compose to run this container and link it to a mongo container such that it refers to mongo-service. The docker-compose.yml looks as follows

version: '3'
services:
    backend:
        container_name: docker-node-mongo-container
        restart: always
        build: .
        ports: 
            - '4700:4500'
        links: 
            - mongo-service

    mongo-service:
        container_name: mongo-container
        image: mongo
        ports: 
            - "27017:27017"

Expected behavior: Everytime I make a new change to the project on my local computer, I want the docker-compose to restart so that the new changes are reflected.

Current behavior: To make the new changed reflect on docker-compose, I have to do docker-compose down and then delete images. I am guessing that it has to rebuild images. How do I make it so that whenever I make change, the dockerfile builds a new image?

I understand that need to use volumes. I am just failing to understand how. Could somebody please help me here? docker

like image 251
Sarthak Batra Avatar asked Jun 04 '20 11:06

Sarthak Batra


2 Answers

When you make a change, you need to run docker-compose up --build. That will rebuild your image and restart containers as needed.

Docker has no facility to detect code changes, and it is not intended as a live-reloading environment. Volumes are not intended to hold code, and there are a couple of problems people run into attempting it (Docker file sync can be slow or inconsistent; putting a node_modules tree into an anonymous volume actively ignores changes to package.json; it ports especially badly to clustered environments like Kubernetes). You can use a host Node pointed at your Docker MongoDB for day-to-day development, and still use this Docker-based setup for deployment.

like image 69
David Maze Avatar answered Sep 27 '22 20:09

David Maze


In order for you to 'restart' your docker application, you need to use docker volumes.

Add into your docker-compose.yml file something like:

version: '3'
services:
    backend:
        container_name: docker-node-mongo-container
        restart: always
        build: .
        ports: 
            - '4700:4500'
        links: 
            - mongo-service
        volumes:
            - .:/usr/src/app


    mongo-service:
        container_name: mongo-container
        image: mongo
        ports: 
            - "27017:27017"

The volumes tag is a simple saying: "Hey, map the current folder outside the container (the dot) to the working directory inside the container".

like image 36
leandrofahur Avatar answered Sep 27 '22 21:09

leandrofahur