0

My server http://127.0.0.1:5438/api provides the api.

The nginx configuration works fine if I'm not using the docker.

server {
   listen       80;
    server_name  127.0.0.1;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    location ^~/api/ { proxy_pass http://127.0.0.1:5438/api/; }
 }

If I'm using docker, the nginx configuration is not working.

sudo docker run \
  -d -p 80:80 \
  -v /usr/share/nginx/html:/usr/share/nginx/html \
  -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /usr/share/nginx/html/nginx.conf:/etc/nginx/sites-enabled/nginx.conf \
  nginx

So how to access host's 5438 port in docker nginx?

user6456568
  • 579
  • 9
  • 23
  • Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze Sep 03 '18 at 10:28
  • Also remember that [localhost means something different to the host and the container](https://stackoverflow.com/questions/50278632/what-does-localhost-means-inside-a-docker-container/51680331#51680331). – David Maze Sep 03 '18 at 10:44

2 Answers2

0

If you are not care network toplogic, try add --net=host, but the limit is host should not have service use 80 port.

sudo docker run \
  --net=host \
  -d \
  -v /usr/share/nginx/html:/usr/share/nginx/html \
  -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /usr/share/nginx/html/nginx.conf:/etc/nginx/sites-enabled/nginx.conf \
  nginx
atline
  • 28,355
  • 16
  • 77
  • 113
-2

Modify "-p 80:80 " to "-p 5438:80" in docker run command, This connects 80 port of docker to 5438 port of Host Machine.

sudo docker run \
-d -p 5438:80 \
-v /usr/share/nginx/html:/usr/share/nginx/html \
-v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /usr/share/nginx/html/nginx.conf:/etc/nginx/sites-enabled/nginx.conf \
nginx

Hope It works !!!

Prem
  • 1,188
  • 7
  • 13
  • `Listen tcp 0.0.0.0:5438: bind: address already in use` Port 5438 is already running to provide api. – user6456568 Sep 03 '18 at 06:55
  • If your nginx server is running already in the host with port 5438, then you cannot bind the port . You need to stop the nginx server which is already running or try with other port for ex : 5439 to test and check whether it works. – Prem Sep 03 '18 at 07:27
  • 127.0.0.1:5438 is a node js express backend which is not using nginx. For example 127.0.0.1:5438/api/user/1 will response the info of user 1. I want to the nginx in docker which listens port 80 to access 127.0.0.1:5438. – user6456568 Sep 03 '18 at 07:32
  • As per my understanding 127.0.0.1:5438 is already used by nodejs in the host where your docker containers are running. If this is correct then you cannot bind the same port to docker nginx container, because it is already used nodejs app. Either change the nodejs listening port or docker nginx port. – Prem Sep 03 '18 at 07:39
  • 1
    The question is trying to make an outbound call to a server already running on port 5438 on the host. The `docker run -p` option will only affect inbound calls into the container. – David Maze Sep 03 '18 at 10:39