How to run multiple applications with single mongodb using docker

与世无争的帅哥 提交于 2019-12-11 12:45:51

问题


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:


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



来源:https://stackoverflow.com/questions/48841261/how-to-run-multiple-applications-with-single-mongodb-using-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!