title: Docker container cannot access LDAP server
body: <p>I have a application need to access a <strong>LDAP</strong> server (Active Directory) and it works properly when is running locally. However when it runs inside a docker container the application cannot access the Active Directory server.
  I executed a ping command inside the container and it worked.</p>
  
  <pre><code># ping 10.10.2.1
  PING 10.10.2.1 (10.10.2.1) 56(84) bytes of data.
  64 bytes from 10.10.2.1: icmp_seq=1 ttl=37 time=14.7 ms
  64 bytes from 10.10.2.1: icmp_seq=2 ttl=37 time=16.5 ms
  64 bytes from 10.10.2.1: icmp_seq=3 ttl=37 time=14.5 ms
  64 bytes from 10.10.2.1: icmp_seq=4 ttl=37 time=11.8 ms
  64 bytes from 10.10.2.1: icmp_seq=5 ttl=37 time=15.8 ms
  ^C
  --- 10.10.2.1 ping statistics ---
  5 packets transmitted, 5 received, 0% packet loss, time 4006ms
  rtt min/avg/max/mdev = 11.854/14.700/16.554/1.606 ms
  
  </code></pre>
  
  <p>The code below is responsible for access the LDAP server.</p>
  
  <pre class="lang-cs prettyprint-override"><code>        /// &lt;summary&gt;
          /// Construtor da classe
          /// &lt;/summary&gt;
          /// &lt;param name="configuration"&gt;Parâmetro de configuração&lt;/param&gt;
          public ActiveDirectoryClient(IConfiguration configuration, ILogger&lt;ActiveDirectoryClient&gt; logger)
          {
              _logger = logger;
              _configuration = configuration;
              _domain = _configuration["LDAPAdress"];
          }
  
          /// &lt;summary&gt;
          /// Tenta conectar com o ActiveDirectory até uma quantidade de vezes e um intervalo de tempo fornecido em segundos
          /// &lt;/summary&gt;
          /// &lt;param name="attemps"&gt;Número de tentativas&lt;/param&gt;
          /// &lt;param name="retryInterval"&gt;Intervalo de tempo entre as tentativas em segundos&lt;/param&gt;
          /// &lt;returns&gt;Retorna o contexto do Active Directory&lt;/returns&gt;
          /// &lt;exception cref="CustomException"&gt;Lançada quando não é possível se conectar com o ActiveDiretory&lt;/exception&gt;
          private PrincipalContext TryConnectActiveDirectory(int attemps, double retryInterval)
          {
              while (attemps &gt; 0)
              {
                  _context = null;
                  var isRetry = false;
  
  
                  _logger.LogCritical(_domain);
                  try
                  {
                      _context = new PrincipalContext(ContextType.Domain, _domain);
                  }
                  catch (Exception)
                  {
                      isRetry = true;
                      attemps--;
                      Thread.Sleep(TimeSpan.FromSeconds(retryInterval));
                  }
  
                  if (isRetry || _context.Container == null)
                  {
                      attemps--;
                      Thread.Sleep(TimeSpan.FromSeconds(retryInterval));
                  }
              }
  
              _logger.LogCritical(_domain);
  
              if (_context == null)
              {
                  throw new CustomException(HttpStatusCode.ServiceUnavailable, "Não foi possível conectar-se com o servidor");
              }
  
              return _context;
          }
  </code></pre>
  
  <p>This file have the configurations of the project</p>
  
  <pre><code>{
    "LDAPAdress": "10.10.2.1",
    "Logging": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "AllowedHosts": "*"
  }
  </code></pre>
  
  <p>I mapped the container port from <code>60331</code> to <code>80</code>. It is working because I have access to the swagger page throw browser on the host in the address <a href="http://localhost:60331" rel="nofollow noreferrer">http://localhost:60331</a>.</p>
  
  <p>I ran the <strong>command</strong> <code>tcpdump -i eth0 port 389 -v</code> inside the container to sniff all request to <code>389</code> port and nothing was returned, however when I run the application locally some requests of <code>389</code> port can be sniffed.</p>
  
  <p>Could someone help?</p>