4

I'm trying to access mongorestore/mongodump commands from within a docker image that's been run via gitlab-ci-multirunner.

My .gitlab-ci.yml looks something like this:

image: node:7.2.0

cache:
  key: "$CI_BUILD_REF_NAME"
  paths:
  - node_modules/

services:
  - mongo

variables:
  DOCKER_ENV: 'true'
  NODE_ENV: 'test'

all_tests:
  script:
  - npm install
  - npm install tsd -g
  - tsd install --save
  - node_modules/typescript/bin/tsc -p .
  - node node_modules/mocha/bin/_mocha --recursive tests --timeout 15000

In my tests I do mongodump/mongorestore. The error I get is:

Create Transaction Tests

Connected to MongoDB: mongodb://mongo:27017/test-db { Error: Command failed: mongorestore --port 27017 --drop /builds/project/repo/project/tests/testData/db/mongo/dump

/bin/sh: 1: mongorestore: not found

I have even tried running a mongorestore command with "docker run" in the scripts section:

- docker run --rm --link mongodb:mongo -v /root:/backup mongo bash -c ‘mongorestore /backup --host $MONGO_PORT_27017_TCP_ADDR’

I get this error:

/bin/bash: line 61: docker: command not found

I have to mention that I'm running this image on a shared docker runner provided by gitlab.

Any advice on how I can access "mongorestore" command from within my test?

  • Can I ask if you want to run mongodump and mongorestore within your node docker container because that's how they will be run in production? Or is it just for the tests, setting up test data? – Vince Bowdren Dec 07 '16 at 15:17
  • Just for tests and setting up data. – Iulian Gheorghita Dec 08 '16 at 11:36
  • 1
    If you don't need them in production, then perhaps you shouldn't have them in your container in testing either. It's worth looking into [using a separate container for seeding your data](http://stackoverflow.com/a/33397913), or seeing if you can drop/create test data using standard mongo calls. – Vince Bowdren Dec 08 '16 at 13:07

3 Answers3

2

Not the most elegant solution, but you can always create your own image on top of node:7.2.0 that has mongo installed, thus allowing the container to execute mongodump/restore. A Dockerfile for such an image should look like so - FROM node:7.2.0 RUN apt-get update -y && \ apt-get install -y mongodb

Then you can pull this image instead of the normal node image, and have mongo cli at your disposal.

Yaron Idan
  • 6,207
  • 5
  • 44
  • 66
  • Thank you for the answer. In case some else is interested, to be able to use the same runners I used Gitlab registry: https://about.gitlab.com/2016/05/23/gitlab-container-registry/. – Iulian Gheorghita Dec 07 '16 at 12:56
1

If you need only mongodump and mongorestore (part of mongodb database tools), there is slightly more elegant solution, which would make image slightly smaller... in my case only 8MB.

# MONGODB_DB_TOOLS_URL=https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu1804-x86_64-100.5.0.deb
ARG MONGODB_DB_TOOLS_URL
ADD ${MONGODB_DB_TOOLS_URL} /mongodb-tools.deb
RUN apt install /mongodb-tools.deb
RUN rm /mongodb-tools.deb

MONGODB_DB_TOOLS_URL can be found/created at MongoDB download site

Lukino
  • 1,407
  • 13
  • 14
0
FROM node:16.8.0-alpine

RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/main' >> /etc/apk/repositories
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories
RUN apk update
RUN apk add mongodb-tools
mehdi parastar
  • 737
  • 4
  • 13
  • 29