I have a docker-compose.yml file containing 2 services, one container for the meteorjs application and one container for my mongodb database.
This is my docker-compose.yml
version: ‘3’
services:
app:
image: myapp
build:
context: .
dockerfile: Dockerfile
volumes:
- backup:/backup
ports:
- '3000:3000'
depends_on:
- mongo
environment:
ROOT_URL: ${APP_ROOT_URL:-http://application-url}
MONGO_URL: mongodb://mongo:27017/meteor
PORT: 3000
NODE_ENV: production
mongo:
image: mongo:latest
build:
context: .
dockerfile: mongo.Dockerfile
command:
- --storageEngine=wiredTiger
volumes:
- data:/data/db
- backup:/backup
ports:
- '27017:27017'
environment:
MONGO_INITDB_DATABASE: meteor
volumes:
data:
backup:
Now, my application downloads a database (scp) into the /backup volume (I don’t have scp or ssh on the mongodb container).
Then, I have a script on my mongodb container that restores the database ( mongorestore ) using the database in /backup.
My problem is that the database in /backup is not available when the mongodb container is started, I tried creating a separate Dockerfile for my mongodb container
FROM mongo
COPY ./sync-prod-with-ci-db.sh /docker-entrypoint-initdb.d/sync-prod-with-ci-db.sh
RUN chmod 777 /docker-entrypoint-initdb.d/sync-prod-with-ci-db.sh
The above Dockerfile comes following the official Mongo Docker Documentation, unfortunately that doesn’t work too (should it work? am I missing something here?)
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 bymongo
using the database specified by theMONGO_INITDB_DATABASE
variable, if it is present, ortest
otherwise. You may also switch databases within the.js
script.
My sync-prod-with-ci-db.sh script :
#!/usr/bin/env bash
DB_SNAPSHOT_NAME='database-snapshot.tar.gz'
echo "Restoring prod DB snapshot to Mongo Container ..."
tar -xzvf /backup/${DB_SNAPSHOT_NAME}
mongorestore -h 127.0.0.1 --drop -d meteor ./backup/my_database
How can I run the sync-prod-with-ci-db.sh script AFTER both the mongo and the myapp containers have started? I would like for it to be done automatically when I run docker-compose up -d --build
PS: IF I RUN THE SCRIPT MANUALLY IT WORKS