private void createDockerFile(final String directory, final List<String> filesToAdd)
      throws IOException {

    final List<String> commands = newArrayList();
    if (baseImage != null) {
      commands.add("FROM " + baseImage);
    }
    if (maintainer != null) {
      commands.add("MAINTAINER " + maintainer);
    }

    if (env != null) {
      final List<String> sortedKeys = Ordering.natural().sortedCopy(env.keySet());
      for (final String key : sortedKeys) {
        final String value = env.get(key);
        commands.add(String.format("ENV %s %s", key, value));
      }
    }

    if (workdir != null) {
      commands.add("WORKDIR " + workdir);
    }

    for (final String file : filesToAdd) {
      // The dollar sign in files has to be escaped because docker interprets it as variable
      commands.add(
              String.format("ADD %s %s", file.replaceAll("\\$", "\\\\\\$"), normalizeDest(file)));
    }

    if (runList != null && !runList.isEmpty()) {
      if (squashRunCommands) {
        commands.add("RUN " + Joiner.on(" &&\\\n\t").join(runList));
      } else {
        for (final String run : runList) {
          commands.add("RUN " + run);
        }
      }
    }

    if (healthcheck != null && healthcheck.containsKey("cmd")) {
      final StringBuffer healthcheckBuffer = new StringBuffer("HEALTHCHECK ");
      if (healthcheck.containsKey("options")) {
        healthcheckBuffer.append(healthcheck.get("options"));
        healthcheckBuffer.append(" ");
      }
      healthcheckBuffer.append("CMD ");
      healthcheckBuffer.append(healthcheck.get("cmd"));
      commands.add(healthcheckBuffer.toString());
    }

    if (exposesSet.size() > 0) {
      // The values will be sorted with no duplicated since exposesSet is a TreeSet
      commands.add("EXPOSE " + Joiner.on(" ").join(exposesSet));
    }

    if (user != null) {
      commands.add("USER " + user);
    }

    if (entryPoint != null) {
      commands.add("ENTRYPOINT " + entryPoint);
    }
    if (cmd != null) {
      // TODO(dano): we actually need to check whether the base image has an entrypoint
      if (entryPoint != null) {
        // CMD needs to be a list of arguments if ENTRYPOINT is set.
        if (cmd.startsWith("[") && cmd.endsWith("]")) {
          // cmd seems to be an argument list, so we're good
          commands.add("CMD " + cmd);
        } else {
          // cmd does not seem to be an argument list, so try to generate one.
          final List<String> args = ImmutableList.copyOf(
              Splitter.on(WHITESPACE).omitEmptyStrings().split(cmd));
          final StringBuilder cmdBuilder = new StringBuilder("[");
          for (final String arg : args) {
            cmdBuilder.append('"').append(arg).append('"');
          }
          cmdBuilder.append(']');
          final String cmdString = cmdBuilder.toString();
          commands.add("CMD " + cmdString);
          getLog().warn("Entrypoint provided but cmd is not an explicit list. Attempting to " +
                        "generate CMD string in the form of an argument list.");
          getLog().warn("CMD " + cmdString);
        }
      } else {
        // no ENTRYPOINT set so use cmd verbatim
        commands.add("CMD " + cmd);
      }
    }

    // Add VOLUME's to dockerfile
    if (volumes != null) {
      for (final String volume : volumes) {
        commands.add("VOLUME " + volume);
      }
    }

    // Add LABEL's to dockerfile
    if (labels != null) {
      for (final String label : labels) {
        commands.add("LABEL " + label);
      }
    }

    getLog().debug("Writing Dockerfile:" + System.lineSeparator() +
                   Joiner.on(System.lineSeparator()).join(commands));

    // this will overwrite an existing file
    Files.createDirectories(Paths.get(directory));
    Files.write(Paths.get(directory, "Dockerfile"), commands, UTF_8);
  }
Set the networking mode for the RUN instructions during build
src/main/java/com/spotify/docker/BuildMojo.java