title: Java Testcontainers - Cannot connect to exposed port
body: <p>I implemented a POP3 server and client using javax.mail just to try doing integration testing with Docker. So I created two Docker images based on the openjdk:8-jre image and copied my jars to them and started it. Based on my configuration (see below) it is working. They are talking to each other.</p>
  <p>But as want to have multiple integration tests it is going to be tedious to build an image for each one and starting them. Also I don't know how to automate the results.
  But then I stumbled across TestContainers and it seems that this would be big help when implementing those tests.</p>
  <p>So I started to port those tests to TestContainers using my POP3 Server image as a GenericContainer and starting my POP3 Client classes in the JUnit test method. I exposed the port 24999 which my POP3 Server is listening to. But when I try to connect to the server I get the following error:</p>
  <pre><code>com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 32782; timeout -1;
    nested exception is:
      java.net.ConnectException: Connection refused
  ...
  </code></pre>
  <p>There is probably some setting I am missing in TestContainers. Could you please help me.</p>
  <p>Here is the code I am using:</p>
  <pre><code>public class DockerPop3AutocryptKeyProvidingAndReceivingTest {
      @Test
      public void test() throws InterruptedException {
          GenericContainer container = new GenericContainer&lt;&gt;(&quot;immerfroehlich/emailfilter:latest&quot;)
                  .withExposedPorts(24999);
          
          container.start();
          
          String host = container.getContainerIpAddress();
          String port = container.getFirstMappedPort().toString();
  
          //The following is simplified, but copied from the working jar used in the Docker Client image/container
          MyPOP3Client client = new MyPOP3Client(host, port);
          client.connect();
          
          container.stop();
      }
  }
  </code></pre>
  <p>This is how I create my Docker image:</p>
  <pre><code>FROM openjdk:8-jre
  
  ADD build/distributions/MyPOP3Server.tar . #This is where I have packed all the needed files to. It gets unpacked by Docker.
  #EXPOSE 24999 #I tried both with and without this expose
  WORKDIR /MyPOP3Server/bin
  ENTRYPOINT [&quot;sh&quot;,&quot;MyPOP3Server&quot;] #Executes the shell script which runs java with my jar
  </code></pre>
  <p>This is a simplified version of the code that is running inside the Server Jar:</p>
  <pre><code>MyPOP3Server server = new MyPOP3Server();
  server.listenToPort(24999);
  </code></pre>
  <p>Please tell me what am I missing. What is wrong here?</p>
  <p>Thanks and kind regards.</p>