Docker Community Forums

Share and learn in the Docker community.

Can't acces my front application in the localhost

I have a front app who uses Angular-cli and i’am trying to check if it works proprely separetly before integrating it in the container core but it seems that i can’t get acces to the localhost.
I’am using a macOS Catalina 10.15.7 with Docker Desktop.
My Dockerfile is build like this:

FROM    node:14-alpine3.12

WORKDIR /app/front

#COPY    ./angular-cli .
COPY    package.json package-lock.json
COPY    . .       

RUN     npm install 
RUN     npm install -g @angular/cli

EXPOSE  4200
# Angular installation if it fails, 

CMD     ["ng", "serve"]

And when i run the app with the command:

docker run -d -p 80:4200 reponame
it works fine with no error at all and shows me this:

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **

:heavy_check_mark: Compiled successfully.

and when i go to Localhost:4200 it shows that the page is not accessible.
i also tried to connect with Ip adress using this commands but same thing.

docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’
If anyone can help me, would really greatfull.
Thanks !

You just forwarded your local port 80 to the container port 4200. localhost:4200 won’t work outside the container.

Using the container IP won’t work either since the containers are runing inside a virtual machine. Try localhost:80 from your host or forward port 4200 to the container instead of port 80.

Well like i said in my previous comment here, i tried to port it to 80, and i did localhost:80 but it didn’t work, same thing.
I run my docker in the 80 port.

docker run -d -p 80:4200 myreponame

I can’t find that part of your comment. I saw you forwarded port 80 but not trying to access through that port. If you did try it then it was a misunderstanding, but that should have worked

1 Like

Well i guess my bad, i’am not a English native sorry.
But yeah i tried it and nothing happens

Can you try it with curl and share the output?

curl -v localhost:80

So i run my command:
docker run -d -p 80:4200 appname
and then when i run:
curl -v localhost:80 i got this:

* Trying ::1...

* TCP_NODELAY set

