0

I am working on a MEAN STACK application which is deployed using docker. The docker-compose.yml file contains two services one is for Angular and express and another for mongoDB.

Inside docker-compose.yml file :

version: '2' # specify docker-compose version

# Define the services/containers to be run
services:
  webapp: #name of the second service
    build: ./ # specify the directory of the Dockerfile
    volumes : 
          - ./dockerScripts:/temp/scripts
    entrypoint:
          -  /temp/scripts/wait-for-service.sh
          - 'localhost'
          - '27018'
          - 'npm run serve' 
    ports:
      - "3000:3000" #specify ports forewarding

  appdb: # name of the third service
    image: mongo # specify image to build container from
    command: mongod --port 27018
    expose:
      - 27018
    ports:
      - "27018:27018" # specify port forewarding

Below is the dockerFile content :

FROM mhart/alpine-node:10

MAINTAINER https://hub.docker.com/u/mhart/

RUN apk update && \
    apk add git && \
    apk add --no-cache python build-base && \
    apk add busybox-extras && \
    apk --no-cache add procps

#RUN apk add --no-cache python build-base

RUN mkdir -p /usr/src/

WORKDIR /usr/src/

COPY package.json .

RUN npm install --production

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

Before executing the command for webapp , I need to check if the mongodb is started on the specified port.

To achieve that I wrote a shell script file as below :

set -e

host="$1"
port="$2"
svc="$3"

echo `Inspectingggggg ` $host $port 
until `telnet $host $port`; do
# until `ps -ax`; do
  >&2 echo "Service is unavailable - going to sleeping "
  sleep 5
done

>&2 echo "Service is now up, will execute npm run " $svc

npm run $svc

**

The problem I am facing is how to check if mongodb service is started before executing the webapp service ?

**

With telnet command I am always getting the below error :

telnet: can't connect to remote host (127.0.0.1): Connection refused
templatexapp_1  | Service is unavailable - going to sleeping 
programoholic
  • 4,830
  • 5
  • 20
  • 59

1 Answers1

0

Define your inter-dependencies in depends_on that starts services in dependency order. This eliminates a shell script to check if mongo is running or not. So you can simply have this docker-compose.yml file:

version: '2' # specify docker-compose version

# Define the services/containers to be run
services:
  webapp: #name of the second service
    build: ./ # specify the directory of the Dockerfile
    volumes : 
          - ./dockerScripts:/temp/scripts
    depends_on:
          - appdb
    command: npm run serve
    ports:
      - "3000:3000" #specify ports forewarding

  appdb: # name of the third service
    image: mongo # specify image to build container from
    command: mongod --port 27018
    expose:
      - 27018
    ports:
      - "27018:27018" # specify port forewarding

Take note of this section in webapp service:

    depends_on:
          - appdb

This ensures that appdb will start before webapp.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Will it also ensure that the port is open before `webapp` starts? Ie, not just running, but ready? – erik258 Nov 15 '19 at 22:14