0

I have a MEAN stack application running on three Docker containers (MongoDB, Express, Angular). I would like to see immediate changes made to the angular app. I tried to follow this thread but got the following error with the Angular container:

no such file or directory, open '/usr/src/app/package.json'

Here is my docker-compose file:

version: '3' # specify docker-compose version

# Define the services/containers to be run
services:
  angular: # name of the first service
    build: angular-src # specify the directory of the Dockerfile
    command: npm start
    ports:
      - "4200:4200" # specify port forewarding
      - "49153:49153"
    volumes:
      - ./angular-src:/usr/src/app

  express: #name of the second service
    build: . # specify the directory of the Dockerfile
    ports:
      - "3000:3000" #specify ports forewarding
    links:
      - mongodb # link this service to the database service

  mongodb: # name of the third service
    image: mongo
    command: mongod --smallfiles
    ports:
      - 27017 # specify port forewarding

  mongo_seed:
    build: ./mongo_seed
    links:
      - mongodb

And my docker file for the Angular app:

# Create image based on the official Node 6 image from dockerhub
FROM node:8.9.1

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package.json /usr/src/app

# Install dependecies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app

# Expose the port the app runs in
EXPOSE 4200 49153

# Serve the app
CMD ["npm", "start"]
Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
Batmax
  • 253
  • 8
  • 17

1 Answers1

0

You're overwriting the result of the COPY commands in your Dockerfile with the volumes element in your angular service in the docker-compose.yml.

COPY package.json /usr/src/app

Contents of /usr/src/app: package.json

COPY . /usr/src/app

Contents of /usr/src/app: (whatever is included in build context at .), packages.json

When you docker-compose up and the angular service is ran, a volume is mounted:

volumes:
     - ./angular-src:/usr/src/app

Contents of /usr/src/app: (everything in angular-src)

When you mount a volume, you aren't adding the files from the volume to the files in the container, you're replacing the directory wholesale.

bluescores
  • 4,437
  • 1
  • 20
  • 34
  • Thank for the reply. After the volume is mounted you said the content is 'everything in angular-src', this the package.json should also be the right? Not sure I completely understood... – Batmax Nov 22 '17 at 14:58
  • if ./angular-src/package.json exists on your host, yes. If not, no. Take a peek in the container: `docker run -it /bin/bash`, and check the contents of /usr/src/app. If it looks ok, mimic the volume mount from your compose: `docker run -it -v $(pwd)/angular-src:/usr/src/app /bin/bash`, and check again to make sure things are as you expect them to be. – bluescores Nov 22 '17 at 15:18