Dan
Dan

Reputation: 2098

Container getting connection refused

I am having an issue with my docker containers, where one of them is a REST API server and the other is just a test container to curl the former one.

I have created a custom docker network with a specific IP address range and both containers are connected to it.

In the Dockerfile(REST API Server image) I have exposed the port 8090 and also mapped it to the host port 8090 with the -p flag when running a container. I did check on the REST container if the port was exposed by netstat -a but it wasn't. I am not sure what the issue is, as I can't see what I am doing wrong!

Dockerfile for REST API Server

FROM openjdk:8-alpine
WORKDIR /
EXPOSE 8090
COPY target/blockchain-0.0.1-SNAPSHOT.jar blockchain.jar
CMD java -jar blockchain.jar

REST Container Creation

docker container run --rm -p 8090:8090 --ip 172.18.0.3 --net multichain-network --name multichain-app bc59

Test Container to run Curl and Response

docker container run --rm -it --net multichain-network --ip 172.18.0.4 byrnedo/alpine-curl http://172.18.0.3:8090/api/blockchain/address/create
curl: (7) Failed to connect to 172.18.0.3 port 8090: Connection refused

Netstat -a Response

$ docker exec multichain-app netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:http-alt        0.0.0.0:*               LISTEN      
tcp        0      0 localhost:41183         0.0.0.0:*               LISTEN      
udp    34560      0 localhost:34462         0.0.0.0:*                           
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         STREAM     CONNECTED     456199

I don't understand why port 8090 is not showing up in the netstat command as I have it defined in the Dockerfile and also specify the -p flag. Can someone shed any light on this for me?

Upvotes: 1

Views: 4054

Answers (2)

Light.G
Light.G

Reputation: 7304

First of all, EXPOSE does not change the port which application(in a container) listening on, nor bind port to host. So the question below comes the most important.

Which port your REST API server is listening on? This is the most important thing we should know.

EXPOSE 8090 only is kind of documentation between people who build image and people who run container from a image, which means that only -p flag could map 8090 on host to 8090 in container.

-p 8090:8090 is the right instruction which could do the port mapping with or without EXPOSE. It could somehow(I could not make it more detailed since my own limit) present a process on host by docker-proxy. Just like below:

/usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8090 -container-ip x.x.x.x -container-port 8090

So above all, you have to confirm your REST API server’s listening port, and then add -p 8090:your-rest-api-server-port to docker run command. Now you may have 8090 working on your host.

Hope this helps~

Upvotes: 1

Ricardo Branco
Ricardo Branco

Reputation: 6079

You must map the 8090 port on the host to the 8080 port on the container like this:

docker container run --rm -p 8090:8080 --ip 172.18.0.3 --net multichain-network --name multichain-app bc59

The difference is here: -p 8090:8080

From the Docker documentation:

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

Upvotes: 1

Related Questions