I did setup a docker-pose file that connects my app to a mongoDB database. My problem is that the database seems to never be initialized at first. My script is not executed and even tho' I can send some requests to the container, I only get connection refused errors due to authentification.
I did follow exactly this thread and I don't know what I'm missing out! (the db
folder is on the same level as my docker-pose.yml
)
Looking for some help on this one, thanks!
edit: None of the logs I did put in the init script are showing in the console, that's how I went to the conclusion that the file is not executed at all.
Here is my docker-pose file:
services:
mongo:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: test
volumes:
- ./db:/docker-entrypoint-initdb.d
- ./db-data:/data/db
ports:
- 27017:27017
networks:
- api1
app:
restart: always
build:
context: .
environment:
DB_HOST: localhost
DB_PORT: 27017
DB_NAME: test
DB_USER: developer
DB_PASS: developer
PORT: 3000
ports:
- 3000:3000
networks:
- api1
depends_on:
- mongo
mand: npm start
networks:
api1:
driver: bridge
Here is my init scipt:
/* eslint-disable no-undef */
try {
print("CREATING USER")
db.createUser(
{
user: "developer",
pwd: "developer",
roles: [{ role: "readWrite", db: "test" }]
}
);
} catch (error) {
print(`Failed to create developer db user:\n${error}`);
}
And my dockerfile:
FROM node:10 as builder
RUN mkdir /home/node/app
WORKDIR /home/node/app
# Install dependencies
COPY package.json yarn.lock ./
RUN yarn install && yarn cache clean
# Copy source scripts
COPY . .
RUN yarn build
FROM node:10-alpine
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
COPY --from=builder --chown=node:node /home/node/app .
USER node
EXPOSE 3000
CMD ["node", "./build/bundle.js"]
I did setup a docker-pose file that connects my app to a mongoDB database. My problem is that the database seems to never be initialized at first. My script is not executed and even tho' I can send some requests to the container, I only get connection refused errors due to authentification.
I did follow exactly this thread and I don't know what I'm missing out! (the db
folder is on the same level as my docker-pose.yml
)
Looking for some help on this one, thanks!
edit: None of the logs I did put in the init script are showing in the console, that's how I went to the conclusion that the file is not executed at all.
Here is my docker-pose file:
services:
mongo:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: test
volumes:
- ./db:/docker-entrypoint-initdb.d
- ./db-data:/data/db
ports:
- 27017:27017
networks:
- api1
app:
restart: always
build:
context: .
environment:
DB_HOST: localhost
DB_PORT: 27017
DB_NAME: test
DB_USER: developer
DB_PASS: developer
PORT: 3000
ports:
- 3000:3000
networks:
- api1
depends_on:
- mongo
mand: npm start
networks:
api1:
driver: bridge
Here is my init scipt:
/* eslint-disable no-undef */
try {
print("CREATING USER")
db.createUser(
{
user: "developer",
pwd: "developer",
roles: [{ role: "readWrite", db: "test" }]
}
);
} catch (error) {
print(`Failed to create developer db user:\n${error}`);
}
And my dockerfile:
FROM node:10 as builder
RUN mkdir /home/node/app
WORKDIR /home/node/app
# Install dependencies
COPY package.json yarn.lock ./
RUN yarn install && yarn cache clean
# Copy source scripts
COPY . .
RUN yarn build
FROM node:10-alpine
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
COPY --from=builder --chown=node:node /home/node/app .
USER node
EXPOSE 3000
CMD ["node", "./build/bundle.js"]
Share
Improve this question
edited Jun 13, 2020 at 0:37
Alex Mass
asked Jun 13, 2020 at 0:26
Alex MassAlex Mass
3013 silver badges11 bronze badges
3
- 13 The code in the docker-entrypoint-init.d folder is only executed if the database has never been initialized before (I.e. if the /data/dB folder is empty. As such, delete the mounted ./db folder in your host machine (it will get recreated automatically) before doing docker-pose up. This will cause your scripts to be run. – camba1 Commented Jun 13, 2020 at 1:44
- @camba1 - well spotted. This was a problem I was having. I had to clear it - docker volume prune. This practically saved me after hours of debugging. – Inspiraller Commented Mar 25, 2023 at 9:35
-
I had a problem with such a setup when I forgot
docker pose down
after... up
. Such another...up
just restarted existing containers without recognizing changes in environment a.s.o. – JeffRSon Commented Mar 31 at 6:55
2 Answers
Reset to default 6In case other people miss the ment from #camba1 like I did, the issue here is that MongoDB will only run the initialization script when it first loads. Thus, if you have previously started MongoDB and you persist the data volume your script will not run.
Make sure to remove the volume between iterations (or simply remove it from the pose while you are working on initialization scripts).
I got the same problem when set up a mongodb by docker pose. After a few hours try, I finally make the .js
method work, I was try in to work out the .sh
way. Here is what set up in the docker-pose.yml
#docker-pose.yml
mongodb_dev:
image: mongo:6.0.5 # version matters, according to the offical doc
container_name: mongo_db_container
restart: always
environment:
TZ: "Asia/Hong_Kong"
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
MONGO_INITDB_DATABASE: init_db
ports:
- 27017:27017
volumes:
- ./app-database/data/db:/data/db
- ./app-database/data/log:/var/log/mongodb
- ./app-database/mongod.conf:/etc/mongod.conf.
- ./app-database/init.d:/docker-entrypoint-initdb.d # !!important, map the folder instead of the any actual file
networks:
- dev
And here is how I structure the files.
I found the work around in Issue here and found some important information in the official doc here initializing-a-fresh-instance
When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongosh (mongo on versions below 6) using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.