Hendrik Jan
Hendrik Jan

Reputation: 4908

Files inside Docker container not updating when I edit in host

I am using Docker which is running fine.
I can start a Docker image using docker-compose.

docker-compose rm nodejs; docker-compose rm db; docker-compose up --build

I attached a shell to the Docker container using

docker exec -it nodejs_nodejs_1 bash

I can view files inside the container

(inside container)
cat server.js

Now when I edit the server.js file inside the host, I would like the file inside the container to change without having to restart Docker.
I have tried to add volumes to the docker-compose.yml file or to the Dockerfile, but somehow I cannot get it to work.

(Dockerfile, not working)
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
VOLUMES ["/usr/src/app"]

EXPOSE 8080
CMD [ "npm", "run", "watch" ]

or

(docker-compose.yml, not working)
version: "3.3"
services:
  nodejs:
    build: ./nodejs-server
    ports:
      - "8001:8080"
    links:
      - db:db
    env_file:
      - ./.env-example
    volumes:
      - src: /usr/src/app
  db:
    build: ./mysql-server
    volumes:
      - ./mysql-server/data:/docker-entrypoint-initdb.d #A folder /mysql-server/data with a .sql file needs to exist
    env_file:
      - ./.env-example
volumes:
  src:

There is probably a simple guide somewhere, but I havn't found it yet.

Upvotes: 3

Views: 4876

Answers (1)

programmerq
programmerq

Reputation: 6534

If you want a copy of the files to be visible in the container, use a bind mount volume (aka host volume) instead of a named volume.

Assuming your docker-compose.yml file is in the root directory of the location that you want in /usr/src/app, then you can change your docker-compose.yml as follows:

version: "3.3"
services:
  nodejs:
    build: ./nodejs-server
    ports:
      - "8001:8080"
    links:
      - db:db
    env_file:
      - ./.env-example
    volumes:
      - .:/usr/src/app
  db:
    build: ./mysql-server
    volumes:
      - ./mysql-server/data:/docker-entrypoint-initdb.d #A folder /mysql-server/data with a .sql file needs to exist
    env_file:
      - ./.env-example

Upvotes: 1

Related Questions