title: Tomcat embedded correct usage of addContext for docBase
body: <p>Does anyone know how to set up the context in an embedded Tomcat instance to serve files from a local directory not within the deployed folder?</p>
  
  <p>In the standard server.xml it looks something like this:</p>
  
  <pre><code>&lt;Context docBase="/MyWebApp/images" path="/tmp/images/" reloadable="false"/&gt;
  </code></pre>
  
  <p>I have tried different variations on the following tomcat embedded call without success:</p>
  
  <pre><code>tomcat.addContext(tomcat.getHost(), "/MyWebApp/images", "/tmp/images/");
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>tomcat.addContext("/MyWebApp/images", "/tmp/images/");
  </code></pre>
  
  <p>It looks like this grails question was along the same lines:</p>
  
  <p><a href="https://stackoverflow.com/questions/8332851/grails-add-context-to-embedded-tomcat-in-development">grails: add context to embedded tomcat in development</a></p>
  
  <p>But im not using grails. (Using Java)</p>
  
  <p>Here is the full startup code I am using:</p>
  
  <pre><code>package launch;
  
  import java.io.File;
  import org.apache.catalina.startup.Tomcat;
  
  public class Main {
  
      public static void main(String[] args) throws Exception {
  
          String webappDirLocation = "/src/main/webapp/";
          Tomcat tomcat = new Tomcat();
  
          //The port that we should run on can be set into an environment variable
          //Look for that variable and default to 8080 if it isn't there.
          String webPort = System.getenv("PORT");
          if(webPort == null || webPort.isEmpty()) {
              webPort = "8080";
          }
  
          tomcat.setPort(Integer.valueOf(webPort));
  
          tomcat.addWebapp("/MyWebApp", new File(webappDirLocation).getAbsolutePath());
          tomcat.addContext("/MyWebApp/images", "/tmp/images/");
  
          tomcat.start();
          tomcat.getServer().await();  
      }
  }
  </code></pre>