Benjamin RD
Benjamin RD

Reputation: 12034

Docker: Node server is not running after start the server

I have a Dockerfile and a docker-compose.yml file. If I execute docker-compose up, it returns:

    Creating network "demoapi_webnet" with the default driver
    Creating demoapi_web_1 ... done
    Creating d2c_postgres  ... done
    Attaching to demoapi_web_1, d2c_postgres
...
d2c_postgres | 2020-07-28 00:47:48.772 UTC [1] LOG:  database system is ready to accept connections

But my node server is not starting. These are my docker configuration files:

Dockerfile

FROM node:12.13-alpine As development
WORKDIR /app
COPY package.json .
COPY package-lock.json .
RUN npm install
COPY dist .
COPY wait-for-it.sh .
CMD ["npm", "run", "start"]

docker-compose.yml

version: '3'
services:
    db:
        image: postgres
        networks: 
            - webnet
        container_name: "d2c_postgres"
        environment:
            POSTGRES_PASSWORD: 010203
            POSTGRES_USER: postgres
            POSTGRES_DB: demo
        ports:
            - "5432:5432"
    web:
        image: nest-app
        ports: 
            - "3000:3000"
        networks:
            - webnet
        environment: 
            DB_HOST: db
        command: ["./wait-for-it.sh", "db:5432", "--", "npm", "run", "start"]
networks:
    webnet:

My only clue is this line: env: can't execute 'bash': No such file or directory

I can stablish a connection to pgadmin/postgres with that configuration, but the node server is not starting. What am I doing wrong and how can I solve it?

Upvotes: 0

Views: 275

Answers (2)

Benjamin RD
Benjamin RD

Reputation: 12034

After big research, I found a similar issue here: docker-compose: nodejs container not communicating with Postgres container

For some reason wait for it wasn't working (not sure if is a windows issue), that sh file is not mandatory to wait until database start, you can use depends_on to indicate that the server should start after a specified service:

version: '3'
services:
    db:
        image: postgres
        networks: 
            - webnet
        container_name: "node_postgres"
        environment:
            POSTGRES_PASSWORD: 010203
            POSTGRES_USER: postgres
            POSTGRES_DB: demo
        ports:
            - "5432:5432"
    web:
        image: nest-app
        depends_on:
            - db
        ports: 
            - "3000:3000"
        networks:
            - webnet
        environment: 
            DB_HOST: db
        command: ["npm", "run", "start"]
networks:
    webnet:

Upvotes: 0

Adiii
Adiii

Reputation: 59926

Wait-for-it is base on bash and it's not compatible with alpine as alpine is base on ash or sh that is why you are seeing can't execute 'bash': No such file or directory. You can look into the open issue for alpine support.

Can you make an /bin/sh version for use with alpine linux

For alpine, you can use wait-for

./wait-for is a script designed to synchronize services like docker containers. It is sh and alpine compatible.

services:
  db:
    image: postgres:9.4

  backend:
    build: backend
    command: sh -c './wait-for db:5432 -- npm start'
    depends_on:
      - db

Upvotes: 1

Related Questions