S
S
S-LoginOff2020-06-25 20:28:07
Docker
S-LoginOff, 2020-06-25 20:28:07

How to forward ports in a docker container (from internal to external)?

Good afternoon, the following problem arose, I use Docker Desktop, I launch two containers through docker-compose, I forward the port, but in the end I get a 403 error. If I go into the container and check locally, then everything works. Tell me what's the problem.

docker-compose.yml

version: "3.8"

services:
  first-node:
    &first
    container_name: first-node
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "9057:9057"
  second-node:
    <<: *first
    container_name: second-node
    ports:
      - "9077:9057"


Dockerfile:
FROM openjdk:8-alpine
WORKDIR /home/project/
COPY ./ /home/project/
CMD ["java", "-jar", "app.jar"]


If you go into the container and punch locally, everything is fine:
curl -I 127.0.0.1:9057/index/main.html
HTTP/1.1 200 OK
Date: Thu, 25 Jun 2020 17:17:47 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 0
Server: Jetty(9.3.z-SNAPSHOT)


If you break through the external IP, it gives 403:
curl -I 172.21.0.3:9057/index/main.html
HTTP/1.1 403 Forbidden
Date: Thu, 25 Jun 2020 17:19:13 GMT
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Content-Length: 330
Server: Jetty(9.3.z-SNAPSHOT)


Ports listening (internal):
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.11:39245        0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:9056            0.0.0.0:*               LISTEN      1/java
tcp        0      0 0.0.0.0:9057            0.0.0.0:*               LISTEN      1/java
udp        0      0 127.0.0.11:56731        0.0.0.0:*                           -


docker ps:
docker_second-node   "java -jar app.…"   9 minutes ago       Up 9 minutes        0.0.0.0:9077->9057/tcp   second-node     
docker_first-node    "java -jar app.…"   9 minutes ago       Up 9 minutes        0.0.0.0:9057->9057/tcp   first-node

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-06-25
@S-LoginOff

403 error - this is your application from the container responding that access is denied - what does port forwarding have to do with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question