-2

Hello i'm new to docker and linux and i don't know how to run a script for my docker to run after my postgress

this is my script:

until psql -c '\l'; do
  echo >&2 "$(date +%Y%m%dt%H%M%S) Postgres is unavailable - sleeping"
  sleep 1
done
echo >&2 "$(date +%Y%m%dt%H%M%S) Postgres is up - executing command"

exec ${@}

my docker file :

FROM node:lts-alpine

RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home/node/api

WORKDIR /home/node/api

COPY wait-pg.sh ormconfig.json .env package.json yarn.* ./

USER node

RUN yarn

COPY --chown=node:node . .

EXPOSE 4000

RUN chmod +x /wait-pg.sh

CMD ["yarn", "dev"]

my docker compose:

version: '3.7'
services:
  db-pg:
    image: postgres:12
    container_name: db-pg
    ports:
      - '${DB_PORT}:5432'
    environment:
      ALLOW_EMPTY_PASSWORD: 'no'
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASS}
      POSTGRES_DB: ${DB_NAME}
    volumes:
      - ci-postgres-data:/data

  ci-api:
    build: .
    container_name: ci-api
    volumes:
      - .:/home/node/api
      - /home/node/api/node_modules
    ports:
      - '${SERVER_PORT}:${SERVER_PORT}'
    depends_on:
      - db-pg
    logging:
      driver: 'json-file'
      options:
        max-size: '10m'
        max-file: '5'

volumes:
  ci-postgres-data:

If someone can help me like I would in my docker compose and in my dockerfile. I would like to know how I can add my script to my docker file and docker compose

Felipe
  • 452
  • 2
  • 7
  • 18
  • Please consider using `healthchecks`, if you want to just check whether postgres is up or not – nischay goyal May 31 '20 at 23:58
  • I tried with healthchecks, but I was not successful, I saw that in version 3 condition in dependcy was discontinued – Felipe Jun 01 '20 at 00:00
  • can u helpme with script and healthchecks ? – Felipe Jun 01 '20 at 00:01
  • The answer in the following question can help you [Unable to connect mysql from docker container?](https://stackoverflow.com/questions/54925218/unable-to-connect-mysql-from-docker-container) – Mostafa Hussein Jun 01 '20 at 00:14
  • @MostafaHussein i try with this but I'm not just using the docker-compose so I'm in doubt – Felipe Jun 01 '20 at 00:15
  • what do you mean by not just docker-compose? the idea of the answer above is to add `wait-for` script inside your image and use it in addition to the original cmd which will make the application wait for the database before going further – Mostafa Hussein Jun 01 '20 at 00:17
  • @Mostafa Hussein can u give me one example with this code please? – Felipe Jun 01 '20 at 00:18
  • This is an example you can use https://docs.docker.com/compose/startup-order/, Just copy the script inside your docker image – Mostafa Hussein Jun 01 '20 at 00:19
  • i need to put my sh in my project folder? – Felipe Jun 01 '20 at 00:24
  • It looks like you copy the `wait-pg.sh` script into your image, but you don't actually run it; it should be part of your `CMD` (or the `ENTRYPOINT`). – David Maze Jun 01 '20 at 01:03
  • @David Maze can u reply with this? i try and not sucess. – Felipe Jun 01 '20 at 01:40

1 Answers1

0

Check the below Dockerfile

FROM node:lts-alpine

RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home/node/api

WORKDIR /home/node/api

COPY wait-pg.sh ormconfig.json .env package.json yarn.* ./

USER node

RUN yarn

COPY --chown=node:node . .

EXPOSE 4000

CMD ["yarn", "dev"]

Please try the below docker-compose.yml in which depends_on, healthcheck and links are added as web service depends on db service.

version: "2.1"
services:
  ci-api:
    build: .
    container_name: ci-api
    volumes:
      - .:/home/node/api
      - /home/node/api/node_modules
    ports:
      - '${SERVER_PORT}:${SERVER_PORT}'
    links:
      - db-pg
    depends_on:
      - db-pg
    logging:
      driver: 'json-file'
      options:
        max-size: '10m'
        max-file: '5'

  db-pg:
    image: postgres:12
    container_name: db-pg
    ports:
      - '${DB_PORT}:5432'
    environment:
      ALLOW_EMPTY_PASSWORD: 'no'
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASS}
      POSTGRES_DB: ${DB_NAME}
    volumes:
      - ci-postgres-data:/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  ci-postgres-data:
nischay goyal
  • 3,206
  • 12
  • 23