Home > Net >  --network=host works in docker build but not in docker-compose
--network=host works in docker build but not in docker-compose

Time:01-27

I have a node.js application and in Ubuntu once it runs npm install it is giving timeout error like below.

Docker build npm install error network timeout

Solution is adding --network=host

docker build -t cassiamani/nodeapp --network=host .

But I have a docker-compose.yaml file like below;

version: '3.8'

services:
  nodejs-server:    
    network_mode: "host"
    build:
      context: ./api
    ports:
      - "8000:8000"
    container_name: node-api
    volumes:
       - ./api:/usr/src/app/api
       - /usr/src/app/api/node_modules
  react-ui:
    network_mode: "host"
    build:
      context: ./web/web-app
    ports:
      - "3000:3000"
    container_name: react-ui
    stdin_open: true
    volumes:
       - ./web/web-app:/usr/src/app/my-app
       - /usr/src/app/my-app/node_modules

And it still stucks on npm install command, and adding network_mode: "host" did not work. Am I missing something here?

CodePudding user response:

The way you've done it specifies the network settings at run-time. To specify them at build time, you need to have network: under the build: section, like this

build:
  context: ./api
  network: host
  • Related