Skip to content

Instantly share code, notes, and snippets.

@earnubs
Created February 5, 2019 10:18
Show Gist options
  • Save earnubs/f615de2586cb0e4b6b28b2c296a18dce to your computer and use it in GitHub Desktop.
Save earnubs/f615de2586cb0e4b6b28b2c296a18dce to your computer and use it in GitHub Desktop.
use dockerode to get container ip address
const Docker = require('dockerode');
const fs = require('fs');
const socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
const stats = fs.statSync(socket);
if (!stats.isSocket()) {
throw new Error('Are you sure the docker is running?');
}
const docker = new Docker({ socketPath: socket });
module.exports.getFrontendIP = function() {
return new Promise((resolve, reject) => {
docker.listContainers({}, (err, containers) => {
if (err) reject(err);
const container = containers.find(
node => node.Image === 'toolchain-web_frontend',
);
resolve(
container.NetworkSettings.Networks['toolchain-web_default'].IPAddress,
);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment