Cannot access nginx container through external IP of Google Cloud VM

Viewed 1386

I have a google cloud vm running an nginx docker container on port 8080. The nginx page is accessible from within the machine when I use curl http://localhost:8080. However, when I open the External IP of the machine (using port 8080), I get the ERR_CONNECTION_REFUSED in chrome. I had enabled the http/https firewall options while creating the VM.

Here is the docker run command that I used and the curl check on localhost:

aayush_pathak15@instance-1:~/devops-environment$ sudo docker run -d -p 8080:80 nginx
f398231940eeb7194e0b4deb50936d7581cf32a451583e162cac40e829f9ebff
aayush_pathak15@instance-1:~/devops-environment$ curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
aayush_pathak15@instance-1:~/devops-environment$ 

Here is a list of all ports on my VM (and docker listening to port 8080)

aayush_pathak15@instance-1:~/devops-environment$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      583/sshd            
tcp6       0      0 :::8080                 :::*                    LISTEN      8936/docker-proxy   
tcp6       0      0 :::22                   :::*                    LISTEN      583/sshd            
udp        0      0 0.0.0.0:68              0.0.0.0:*                           310/dhclient        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           402/chronyd         
udp6       0      0 ::1:323                 :::*                                402/chronyd  

I tried adding another firewall rule to allow all ports (please ignore the unrelated naming that says "jenkins") enter image description here

Below are my machine details

enter image description here

Could this have anything to do with the "IP Forwarding" being off? (See above image)

2 Answers

According to the Docker docs The -p 8080:80 argument in your docker run statement is telling it to publish port 8080 on the host to port 80 on the container, so in this case you would access it externally by visiting the node's external IP on port 8080. 34.68.171.6:8080

I ran my container using

sudo docker run -d -p [Internal IP]:8080:80 nginx

and I could access the nginx page on http://[External IP]:8080

Also it was necessary to add the firewall rule mentioned in the question.

Related