I have the following docker container and I am trying to connect to it from Android Studio emulated phone. On the phone logcat I get the error:
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 1234) from /:: (port 49626): connect failed: ECONNREFUSED (Connection refused)
This is the docker container info:
{
"Id": "c23d4bb8709ef13aaa558821ac4cf5aacd1cbff3e0b8a8b1c85a36fa7cf4ae66",
"Created": "2024-06-04T18:32:22.478302527Z",
"Path": "/__cacert_entrypoint.sh",
"Args": [
"java",
"-jar",
"FCMS.jar"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 20822,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-06-04T18:32:23.013000218Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:7fdc162db088d15b898bb14b89d2117752281368e2d7fdbe15ec355e88d9b392",
"ResolvConfPath": "/var/lib/docker/containers/c23d4bb8709ef13aaa558821ac4cf5aacd1cbff3e0b8a8b1c85a36fa7cf4ae66/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c23d4bb8709ef13aaa558821ac4cf5aacd1cbff3e0b8a8b1c85a36fa7cf4ae66/hostname",
"HostsPath": "/var/lib/docker/containers/c23d4bb8709ef13aaa558821ac4cf5aacd1cbff3e0b8a8b1c85a36fa7cf4ae66/hosts",
"LogPath": "/var/lib/docker/containers/c23d4bb8709ef13aaa558821ac4cf5aacd1cbff3e0b8a8b1c85a36fa7cf4ae66/c23d4bb8709ef13aaa558821ac4cf5aacd1cbff3e0b8a8b1c85a36fa7cf4ae66-json.log",
"Name": "/fcms",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"1234/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1234"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": true,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
0,
0
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": [],
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware",
"/sys/devices/virtual/powercap"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/ebc9292164c7208cb422cbad2f6f5317a852153ebaf07c95d0fded94cd25a99b-init/diff:/var/lib/docker/overlay2/r7qce143p01wl85fgqb12r7b9/diff:/var/lib/docker/overlay2/tcx71kyqp9oeuejufqbdhxbvb/diff:/var/lib/docker/overlay2/7be6s2pfdp2hb1rk0htdeatg3/diff:/var/lib/docker/overlay2/0cjzrmhvj40ntmhv5hmce00xi/diff:/var/lib/docker/overlay2/59f1b26cc58acb6634b370f2a0287bd2b3c6fdfc578ee5db0aeb1a226daf9233/diff:/var/lib/docker/overlay2/6c8d4669466a22f164f52118b0e7b8b0e74774349bd8bb83fee7ce14cab8451f/diff:/var/lib/docker/overlay2/17a3a681b844eee48f07e897d4ad93f088132a9c6e0da24ffaba482cd280d017/diff:/var/lib/docker/overlay2/613b7915c9722c24693423fabad0ffcab66426536fcee2638fa930dc614c5e8b/diff:/var/lib/docker/overlay2/117e567bfe13aeb900fd225ff0ef0f3ca3b213c5ba6af9cce4ed4cad3b629b3c/diff",
"MergedDir": "/var/lib/docker/overlay2/ebc9292164c7208cb422cbad2f6f5317a852153ebaf07c95d0fded94cd25a99b/merged",
"UpperDir": "/var/lib/docker/overlay2/ebc9292164c7208cb422cbad2f6f5317a852153ebaf07c95d0fded94cd25a99b/diff",
"WorkDir": "/var/lib/docker/overlay2/ebc9292164c7208cb422cbad2f6f5317a852153ebaf07c95d0fded94cd25a99b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "fcms",
"Domainname": "",
"User": "user",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"1234/tcp": {},
"1234/udp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"JAVA_HOME=/opt/java/openjdk",
"LANG=en_US.UTF-8",
"LANGUAGE=en_US:en",
"LC_ALL=en_US.UTF-8",
"JAVA_VERSION=jdk-17.0.11+9"
],
"Cmd": [
"java",
"-jar",
"FCMS.jar"
],
"Image": "fcms",
"Volumes": null,
"WorkingDir": "/runtime",
"Entrypoint": [
"/__cacert_entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "22.04"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "544a80f6495c29a42bbd4327c72ac1e00a1be9f1700d1d3217e9e0a7373823a8",
"SandboxKey": "/var/run/docker/netns/544a80f6495c",
"Ports": {
"1234/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1234"
}
],
"1234/udp": null
},
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "703ecf9bfd7b8cfa7f0df6cdf6eb93a098e1eae4336a534aa04ed8be8d836ba1",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"MacAddress": "02:42:ac:11:00:04",
"NetworkID": "f97b1b820cbc85e8efd37a818a0c5dbaaeb9cd823bec3407371cf041b2ba35a4",
"EndpointID": "703ecf9bfd7b8cfa7f0df6cdf6eb93a098e1eae4336a534aa04ed8be8d836ba1",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": null
}
}
}
}
I have also checked netstat and it appears as listening:
Proto Local Address Foreign Address State
TCP 0.0.0.0:135 DESKTOP-670L2MO:0 LISTENING
TCP 0.0.0.0:445 DESKTOP-670L2MO:0 LISTENING
TCP 0.0.0.0:1234 DESKTOP-670L2MO:0 LISTENING
This is the Java Code of the Docker application:
serverSocket = new ServerSocket(port);
while (true) {
try {
logger.log(Level.INFO, "Waiting");
ConnectionTCP connectionTCP = new ConnectionTCP(serverSocket.accept());
new Client(connectionTCP);
logger.log(Level.INFO, "Connection Accepted");
} catch (Exception e) {
e.printStackTrace();
}
}
Android App Code:
private static String HOSTNAME = "localhost";
private static int PORT = 1234;
try {
this.socket = new Socket(HOSTNAME, PORT);
Logging.logd(this.getClass().getName(), "Creating the ConnectionTCP object");
this.reader = createReader(socket);
this.writer = createWriter(socket);
....
}
Dockerfile:
FROM maven:3.9.7-eclipse-temurin-17 AS build
COPY . ./build/
WORKDIR ./build/
RUN mvn clean package
FROM eclipse-temurin:17 as runtime
RUN useradd user
WORKDIR /runtime
RUN chown user /runtime
COPY --from=build /build/target/FCMS-jar-with-dependencies.jar ./FCMS.jar
EXPOSE 1234/TCP
EXPOSE 1234/UDP
USER user
CMD ["java","-jar","FCMS.jar"]
build and run bash:
echo "building docker image"
docker buildx build -t fcms ...
echo "running docker"
docker remove --force fcms
docker run --rm --name fcms -p 0.0.0.0:1234:1234 --hostname fcms fcms
echo _____________
echo SUCCESS
echo _____________
pause
If I am hosting it on a totally different linux machine(a Raspberry PI) it works with no issues(I am running directly the .jar
file on the Raspberry Pi). The port is 1234.
I have added the -p 1234:1234
to the run command but the same issue, I tried to ping the docker container via normal ping and I get timeout. In the Dockerfile I have also exposed the port.