简体   繁体   中英

Docker service is accessible from EC2 Instance (localhost) but not remotely

I have a docker container running a .NET WebAPI. I have configured the container to running using the.network mode "host". The reason for this is that I have a MySQL Database running on the same EC2 Instance, but not containerized:

services:
  api:
    image: <<>>
    restart: always
    network_mode: host

I am able to access the service with wget:

Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK

However I am unable to access the same when attempting to connect using my EC2 Instance public endpoint. I have made sure that the EC2 Instance security group is allowing HTTP/HTTPS traffic:

在此处输入图像描述

Any suggestions on how to resolve this?

You need to check your service is running on 127.0.0.1 or 0.0.0.0 . 127.0.0.1 is loopback ip address. You can not access 127.0.0.1 from external. EC2 instances has more than one ip addresses.

For more details check here

I resolved this by adding an iptables rule to allow traffic on port 80. I've never had to do this but there you go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM