unable to launch localhost:3000 on mac with docker set up unable to launch localhost:3000 on mac with docker set up docker docker

unable to launch localhost:3000 on mac with docker set up


You might be missing to define the command of web service on the docker-compose.yml:

...entrypoint: /bin/bash tty: true command: "npm start"...

Have in mind that, even tough web will be started when db is up, is not guaranteed that the database is ready when you start your app.docker-cmpose docs suggests to use
wait-for-it, dockerize, or sh-compatible wait-for to control startup order.

You could also define a HEALTHCHECK (or Dockerfile healthcheck) to check whether the database is available or not.


Looking over your compose file again, you need to ensure your entry node script is calling the bootup function, startServer -- you define it but don't call it. You should also ensure that the web service is invoking your Nodejs script. For example:

version: '3.0'services:  web:    image: node:boron    ports:      - "3000:3000"    volumes:      - .:/myapp    environment:      NODE_ENV: 'dev'      DATABASE_URL: postgres://postgres:xxxx@db:5432/myapp    depends_on:      - db     entrypoint: ["/bin/bash"]    command: ["node", "dist/index.js"]  db:    image: postgres:9.6.3    environment:      POSTGRES_USER: "postgres"      POSTGRES_PASSWORD: "xxxxx"

As you are using Typescript, you'll need to build your JS, which I assume will be in a dist folder. Also, if your app needs to wait for the db to be up and running, you can use the example wait-for-postgres.sh script discussed here: https://docs.docker.com/compose/startup-order/; then update your entrypoint accordingly.

As for running multiple nodejs apps on the same host, you can do this as long as they each bind to a separate port on the host. So you can duplicate your web service and update the app entrypoint (dist/index.js above) and the Docker host port (i.e. your computer -- the internal node app can still listen on 3000). For example,

version: '3.0'services:  web1:    image: node:boron    ports:      - "3000:3000"    volumes:      - .:/myapp    environment:      NODE_ENV: 'dev'      DATABASE_URL: postgres://postgres:xxxx@db:5432/myapp    depends_on:      - db     entrypoint: ["/bin/bash"]    command: ["node", "dist/index1.js"]  web2:    image: node:boron    ports:      - "3001:3000"    volumes:      - .:/myapp    environment:      NODE_ENV: 'dev'      DATABASE_URL: postgres://postgres:xxxx@db:5432/myapp    depends_on:      - db     entrypoint: ["/bin/bash"]    command: ["node", "dist/Index2.js"]  db:    image: postgres:9.6.3    environment:      POSTGRES_USER: "postgres"      POSTGRES_PASSWORD: "xxxxx"

Although, if you need multiple instances of the same web service for scaling or load management, then you should look into horizontal container scaling solution, such as Docker Swarm or Kubernetes.

Hope that helps.