本文介绍了创建后mongo docker映像未运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker-compose为前端,后端和mongo实例创建3个不同的容器.它们中的3个正在运行并在它们之间建立连接,但是我需要在mongo实例运行后立即在数据库上创建一个管理员用户.根据mongo image文档,创建实例后,应运行位于 docker-entrypoint-initdb.d 上的每个脚本.正如我已经说过的那样,我正在使用docker compose,因此mongo没有 Dockerfile ,只有使用官方mongo映像的服务,因此我为此文件设置了一个卷[不知道是否由于该脚本未运行,因为它是一个符号链接,而不是复制的文件].所以我尝试运行命令来运行此脚本,但是它不起作用,但是如果我对mongodb实例执行 docker exec 并由我自己运行脚本,它将可以工作并创建管理员用户.

I'm using docker-compose to create 3 diferent containers for a frontend, backend and mongo instance. the 3 of them are running and connected between them, but I need to create an admin user on DB as soon as the mongo instance is running. According to mongo image documentation every script located on docker-entrypoint-initdb.d should be run after the instance is created. As I already said I'm using docker compose so theres no Dockerfile for mongo, just a service using the official mongo image so I've set a volume to this file [don't know if because of that the script is not running, because is a symlink instead of a copied file]. So I tried to run a command to run this script but it doesn't work, but if then I do a docker exec to the mongodb instance and run the script by myself it will work and create the admin user.

那么,如何在创建实例之后使该脚本运行?

So, how can i make this script run after the instance is created?

这是我的 docker-compose.yml 文件

mongodb:
    container_name: mongodb
    image: mongo
    command: /bin/bash "cd /docker-entrypoint-initdb.d && mongo-init.sh"
    networks:
      - backend-network
    volumes:
      - './db:/data/db'
      - './mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh'
    tty: true
    restart: always

推荐答案

您不需要修改mongo命令,因为它将导致mongodb容器出现问题.通过定义 command ,您将覆盖预期会运行的原始 CMD .只要您将脚本安装在/docker-entrypoint-initdb.d/下,该脚本就会自动执行,如此处

You don't need to modify the mongo command because it will cause issues to the mongodb container. By defining command you will override the original CMD which expected to run. the script will be executed automatically as long as you have mounted it under /docker-entrypoint-initdb.d/ as explained in here

请注意,脚本只会在容器开始时执行一次,并且要使其再次执行,您需要删除与容器关联的/data/db

Note that scripts will be executed only one time at the start of your container and in order to make it execute again you need to delete the /data/db volume which is associated with your container

这篇关于创建后mongo docker映像未运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:32