3

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.

1
  • 1
    use volumes in compose file like this: ./path/to/application:/usr/src/app Commented Mar 11, 2019 at 14:14

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

In my case the location that I want in /usr/src/app is in the sub-folder nodejs-server. I changed your line - .:/usr/src/app to - ./nodejs-server:/usr/src/app and it works :-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.