Docker Community Forums

Share and learn in the Docker community.

TCP Java Socket custom timeouts not working only on docker

This code works perfectly on my machine default O.S but not in Docker, and I didn’t found any documentation about it.

    clientSocket = new Socket();
	
	SocketAddress sockaddr = new InetSocketAddress(propertyFile.getProperty("tcp.pod.ip"),
			Integer.parseInt(propertyFile.getProperty("tcp.pod.port")));
	
	clientSocket.connect(sockaddr, 150);

	clientSocket.setTcpNoDelay(true);

The part that does not work in docker is the clientSocket connect timeout(second parameter), it takes the default timeout instead the one I am passing to him. Everything else works perfectly.

The docker image I am using is

rabbitmq:management

As sockets are from the O.S, if I can not set it from Java, can I set it in any way from the Dockerfile or docker-compose.yml?
May I need to install something as docker image is not entire linux?

docker-compose.yml:

version: '3'

services:
  middleware:
    container_name: middleware
    image: hyperloopupv:middleware
    build: './receta' 
    ports:
    - "5672:5672"
    - "15672:15672"
    - "1338:1338/udp"
    - "5556:5556/udp"
    volumes:
      - ./container-data/logs/data:/opt/mule/logs/data
      - ./container-data/logs/spacex_data:/opt/mule/logs/spacex_data
      - ./container-data/logs/alive:/opt/mule/logs/alive
      - ./container-data/apps:/opt/mule/apps

Dockerfile:

FROM rabbitmq:management

MAINTAINER victorcf999@gmail.com

EXPOSE 5672 15672 1338 5556

ADD rabbitmq.config /etc/rabbitmq/

RUN chown rabbitmq:rabbitmq /etc/rabbitmq/rabbitmq.config

# APT update, wget and sudo
RUN apt-get update -y && apt-get upgrade -y
RUN apt-get install apt-utils -y
RUN apt-get install wget -y
RUN apt-get install sudo -y
RUN apt-get install vim -y

#Install java 8
RUN apt-get update && \
	apt-get install -y openjdk-8-jdk && \
	apt-get install -y ant && \
	apt-get clean && \
	rm -rf /var/lib/apt/lists/* && \
	rm -rf /var/cache/oracle-jdk8-installer;
	
RUN apt-get update && \
	apt-get install -y ca-certificates-java && \
	apt-get clean && \
	update-ca-certificates -f && \
	rm -rf /var/lib/apt/lists/* && \
	rm -rf /var/cache/oracle-jdk8-installer;

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

# Install mule
WORKDIR /opt/
RUN wget https://repository-master.mulesoft.org/nexus/content/repositories/releases/org/mule/distributions/mule-standalone/3.8.0/mule-standalone-3.8.0.tar.gz && echo "d9279b3f0373587715613341a16483f3 mule-standalone-3.8.0.tar.gz" | md5sum -c
RUN tar xvzf mule-standalone-3.8.0.tar.gz && rm mule-standalone-3.8.0.tar.gz && mkdir mule && sudo mv /opt/mule-standalone-3.8.0/* /opt/mule/

# Define environment variables.
ENV mule.home /opt/mule/
VOLUME ["/opt/mule/logs", "/opt/mule/conf", "/opt/mule/apps", "/opt/mule/domains"]

WORKDIR  /opt/mule/apps/
#RUN echo "Downloading latest middleware" && wget  https://s3.amazonaws.com/hyperloopupv/middleware.zip
RUN sudo rm -r *
#COPY hyperloop-tcp-receiver-1.0.0-SNAPSHOT.zip middleware

WORKDIR  /opt/
COPY start.sh start.sh
RUN chmod +x start.sh
RUN sed -i -e 's/\r$//' start.sh

CMD [ "./start.sh" ]

We have been testing for months but the telemetry was just from our O.S through two access points to the prototypes, so if an ack is not received because the protopype died I will see it(We were using this to see if there were communication)

With docker there is a proxy between the O.S and the docker engine so you will see always there is connection because the acks arrives always from the container to the O.S socket.

The solution to implement a keep alive in the application layer(you can not do this in the protocol layer).

This post explain it better: https://stackoverflow.com/questions/1531207/finding-out-if-a-message-over-tcp-was-delivered