简体   繁体   English

如何从 dockerfile 启动 mongodb

[英]How to start mongodb from dockerfile

I'm building a dockerfile.我正在构建一个 dockerfile。 But I meet a problem.但是我遇到了一个问题。 It says that :它说:

/bin/sh: 1: mongod: not found /bin/sh: 1: mongod: 未找到

My dockerfile:我的码头档案:

FROM mongo:latest
FROM node

RUN mongod

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

Here is what happen when docker build:这是 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 ?任何的想法 ?

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. 发生这种情况的原因是,应将多个FROM指令用于多阶段构建(请参阅文档),而不是用于映像创建包含所有当前应用程序。

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. 换句话说,如果要使用rabbitmq,mongo和其他应用程序创建映像,则必须选择该映像并手动安装应用程序。

The problem is that you are using two FROM instructions, which is referred to as a multi-stage build . 问题是您正在使用两个FROM指令,这被称为multi-stage build The final image will be based on the node image that doesn't contain the mongo database. 最终图像将基于不包含mongo数据库的node图像。

* Edit * *编辑*

here are more details about what is happening: 以下是有关正在发生的事情的更多详细信息:

FROM mongo:latest 来自mongo:latest

  • the base image is mongo:latest 基本映像是mongo:latest

FROM node FROM节点

  • now the base image is node:latest . 现在基本映像是node:latest The previous image is just standing there... 上一张图片只是站在那儿...

RUN mongod RUN mongod

COPY . 复制。 .

RUN node ./scripts/import-data.js RUN节点./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) 现在您在基于node最终映像中运行mongod和其他命令(不包含mongo)

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. 使用docker-composehttps://docs.docker.com/compose/install/ )运行映像,而不是尝试从现有映像的集合中构建新映像。 Your docker-compose.yml might look something like: docker-compose.yml可能类似于:

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:
version: '3'
services:
  mongo:
    image: 'mongo:latest'
    container_name: 'mongodb'
    hostname: 'mongo'
    domainname: 'example.com'
    restart: 'always'
    environment:
      - 'MONGO_INITDB_ROOT_USERNAME=root'
      - 'MONGO_INITDB_ROOT_PASSWORD=example'
    ports:
      - '27017:27017'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM