Docker: cannot open port from container to host

I am using Docker for Mac . I have a container that starts the server, for example, my server runs on port 5000. I set this port to Dockerfile

When my container is running, I connect to this container and check if this server is working or not by running the command below and seeing that it returns data (a bunch of html and javascript).

 wget -d localhost:5000 

Notes, I run this container and also publish the port outside of the command:

 docker run -d -p 5000:5000 <docker_image_name> 

But on the docker host (my poppy works with El Capitan) I open chrome and go to localhost:5000 . This does not work. Just a small note, if I go to any arbitrary port, for example localhost:4000 , I see an error message from chrome, for example:

 This site can't be reached localhost refused to connect. 

But the error message for localhost:5000 :

 The localhost page isn't working localhost didn't send any data. 

So it seems that I set the work "a little", but something is wrong. Please tell me how to fix it.

+5
source share
2 answers

Please check that the program in the container is listening on the 0.0.0.0 interface.

In the container, run the command:

 ss -lntp 

If it looks like this:

 LISTEN 0 128 127.0.0.1:5000 *:* 

this means that your web application is only listening on the local host, so the container container cannot access your web application. You must force your server to listen on the 0.0.0.0 interface by changing the build configuration of the web application.

For example, if your server is a nodejs application:

 var app = connect().use(connect.static('public')).listen(5000, "0.0.0.0"); 

If your server is a web package:

  "scripts": { "dev": "webpack-dev-server --host 0.0.0.0 --port 5000 --progress" } 
+6
source

I had this problem using Docker for Mac while trying to run an Angular2 application.

I fixed the problem by changing my package.json to

  "scripts": { ... "start": "ng serve -H 0.0.0.0", # before, this was "start": "ng serve" } 
+1
source

Source: https://habr.com/ru/post/1260403/


All Articles