简体   繁体   English

Docker docker-entrypoint-initdb.d/ 带有 Mongodb 的脚本不起作用

[英]Docker docker-entrypoint-initdb.d/ scripts with Mongodb aren't working

I have been struggling to configure the execution of scripts via /docker-entrypoint-initdb.d in a Mongo container in Docker.我一直在努力通过 /docker-entrypoint-initdb.d 在 Docker 的 Mongo 容器中配置脚本的执行。

I have tried to follow the documentation carefully, but without success.我试图仔细遵循文档,但没有成功。

project structure项目结构

在此处输入图像描述

docker-compose.yml docker-compose.yml

version: '3.3'
services:
  mongodb:
    image: mongo:latest
    container_name: mongo_db
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: rootpass
      MONGO_INITDB_DATABASE: database
    volumes:
      - mongodbdata:/data/db
      - ./init.d/:/docker-entrypoint-initdb.d/
    restart: always
volumes:
  mongodbdata:
    driver: local

init-mongo.sh初始化mongo.sh

#!/bin/bash
echo 'hello script'

In the logs the command echo 'hello script' is not executed.在日志中未执行命令 echo 'hello script'。

What should I do?我应该怎么办? Is there something I'm missing?有什么我想念的吗?

Docker only executes scripts inside docker-entrypoint-initdb.d upon creation of the image as this is mostly used for seeding data. Docker 仅在创建映像时执行 docker docker-entrypoint-initdb.d中的脚本,因为这主要用于播种数据。 If it's not a fresh image and you're just trying to spawn it again with your script then that's what caused it.如果它不是一个新鲜的图像,而您只是想用您的脚本再次生成它,那么这就是造成它的原因。

What you can try to do is type this in your terminal:您可以尝试在终端中输入以下内容:

docker stop $(docker ps -aq) && docker rm $(docker ps -aq) && docker rmi mongo:latest -f && docker-compose up

Then check the logs:P然后检查日志:P

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

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