nad34
nad34

Reputation: 393

How to run two node.js apps and mysql in Docker

I have two node.js applications using the same database and running them locally works fine but I would like to run them as Services in docker containers. If I run either of the applications and the database in docker it works fine, but when I try to run both node applications I run into issues.

My docker-compose.yml file contains the following

version: '3'

services:
  db:
    build: ./db
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: mysql
      MYSQL_PASSWORD: password
      DATABASE_HOST: db
  admin:
    build: ./admin
    environment:
      DATABASE_HOST: db
      MYSQL_PORT: 3306
      MYSQL_DATABASE: mydb
      MYSQL_USER: mysql
      MYSQL_PASSWORD: password
  user:
    build: ./user
    environment:
      DATABASE_HOST: db
      MYSQL_PORT: 3306
      MYSQL_DATABASE: mydb
      MYSQL_USER: mysql
      MYSQL_PASSWORD: password

    ports:
      - "3000:3000"
      - "3001:3001"
    depends_on:
      - db
    restart: on-failure

And the dockerfile for the admin service contains this

FROM node:8

WORKDIR /usr/src/admin

COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]

And the dockerfile for the user service contains this

FROM node:8

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]

My problem is that it seems that I can only access the last service in the docker-compose.yml file in this case the user service but not the admin service.

I sometimes says that express is listening on both ports but I can only access the service user

I have tried swapping the services and then only the admin service is accessable.

What Have I done wrong and how can I fix it so that I can access both services

Thanks in advance

Upvotes: 1

Views: 1251

Answers (2)

alex067
alex067

Reputation: 3281

You're not exposing ports for your admin service, just your user service.

The way your compose currently stands, you're exposing 2 ports to your user service, but your server is only going to be listening to one of those ports.

Upvotes: 1

robrich
robrich

Reputation: 13185

In the user container you've exposed the ports:

    ports:
      - "3000:3000"
      - "3001:3001"

Add a similar section to the other containers if you'd like to also expose their ports:

  db:
    ...snip...
    ports:
      - "3306:3306"

Upvotes: 1

Related Questions