Kris
Kris

Reputation: 199

curl: (7) Failed to connect to my-boot-system port 8079: Connection refused

I tried to come into the nginx container to curl the url:http://my-boot-system:8079, but the error occured as the title.

In the nginx Dockerfile, I have:

FROM nginx
VOLUME /tmp
ENV LANG en_US.UTF-8
RUN echo "server {  \
                      listen       80; \
                      location ^~ /my-boot { \
                      proxy_pass              http://my-boot-system:8079/my-boot/; \
                      proxy_set_header        Host my-boot-system; \
                      proxy_set_header        X-Real-IP \$remote_addr; \
                      proxy_set_header        X-Forwarded-For \$proxy_add_x_forwarded_for; \
                  } \
                  location / { \
                     root   /var/www/html/; \
                      index  index.html index.htm; \
                      if (!-e \$request_filename) { \
                          rewrite ^(.*)\$ /index.html?s=\$1 last; \
                          break; \
                      } \
                  } \
                  access_log  /var/log/nginx/access.log ; \
              } " > /etc/nginx/conf.d/default.conf \
    &&  mkdir  -p  /var/www \
    &&  mkdir -p /var/www/html

ADD dist/ /var/www/html/
EXPOSE 80

It seems the nginx container couldn't find the network ?? But, In the docker-compose, I have:

version: '2.4'
services:

  my-iot-survey-web:
    build:
      context: .
    restart: always
    container_name: my-iot-survey-web
    image: my-iot-survey-web
    ports:
      - 7070:80
    networks:
      - my-iot-surver-api_default

networks:
  my-iot-surver-api_default:
    external: true

I have already had a network named my-iot-surver-api_default which shows in the 'docker network ls' command. and the network my-iot-surver-api_default is also present in the docker-compose of my-boot-system definition

version: '2.4'
services:
  my-boot-redis:
    image: redis:5.0
    ports:
      - 6378:6379
    restart: always
    container_name: my-boot-redis

  my-boot-system:
    build:
      context: ./my-boot-module-system
    restart: always
    container_name: my-boot-system
    image: my-boot-system
    ports:
      - 8079:8080
    networks:
      - my-iot-surver-api_default
networks:
  my-iot-surver-api_default:
    external: true

Following is the network inspection of docker:

[
    {
        "Name": "my-iot-surver-api_default",
        "Id": "aaeda9e6419a1d603e6c3de6364025ef7c3ea034de57ba3a63c00b608f844d5f",
        "Created": "2021-03-19T14:39:56.432760565+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "3c59d1d44ea2d08a3c6a68fb16539db0830d535ce585d128d10e90b57c1f5642": {
                "Name": "my-boot-redis",
                "EndpointID": "30b21c59c1da1b031f8ce2b85c7fc8c62f03b7623fb69ee205af8dfa5a95d61a",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "9fd99e54d2bc7ad51dc539586f8a49a295f9ea0ba2bd1c9555d864d351d4d4be": {
                "Name": "my-iot-survey-web",
                "EndpointID": "dc8fabe7113742476c00f0eee98f40c99772835e1e3fe9b41f5cef8cda9824ae",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            },
            "fd999975f007537c4449b65cf5f2cb86b1d5b40655a7c324c2cc9f35bf4632f5": {
                "Name": "my-boot-system",
                "EndpointID": "ede496dc1ff7f73d45bff969a15dd5e3a05d82979cbfa7f3226360c24b4369f4",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

Upvotes: 0

Views: 708

Answers (1)

David Maze
David Maze

Reputation: 158995

Connections between containers on the same Docker network ignore ports:. You always need to make the connection to the port the service inside the container is listening on; if you do have ports:, the port number for inter-container connections need to match the second port number. (If the service doesn't need to be reached from outside Docker it's also valid to leave off ports: entirely.)

In this particular setup, you can also notice you're getting a "connection refused" error. If you get that error (and not a "no such host" error), Nginx has successfully looked up the host name it's been given, which implies the Docker-level setup is correct.

This means you can change your Nginx configuration to:

proxy_pass              http://my-boot-system:8080/my-boot/;
#     Not remapped 8079 but the standard port ^^^^

(I'd consider some other cleanups in the Dockerfile. COPY the configuration file in instead of trying to RUN a long-winded escape-prone shell command to create it inline. Don't declare a VOLUME; it mostly only has confusing side effects and doesn't bring any benefits. Prefer COPY to ADD in most cases. The Docker Hub nginx base image also already includes EXPOSE and a content directory, so use its /usr/share/nginx/html instead of /var/www/html. That would reduce the Dockerfile to the FROM line and two COPY lines to add in the configuration and content.)

Upvotes: 1

Related Questions