MathKimRobin
MathKimRobin

Reputation: 1500

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 ?

Upvotes: 5

Views: 9795

Answers (5)

Zitoun
Zitoun

Reputation: 720

For some testing purpose, I actually had to cram both MongoDB and NodeJS in the same container.

I first tried to use the Alpine-based Node image

FROM node:20-alpine3.19

but figured out you could not import Mongo through apk commands as it's not available anymore on the latest Alpine.

I thus did it the other way around, starting from the Ubuntu-based Mongo image and using apt-get to import node and npm in the image

FROM mongo:7.0.7

RUN apt-get update && apt-get install -y curl && \
    curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

Upvotes: 0

Siwei
Siwei

Reputation: 21617

I see, quite simple

step1. create this Dockerfile:

FROM mongo:latest

step2. create image from this Dockerfile:

docker build . -t my_mongo_build

This is equal to docker run ..... mongo:latest, used for some strange scenario

Upvotes: -1

masseyb
masseyb

Reputation: 4150

Use docker-compose (https://docs.docker.com/compose/install/) to run the images rather than attempting to build a new image from a collection of existing images. Your docker-compose.yml might look something like:

version: '3.7'
services:
  portainer:
    image: 'portainer/portainer'
    container_name: 'portainer'
    hostname: 'portainer'
    domainname: 'example.com'
    volumes:
    - '/var/run/docker.sock:/var/run/docker.sock'
    - 'portainer_data:/data'
    ports:
    - '9000:9000'
  rabbitmq:
    image: 'rabbitmq'
    container_name: 'rabbitmq'
    hostname: 'rabbitmq'
    domainname: 'example.com'
    volumes:
    - 'rabbitmq_data:/var/lib/rabbitmq'
  elasticsearch:
    image: 'elasticsearch:7.1.1'
    container_name: 'elasticsearch'
    hostname: 'elasticsearch'
    domainname: 'example.com'
    environment:
    - 'discovery.type=single-node'
    volumes:
    - 'elasticsearch_data:/usr/share/elasticsearch/data'
    ports:
    - '9200:9200'
    - '9300:9300'
  node:
    image: 'node:12'
    container_name: 'node'
    hostname: 'node'
    domainname: 'example.com'
    user: 'node'
    working_dir: '/home/node/app'
    environment:
    - 'NODE_ENV=production'
    volumes:
    - './my-app:/home/node/app'
    ports:
    - '3000:3000'
    command: 'npm start'
  mongo:
    image: 'mongo'
    container_name: 'mongo'
    hostname: 'mongo'
    domainname: 'example.com'
    restart: 'always'
    environment:
    - 'MONGO_INITDB_ROOT_USERNAME=root'
    - 'MONGO_INITDB_ROOT_PASSWORD=example'
    volumes:
    - 'mongo_data:/data/db'
volumes:
  portainer_data:
  rabbitmq_data:
  elasticsearch_data:
  mongo_data:

Upvotes: 3

Adi Fatol
Adi Fatol

Reputation: 964

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)

Upvotes: 4

MartenCatcher
MartenCatcher

Reputation: 2887

It happens because multiple FROM instructions should be used for Multistage Build (check the documentation) and NOT for image creation contains all of present applications.

Multistage builds provide you possibility of delegation building process into container's environment without local application installation.

FROM rabbitmq

...some instructions require rabbitmq...

FROM mongo:latest

...some instructions require mongo...

In other words if you want to create an image with rabbitmq, mongo and other application you have to choose the image and install applications manually.

Upvotes: 2

Related Questions