title: Writing Log4j2 logs to remote server from Docker container gives error
body: <p>I am using the <a href="https://logging.apache.org/log4j/2.0/log4j-core/apidocs/org/apache/logging/log4j/core/appender/SocketAppender.html" rel="nofollow noreferrer">Socket Appender</a> of Log4j2 to write logs to a remote server. It was working fine when I was writing from a spring boot application. After I containerized this app with docker and run as a docker container, it gives the following errors. What is the proper was to write to a remote server from a docker container?</p>
  
  <p><strong>Errors</strong></p>
  
  <pre><code>kabilesh@kabilesh-Latitude-E6540:~/IdeaProjects/SpringResearch$ docker run -p 8080:8080 springappnew
  2019-06-21 10:39:50,796 main ERROR TcpSocketManager (TCP:127.0.0.1:8000) java.net.ConnectException: Connection refused (Connection refused) java.net.ConnectException: Connection refused (Connection refused)
          at java.net.PlainSocketImpl.socketConnect(Native Method)
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
          at java.net.Socket.connect(Socket.java:589)
          at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createSocket(TcpSocketManager.java:423)
          at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createManager(TcpSocketManager.java:404)
          at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createManager(TcpSocketManager.java:387)
          at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:112)
          at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:114)
          at org.apache.logging.log4j.core.net.TcpSocketManager.getSocketManager(TcpSocketManager.java:201)
          at org.apache.logging.log4j.core.appender.SocketAppender.createSocketManager(SocketAppender.java:430)
          at org.apache.logging.log4j.core.appender.SocketAppender$Builder.build(SocketAppender.java:215)
          at org.apache.logging.log4j.core.appender.SocketAppender$Builder.build(SocketAppender.java:190)
          at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
          at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:952)
          at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:892)
          at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:884)
          at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:508)
          at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:232)
          at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:244)
          at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
          at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
          at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
          at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
          at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
          at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
          at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
          at org.apache.commons.logging.LogFactory$Log4jLog.&lt;clinit&gt;(LogFactory.java:199)
          at org.apache.commons.logging.LogFactory$Log4jDelegate.createLog(LogFactory.java:166)
          at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:109)
          at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:99)
          at org.springframework.boot.SpringApplication.&lt;clinit&gt;(SpringApplication.java:189)
          at com.springResearch.SpringRoot.main(SpringRoot.java:9)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
          at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
          at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
          at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
          at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
  
  2019-06-21 10:39:51,396 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR Unable to write to stream TCP:127.0.0.1:8000 for appender socket: org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
  2019-06-21 10:39:51,398 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR An exception occurred processing Appender socket org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
          at org.apache.logging.log4j.core.net.TcpSocketManager.write(TcpSocketManager.java:213)
          at org.apache.logging.log4j.core.appender.OutputStreamManager.write(OutputStreamManager.java:201)
          at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.writeByteArrayToManager(AbstractOutputStreamAppender.java:186)
          at org.apache.logging.log4j.core.appender.SocketAppender.directEncodeEvent(SocketAppender.java:446)
          at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170)
          at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161)
          at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
          at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
          at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
          at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
          at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
          at org.apache.logging.log4j.core.async.AsyncLoggerConfig.asyncCallAppenders(AsyncLoggerConfig.java:115)
          at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:112)
          at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:98)
          at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:129)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)
  
  
    .   ____          _            __ _ _
   /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
  ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
   \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
    '  |____| .__|_| |_|_| |_\__, | / / / /
   =========|_|==============|___/=/_/_/_/
   :: Spring Boot ::        (v2.0.6.RELEASE)
  
  2019-06-21 10:39:51,616 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR Unable to write to stream TCP:127.0.0.1:8000 for appender socket: org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
  2019-06-21 10:39:51,617 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR An exception occurred processing Appender socket org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
          at org.apache.logging.log4j.core.net.TcpSocketManager.write(TcpSocketManager.java:213)
          at org.apache.logging.log4j.core.appender.OutputStreamManager.write(OutputStreamManager.java:201)
          at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.writeByteArrayToManager(AbstractOutputStreamAppender.java:186)
          at org.apache.logging.log4j.core.appender.SocketAppender.directEncodeEvent(SocketAppender.java:446)
          at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170)
          at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161)
          at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
          at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
          at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
          at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
          at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
          at org.apache.logging.log4j.core.async.AsyncLoggerConfig.asyncCallAppenders(AsyncLoggerConfig.java:115)
          at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:112)
          at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:98)
          at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:129)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)
  
  2019-06-21 10:39:53.884  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
  2019-06-21 10:39:53.886  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
  2019-06-21 10:39:53.905  INFO 1 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
  2019-06-21 10:39:54.013  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
  </code></pre>
  
  <p><strong>In Client app</strong></p>
  
  <blockquote>
    <p>log4j2.xml (log4j2 configurations)</p>
  </blockquote>
  
  <pre><code>&lt;Configuration status="warn" name="SpringResearch" packages="com.springResearch"&gt;
      &lt;Appenders&gt;
          &lt;Socket name="socket" host="127.0.0.1" port="8000"&gt;
              &lt;SerializedLayout /&gt;
          &lt;/Socket&gt;
      &lt;/Appenders&gt;
  
      &lt;Loggers&gt;
          &lt;AsyncRoot level="info"&gt;
              &lt;AppenderRef ref="socket"/&gt;
          &lt;/AsyncRoot&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p><strong>Dockerfile</strong></p>
  
  <pre><code>FROM java:8
  EXPOSE 8080
  ADD /target/SpringResearch-1.0-SNAPSHOT.jar SpringResearch-1.0-SNAPSHOT.jar
  ENTRYPOINT ["java", "-jar", "SpringResearch-1.0-SNAPSHOT.jar"]
  </code></pre>
  
  <p><strong>I run the docker container as</strong></p>
  
  <pre><code>docker run -p 8080:8080 springappnew
  </code></pre>
  
  <p>I use this - <a href="https://github.com/piruin/log4j2-socket-server" rel="nofollow noreferrer">https://github.com/piruin/log4j2-socket-server</a> as the app that listens to the socket appender. This is listening on localhost : port 8000</p>
  
  <p><strong>In log server app</strong></p>
  
  <p><strong>log4j2.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Configuration status="warn" name="SocketServerLog" monitorInterval="30"&gt;
    &lt;Appenders&gt;
      &lt;RollingRandomAccessFile name="RollingRandomAccessFile" fileName="logs/info.log"
                   filePattern="logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd-HH}-%i.log.gz"&gt;
        &lt;PatternLayout&gt;
          &lt;Pattern&gt;%d %p %c{1.} [%t] %m%n&lt;/Pattern&gt;
        &lt;/PatternLayout&gt;
        &lt;Policies&gt;
          &lt;TimeBasedTriggeringPolicy interval="6" modulate="true"/&gt;
          &lt;SizeBasedTriggeringPolicy size="25 MB"/&gt;
        &lt;/Policies&gt;
      &lt;/RollingRandomAccessFile&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
      &lt;Root level="info"&gt;
        &lt;AppenderRef ref="RollingRandomAccessFile"/&gt;
      &lt;/Root&gt;
    &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>