Unable to connect to nx app running inside docker container

703 Views Asked by At

I am unable to open the app from browser. Running the following command inside docker:

nx serve --port 80

Docker is started with following command:

docker run -p 80:80 -d <my_nx_app_image>

I am able to get the index.html from terminal (inside running docker container)

wget http://localhost:80

2

There are 2 best solutions below

0
On BEST ANSWER

Steps:

  • Open project.json file of your nx app.
  • Find the key "serve" and search for parameter "configuration" under it.
  • Add a param with name "hostname" with value 0.0.0.0

Example:

"production": {
      "buildTarget": "<your_nx_app>:build:production",
      "dev": false,
      "hostname": "0.0.0.0"
    }

Note: By default nx daemon does not allow remote connections on ports below 1024 by default.

0
On

Okay, here we are. I finally found a solution that helped me. Thanks to manojadams and Docker-compose, Nx, Angular error "localhost didn’t send any data.". It helped me to dig in the right way

FROM node:16.15.0-alpine3.15

WORKDIR /app

COPY package*.json ./
RUN [ "npm", "install" ]
COPY . .

EXPOSE 4200

CMD [ "npx", "nx", "serve", "vaccine", "--host", "0.0.0.0"]

And related docker commands:

docker build -t my_app:dev .
docker run -d --rm --name my_app-1 -p 4200:4200 my_app:dev