* Connected to localhost (::1) port 80 (#0)

> GET / HTTP/1.1

> Host: localhost

> User-Agent: curl/7.64.1

> Accept: */*

>

* Empty reply from server

* Connection #0 to host localhost left intact

curl: (52) Empty reply from server

* Closing connection 0

iMac-de-iMac-6:Desktop Home$ curl -v localhost:80

* Trying ::1...

* TCP_NODELAY set

* Connected to localhost (::1) port 80 (#0)

> GET / HTTP/1.1

> Host: localhost

> User-Agent: curl/7.64.1

> Accept: */*

>

* Empty reply from server

* Connection #0 to host localhost left intact

curl: (52) Empty reply from server

* Closing connection 0

Have you tried forwarding a different port?

docker run -d -p 4200:4200 appname

I didn’t have any problem on Mac but I heard that port 80 didn’t work for everyone

Hi same thing, i really can’t figure out what’s the problem tbh…

I’d say that an empty reply is still a reply, so indicates that the connection was established. So, I’d not expect port mappings to be the issue?

Do you know how to get into the container using docker exec -it? I’d try some curl or wget in the container (for localhost:4200) to see what that gets you.

1 Like

Yes, except we cannot be sure the reply is from the container. At least I wasn’t sure using port 80.

Good idea. I thought I had read that @tazbiopooltech tried it.

2 Likes

Yes i did go into the container and run the commands and that’s what i got

/app/front # wget localhost:4200
Connecting to localhost:4200 (127.0.0.1:4200)
saving to 'index.html'
index.html           100% |************************************************************************|  1691  0:00:00 ETA
'index.html' saved
/app/front #

@tazbiopooltech, in case it helps, on a Mac lsof -i :port will tell what is listening. Like below 5432 is used for a container running my PostgreSQL database:

~ lsof -i :5432
COMMAND     PID       USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
com.docke 15228 avanbentem  114u  IPv6 0xfc6e512f1617cbb      0t0  TCP *:postgresql (LISTEN)

Also, I’d expect docker run -p to complain when using a port that is already in use:

docker run --rm -it -p 5432:443 busybox
docker: Error response from daemon: driver failed programming external connectivity on endpoint vigilant_banach (...): Bind for 0.0.0.0:5432 failed: port is already allocated.

Are you sure the app allows connections for non-localhost (so: is listening on 0.0.0.0 in the container?)

This may indicate it’s really only accepting connections from (the container’s) localhost?

For the curl command i get what i want but seems not able to show me that on localhost page.

/app/front # curl -v localhost:4200
*   Trying 127.0.0.1:4200...
* Connected to localhost (127.0.0.1) port 4200 (#0)
> GET / HTTP/1.1
> Host: localhost:4200
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Accept-Ranges: bytes
< Content-Type: text/html; charset=UTF-8
< Content-Length: 1691
< ETag: W/"69b-qoCvwl6dTDdmRvhmQQJi/oVXmDo"
< Date: Thu, 02 Dec 2021 12:36:07 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
<
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Biopoolservice</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!--
  <meta http-equiv="ScreenOrientation" content="autoRotate:disabled">
  <link rel="stylesheet" type="text/css" href="css/landscape.css" media="screen and (orientation: landscape)">
  <link rel="stylesheet" type="text/css" href="css/portrait.css" media="screen and (orientation: portrait)">
  -->

  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.1.0/css/flag-icon.min.css" rel="stylesheet">

  <link rel="manifest" href="manifest.webmanifest">
  <meta name="theme-color" content="#1976d2">
  <link rel="manifest" href="manifest.webmanifest">
  <meta name="theme-color" content="#1976d2">
  <link rel="manifest" href="manifest.webmanifest">
  <meta name="theme-color" content="#1976d2">
  <link rel="manifest" href="manifest.webmanifest">
  <meta name="theme-color" content="#1976d2">
  <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
<link rel="stylesheet" href="styles.css"></head>
<body>
  <app-root></app-root>
  <noscript>Please enable JavaScript to continue using this application.</noscript>
<script src="runtime.js" type="module"></script><script src="polyfills.js" type="module"></script><script src="scripts.js" defer></script><script src="vendor.js" type="module"></script><script src="main.js" type="module"></script></body>
</html>
* Connection #0 to host localhost left intact
/app/front #

$ lsof -i :4200
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 1003 Home 79u IPv6 0x2b70bd61753ec913 0t0 TCP *:4200 (LISTEN)

So, Docker it is. :+1:

In case you missed it, what about:

(I may have added that line after you already read my response.)

Well if you mean that when i try to run it in: -host 0.0.0.0

i got this output:
got this error when typing the run command in port 80. An unhandled exception occurred: getaddrinfo ENOTFOUND O.O.O.O See “/tmp/ng-aAAkli/angular-errors.log” for further details.

That looks like the letter O rather than the number 0?

Well yes i’am sure that it’s a zero.

ng serve --host 0.0.0.0
Your global Angular CLI version (13.0.4) is greater than your local version (11.2.8). The local Angular CLI version is used.

To disable this warning use "ng config -g cli.warnings.versionMismatch false".
? Port 4200 is already in use.
Would you like to use a different port? Yes
Warning: This is a simple server for use in testing or debugging Angular applications
locally. It hasn't been reviewed for security issues.

Binding this server to an open connection can result in compromising your application or
computer. Using a different host than the one passed to the "--host" flag might result in
websocket connection issues. You might need to use "--disableHostCheck" if that's the
case.
✔ Browser application bundle generation complete.

Initial Chunk Files | Names         |      Size
styles.css          | styles        |  15.71 MB
vendor.js           | vendor        |   9.12 MB
scripts.js          | scripts       | 691.12 kB
main.js             | main          | 224.87 kB
polyfills.js        | polyfills     | 128.73 kB
runtime.js          | runtime       |   6.15 kB

                    | Initial Total |  25.85 MB

Build at: 2021-12-02T15:17:52.924Z - Hash: a1fe9448a2733e019729 - Time: 41442ms

** Angular Live Development Server is listening on 0.0.0.0:35145, open your browser on http://localhost:35145/ **


✔ Compiled successfully.

And same output again nothing happens
Site note reachable