我有一个用于以下节点js代码的dockerfile
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文件如下:-
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"
我在节点js代码中使用mongoimport命令在mongodb中导入csv文件。当我在本地开发环境(没有docker)上运行时,代码运行良好,但当我通过docker compose文件运行时,代码运行不正常。
我得到的错误是:-
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' }
在本地开发环境中。我已经安装了以下软件包以成功运行代码:
sudo apt-get install -y mongodb-server-core
sudo apt install mongodb-clients
sudo apt install mongo-tools
但是当我使用
通过Dockerfile运行相同的命令时RUN apk add mongodb-server-core
它给出了一些丢失的错误。
请帮助。
答案 0 :(得分:1)
将以下内容添加到您的dockerfile中:
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/community' >> /etc/apk/repositories
RUN apk add mongodb-tools