Search code examples
mongodbdocker

How to start mongodb from dockerfile


I'm building a dockerfile. But I meet a problem. It says that :

/bin/sh: 1: mongod: not found

My dockerfile:

FROM mongo:latest
FROM node

RUN mongod

COPY . .
RUN node ./scripts/import-data.js

Here is what happen when docker build:

Sending build context to Docker daemon  829.5MB
Step 1/8 : FROM rabbitmq
 ---> e8261c2af9fe
Step 2/8 : FROM portainer/portainer
 ---> 00ead811e8ae
Step 3/8 : FROM docker.elastic.co/elasticsearch/elasticsearch:6.5.1
 ---> 32f93c89076d
Step 4/8 : FROM mongo:latest
 ---> 5976dac61f4f
Step 5/8 : FROM node
 ---> b074182f4154
Step 6/8 : RUN mongod
 ---> Running in 0a4b66a77178
/bin/sh: 1: mongod: not found
The command '/bin/sh -c mongod' returned a non-zero code: 127

Any idea ?


Solution

  • The problem is that you are using two FROM instructions, which is referred to as a multi-stage build. The final image will be based on the node image that doesn't contain the mongo database.

    * Edit *

    here are more details about what is happening:

    FROM mongo:latest

    • the base image is mongo:latest

    FROM node

    • now the base image is node:latest. The previous image is just standing there...

    RUN mongod

    COPY . .

    RUN node ./scripts/import-data.js

    • now you run mongod and the other commands in your final image that is based on node (which doesn't contain mongo)