docker-lambda
docker-lambda copied to clipboard
Adding a `docker exec` option
@mhart First, thanks for all the contributions! I've used / gotten inspiration from several of your node-based containers from docker hub.
When I'm testing, I prefer to start the container once and keep it running, rather than executing docker run
each time. With that in mind, I made some simple changes to docker-lambda
to allow the use of docker exec
on a running container. Here's the relevant piece:
options = options || {};
var dockerImage = options.dockerImage || 'lambci/lambda:nodejs4.3';
var handler = options.handler || 'index.handler';
var event = options.event || {};
var taskDir = options.taskDir == null ? process.cwd() : options.taskDir;
var cleanUp = options.cleanUp == null ? true : options.cleanUp;
var addEnvVars = options.addEnvVars || false;
var dockerCmd = options.dockerCmd || 'run';
var dockerArgs = options.dockerArgs || [];
var spawnOptions = options.spawnOptions || { encoding: 'utf8' };
var returnSpawnResult = options.returnSpawnResult || false;
var args = [dockerCmd];
if (dockerCmd === 'run') {
args = args
.concat(taskDir ? ['-v', taskDir + ':/var/task'] : [])
.concat(cleanUp ? ['--rm'] : [])
.concat(addEnvVars ? ENV_ARGS : [])
.concat(dockerArgs)
.concat([dockerImage, handler, JSON.stringify(event)]);
} else {
args = args
.concat(addEnvVars ? ENV_ARGS : [])
.concat(dockerArgs)
.concat([
'node',
'--expose-gc',
'--max-semi-space-size=150',
'--max-old-space-size=2707',
'/var/runtime/node_modules/awslambda/index.js',
])
.concat([handler, JSON.stringify(event)]);
}
I added a dockerCmd
option that accepts either run
or exec
, and defaults to run
. When using exec
, the dockerArgs
option takes the name of the running container. I then just execute the same command that you have in your entrypoint
config.
It's not a huge difference, but using exec
cuts about a second off each run on a basic hello world
type test. If you're interested, I can create a PR for this -- or you can just copy and paste the above. Everything else is identical.
Ideally, to really speed up testing, I'd like to keep the node process running the same way lambda does. Do you have any insight on how I might go about that?