1

I want to be able to run two separate apps in two containers - one for each independent app. Then both apps should be able to communicate with the 3rd container which will hold MongoDB.

  1. Folder structure on my local machine

    /Users/me/Dev: Dockerfile, docker-compose.yml 
    /Users/me/Dev/App1: with source code and package.json for app1
    /Users/me/Dev/App2: with source code and package.json for app2
    /Users/me/Dev/data/db: I wanna keep datafile for Mongo
    
  2. dockerfile - but honestly it does not look right too me to repeat twice for both apps. (Its a first time I am using docker so am not yet familiar with how to best prepare the Dockerfile for my scenario)

    FROM node:8.9.1
    WORKDIR ./app1/
    COPY ./package.json ./app1/
    RUN npm install
    CMD npm start
    COPY ./app1/ ./app1/
    EXPOSE 8081
    #
    FROM node:8.9.1
    WORKDIR ./app2/
    COPY ./package.json ./app2/
    RUN npm install
    CMD npm start
    COPY ./app2/ ./app2/
    EXPOSE 8082
    
  3. docker-compose.yml

    version: '2'
    
    services:
    app1:
    build: .
    volumes:
      - .:/app1
    ports:
      - "8082:8082"
    links:
      - mongo
    depends_on:
      - mongo
    
    # app2:
    #   build: .
    #   volumes:
    #     - .:./app2
    #   ports:
    #     - "8081:8081"
    #   links:
    #     - mongo
    #   depends_on:
    #     - mongo
    
    mongo:
    image: mongo:3.4.10
    volumes:
        - './dev/data/db:/data/db'
    ports:
        - "27017:27017"
    

Few issues: On docker-compose build for example I got - ERROR: Service 'app1' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder287108072/package.json: no such file or directory

Even if I tried multiple ways of providing the path in Dockerfile and d-compose I still am not convinced this is gonna work.

So I need some guidance on how to prepare Dockerfile and docker compose in my main DEV folder (as described above) so that docker will start up 3 containers (app1, app2, mongo) all on the same docker network to communicate with each other.

1
  • create the docker images first. Then write the docker-compose.yml file for running the app. Instead of buiding. Its good practice. Commented Feb 18, 2018 at 20:21

1 Answer 1

2

Approach#1 Just correct your Dockerfile

Path need to be replaced with the folder path first

correct it with

COPY ./App1/package.json ./app1/

COPY ./App2/package.json ./app2/

But this look wrong practice. Folow bellow steps to create the docker images and deploy them with the docker-compose.yml just like mongo image you have to create your own image and run them with compose.

Approach#2 Good practice to deploy apps

App1 Docker Image

  1. Goto your App1 folder and create a Dockerfile

    Dockerfile

    FROM node:8.9.1
    WORKDIR ./app1/
    COPY ./package.json ./app1/
    RUN npm install
    CMD npm start
    COPY ./app1/ ./app1/
    EXPOSE 8081
    
  2. Create docker images with

    docker build -t app1Image .

App2 Docker Image

  1. Goto your App2 folder and create a Dockerfile

    Dockerfile

    FROM node:8.9.1
    WORKDIR ./app2/
    COPY ./package.json ./app2/
    RUN npm install
    CMD npm start
    COPY ./app2/ ./app2/
    EXPOSE 8081
    
  2. Create docker images with

    docker build -t app1Image .

Deploy Image with compose

  1. Create docker-compose.yml in Dev folder

    docker-compose.yml

    version: '2'
    services:
       app1:
         image: app1Image
         ports:
           - 8082:8082
         links:
           - mongo
         depends_on:
           - mongo
       app2:
         image: app2Image
         ports:
           - 8081:8081
         links:
           - mongo
         depends_on:
           - mongo
       mongo:
         image: mongo:3.4.10
         ports:
           - 27017:27017
         volumes:
           - './dev/data/db:/data/db'
         links:
           - mongo
         depends_on:
           - mongo
    
  2. RUN App with docker-compose

    docker-compose up -d

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much. It worked with images. I will keep the build version, slightly adjusted after your answer, for my day to day development when I want change reflected live when I make edits to js files. Again thanks so much for the answer.
You don't need to change the build versio. use latest always. Kepp backup of each image by tagging. mark it as answer if it solves your purpose

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.