Tree
Tree

Reputation: 31341

docker-compose and using local image with mounted volume

I have an image I create with Dockerfile

FROM mhart/alpine-node:latest

WORKDIR /app
COPY package*.json ./
RUN npm install
COPY src /app

Now in docker-compose.yml I build this image

version: '3.7'
services:
  enginetonic:
    build:
      context: .
    image: enginetonic:compose

  mongodb:
    image: mongo:latest
    container_name: 'mongodb'
    ports:
      - 27017:27017
    restart: always


  monitor-service:
    image: enginetonic:compose
    container_name: monitorService
    command: nodemon monitor/monitor.js
    restart: on-failure

  #common services
  access-token-service:
    image: enginetonic:compose
    container_name: accessTokenService
    command: nodemon service/access-token-service/access-token-service.js
    restart: on-failure
    depends_on:
      - mongodb

In all documentation to bind:mount or use volumes I found, it is used with other docker commands

example

$ docker service create \
     --mount 'type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH>,volume-driver=local,volume-opt=type=nfs,volume-opt=device=<nfs-server>:<nfs-path>,"volume-opt=o=addr=<nfs-address>,vers=4,soft,timeo=180,bg,tcp,rw"'
    --name myservice \
    <IMAGE>

How to use volumes, so that every service that covers the whole /src/ directory, so that every service I start with nodemon reflects the files changed in the whole source code?

Upvotes: 3

Views: 2888

Answers (1)

mixth
mixth

Reputation: 636

I would do a volume map in docker-compose.yml like this:

volumes:
  - ./app/monitor:/path/to/your/workdir/monitor

And adjust the command to use file monitor, like nodemon, to restart service when there is any file changes:

command: ["nodemon", "/path/to/your/workdir/monitor/monitor.js"]

You may need to adjust the nodemon arguments or configs based on what you need.

PS. you do not need to tag/push your image. Simply build it directly in docker-compose#build

Upvotes: 1

Related Questions