Skip to content

Instantly share code, notes, and snippets.

@jshbrntt
Created December 18, 2020 15:22
Show Gist options
  • Save jshbrntt/7f695a7271d191e292c1506714556442 to your computer and use it in GitHub Desktop.
Save jshbrntt/7f695a7271d191e292c1506714556442 to your computer and use it in GitHub Desktop.
Create MongoDB container that starts and initializes itself as a single node replicaset
const Docker = require('dockerode');
(async () => {
const docker = new Docker();
const container = await docker.createContainer({
name: 'mongodb',
Image: 'mongo:4.0.20@sha256:dd4cbe24eb8233db92b871cc556b77efcc7f9e67bc9516579796d4d08818273e',
Cmd: [
"bash",
"-c",
"set -m ; mongod --replSet rs0 & while ! 2> /dev/null > '/dev/tcp/0.0.0.0/27017'; do sleep 1; done ; mongo --eval 'rs.initiate()' ; fg 1"
],
HostConfig: {
AutoRemove: true,
Binds: [
'/srv/service-tests/config/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro'
]
}
});
process.on('SIGINT', async () => {
await container.remove({
force: true
});
});
const stream = await container.attach({
stderr: true,
stdout: true,
stream: true
});
stream.on('data', data => console.log(data.toString()));
stream.on('error', data => console.error(data.toString()));
stream.on('end', () => {
stream.removeAllListeners();
});
await container.start();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment