-1

I have a dockerfile for node js code as below

FROM node:10.14-alpine as buildcontainer

RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/main' >> /etc/apk/repositories
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/community' >> /etc/apk/repositories
RUN apk update
RUN apk add mongodb

RUN mongo --version
# RUN apt-get update
# RUN apt-get install -y mongodb
# RUN apt-get install -y npm
# RUN apt-get install -y nodejs

COPY source-code/config /home/app/config
COPY source-code/src /home/app/src
COPY source-code/package.json /home/app/package.json
COPY source-code/tsconfig.json /home/app/tsconfig.json
COPY source-code/tsconfig.build.json /home/app/tsconfig.build.json
WORKDIR /home/app
RUN npm install

FROM buildcontainer as tester
COPY . /home/app
WORKDIR /home/app

FROM tester as dev
WORKDIR /home/app
RUN npm run build
ENTRYPOINT ["/usr/local/bin/npm", "run", "start"]

Docker-compose file is below:-

version: '3.4'
services:
  mongodb-load-service:
    image: mongodb-load-service
    container_name: mongodb-load-service
    build:
      context: .
      target: dev
    depends_on:
      - mongo
    ports:
      - "8080:8080"
    links:
      - mongo
    networks: 
      - backend
    environment:
      - MONGODB_URI=mongodb://mongo/test
      - MONGO_HOST=localhost:27017
  mongo:
    image: mongo:latest
    container_name: mongo
    ports:
      - 27017:27017
    networks: 
      - backend
networks:
  backend:
    driver: "bridge"

Im using mongoimport command in node js code to import csv files in mongodb. The code runs fine when i run on local dev env.(without docker) but not when i run through docker compose file.

Error i get is:-

mongo                   | 2019-11-22T13:27:56.606+0000 I  NETWORK  [conn1] received client metadata from 172.18.0.3:59994 conn1: { driver: { name: "nodejs", version: "3.3.4" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "5.0.0-32-generic" }, platform: "'Node.js v10.14.2, LE (unified)" }
mongodb-load-service    | Error occured
mongodb-load-service    | { Error: Command failed: mongoimport -d test -c data --type csv --file files/Mock_Members-a4ae.csv --headerline --host localhost:27017
mongodb-load-service    | /bin/sh: mongoimport: not found
mongodb-load-service    | 
mongodb-load-service    |     at ChildProcess.exithandler (child_process.js:294:12)
mongodb-load-service    |     at ChildProcess.emit (events.js:182:13)
mongodb-load-service    |     at maybeClose (internal/child_process.js:962:16)
mongodb-load-service    |     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
mongodb-load-service    |   killed: false,
mongodb-load-service    |   code: 127,
mongodb-load-service    |   signal: null,
mongodb-load-service    |   cmd:
mongodb-load-service    |    'mongoimport -d test -c data --type csv --file files/Mock_Members-a4ae.csv --headerline --host localhost:27017',
mongodb-load-service    |   stdout: '',
mongodb-load-service    |   stderr: '/bin/sh: mongoimport: not found\n' }

In local dev env. i have installed below packages to run the code successfully:

sudo apt-get install -y mongodb-server-core
sudo apt install mongodb-clients
sudo apt install mongo-tools

But when i run the same commands through the Dockerfile using

RUN apk add mongodb-server-core

It gives some missing error.

Please help.

iAviator
  • 1,310
  • 13
  • 31
  • You need to have the client tool installed in your image to be able to run it; installing it on your host doesn't help anything. https://pkgs.alpinelinux.org/packages might help you find an appropriate package, or you can switch your image to be based on Debian or Ubuntu. You probably don't need the server package installed. – David Maze Nov 22 '19 at 12:24
  • What probably i need to give is the --host option inmongoimport command. --host should point to the mongo docker container. In mongo image the mongoimport command works. So i just need to tell in my code that run mongoimport command on mongo container host. – iAviator Nov 22 '19 at 12:26
  • Would be helpful to post the 'some missing error' – Quinten Scheppermans Nov 22 '19 at 12:56
  • ERROR: unsatisfiable constraints: mongodb (missing): required by: world[mongodb] – iAviator Nov 22 '19 at 13:01
  • I'm guessing one off your packages are only available in edge alpine repos (or not at all). apk can only get them from stable repos. – Quinten Scheppermans Nov 22 '19 at 13:04
  • Using this https://stackoverflow.com/questions/53850369/issue-installing-mongodb-on-alpine i installed mongod in alpine image. But stiil the error is same. Mongoimport command not found. – iAviator Nov 22 '19 at 13:14
  • I need to see the part of your dockerfile where you are actually trying to install mongo-tools (which is the package that should have the mongoimport command). You can edit your post. – Quinten Scheppermans Nov 22 '19 at 13:23
  • Updated dockerfile. Also added detailed error for mongoimport command not found. – iAviator Nov 22 '19 at 13:26

1 Answers1

4

Add the following to your dockerfile:

RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/community' >> /etc/apk/repositories
RUN apk add mongodb-tools
Quinten Scheppermans
  • 954
  • 1
  • 10
  • 29
  • It worked. Now getting this error. mongodb-load-service | { Error: Command failed: mongoimport -d test -c data --type csv --file files/Mock_Members-5e410.csv --headerline --host localhost:27017 Failed: error connecting to db server: no reachable servers – iAviator Nov 22 '19 at 13:41
  • MONGODB_URI=mongodb://mongo/test, you can connect to another docker container in the same network by replacing its hostname by its containername. So in you case that would be MONGODB_URI:mongo://......... But they also need to be in the same network so add a network to your compose file and add both of your containers to it. – Quinten Scheppermans Nov 22 '19 at 13:45
  • Another issue that you might have is that your seed container is trying to connect to your mongo container that is still starting up. You can add the depends_on tag to your seed container to help with this a little. It's won't always work since it just influences build order of the container, not run order of the applications within. – Quinten Scheppermans Nov 22 '19 at 13:49
  • using --host as mongo:27017 and could u let me know the network to be added in docker-compose? Wil this do:- networks: default: driver: custom-driver-1 – iAviator Nov 22 '19 at 13:54
  • You don't need a custom driver, this stackoverflow answer will help you: https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects – Quinten Scheppermans Nov 22 '19 at 14:03
  • I'm sorry but I can't really help you with a different issue in this thread. This is against Stackoverflow policy. If you need help with that issue, you're going to need to open a different one. You can link it here if you want. – Quinten Scheppermans Nov 22 '19 at 15:48