Documentationcurrent version
Help us improve the docs by fixing typos and proposing enhancements.


Action ""

Execute a docker command.

Schema definitions

definitions =
    type: 'object'
    allOf: [
      $ref: '#/definitions/docker'
          type: ['boolean', 'string']
          description: '''
          Serialize the command into a file and execute it with bash.
          oneOf: [
            type: 'string'
            typeof: 'function'
          description: '''
          String, Object or array; Command to execute. A value provided as a
          function is interpreted as an action and will be called by forwarding
          the config object. The result is the expected to be the command
          to execute.
          type: 'string'
          description: '''
          Current working directory from where to execute the command.
          type: 'array'
          default: [0]
            type: 'integer'
          description: '''
          Expected code(s) returned by the command, int or array of int, default
          to 0.
          $ref: '#/definitions/docker'
        $ref: 'module://@nikitajs/core/lib/actions/execute'
    required: ['command']
  # Note, we can't use additionalProperties properties with anyOf for now,
  # from the doc: "There are some proposals to address this in the next
  # version of the JSON schema specification."
  # additionalProperties: false
    type: 'object'
    description: '''
    Isolate all the parent configuration properties into a docker
    property, used when providing and cascading a docker configuration at
    a global scale.
        type: 'boolean'
        default: false
        description: '''
        Whether to use boot2docker or not.
        type: 'boolean'
        description: '''
        Use the `docker-compose` command instead of `docker`.
        type: 'string'
        format: 'hostname'
        description: '''
        Name of the docker-machine, required if using docker-machine.
# (
#["#{property}"] =
#     $ref: "module://@nikitajs/core/lib/actions/execute#/properties/#{property}"
# ) for property in [
#   'code_skipped', 'dry', 'env', 'format', 'gid', 'stdin_log',
#   'stdout', 'stdout_return', 'stdout_log',
#   'stderr', 'stderr_return', 'stderr_log',
#   'sudo', 'target', 'trap', 'uid'
# ]


handler = ({config, tools: {find}}) ->
  # Global config
  config.docker = await find ({config: {docker}}) -> docker
  config[k] ?= v for k, v of config.docker
  opts = for option in utils[ unless config.compose then 'options' else 'compose_options' ]
    value = config[option]
    continue unless value?
    value = 'true' if value is true
    value = 'false' if value is false
    if option in ['tlsverify'] then  "--#{option}" else "--#{option}=#{value}"
  opts = opts.join ' '
  bin = if config.compose then 'bin_compose' else 'bin_docker'
    await @execute config,
      command: """
      export SHELL=/bin/bash
      export PATH=/opt/local/bin/:/opt/local/sbin/:/usr/local/bin/:/usr/local/sbin/:$PATH
      bin_boot2docker=$(command -v boot2docker)
      bin_docker=$(command -v docker)
      bin_machine=$(command -v docker-machine)
      bin_compose=$(command -v docker-compose)
      machine='#{config.machine or ''}'
      boot2docker='#{if config.boot2docker then '1' else ''}'
      if [[ $machine != '' ]] && [ $bin_machine ]; then
        if [ -z "#{config.machine or ''}" ]; then exit 5; fi
        if docker-machine status "${machine}" | egrep 'Stopped|Saved'; then
          docker-machine start "${machine}";
        eval "$(${bin_machine} env ${machine})"
      elif [[ $boot2docker != '1' ]] && [  $bin_boot2docker ]; then
        eval "$(${bin_boot2docker} shellinit)"
      $#{bin} #{opts} #{config.command}
  catch err
    throw Error err.stderr.trim() if utils.string.lines(err.stderr.trim()).length is 1
    throw Error err.stderr.trim().replace 'Error response from daemon: ', '' if /^Error response from daemon/.test err.stderr


module.exports =
  handler: handler
  # hooks:
  #   on_action: require('@nikitajs/core/lib/actions/execute').hooks.on_action
    argument_to_config: 'command'
    definitions: definitions


utils = require '../utils'
Edit on GitHub

Nikita is an open source project hosted on GitHub and developed by Adaltas.