简体   繁体   中英

Docker: run script : ERROR: Unable to lock database: Permission denied

Hello I'm trying to run a script to just start my yarn dev after my postgres is connected:

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 ${@}

docker file:

    #building code
    FROM node:lts-alpine

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

    WORKDIR /home/node/api

    COPY ormconfig.json .env package.json yarn.* ./

    USER node

    RUN yarn

    COPY --chown=node:node . .

    RUN apk add --no-cache openssl

    COPY wait-pg.sh ./
    RUN chmod +x /wait-pg.sh
    ENTRYPOINT ["/wait-pg.sh"]

    EXPOSE 4000

    CMD ["yarn", "dev"]

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
    command: ['./wait-pg.sh', 'yarn', 'dev']
    logging:
      driver: 'json-file'
      options:
        max-size: '10m'
        max-file: '5'

volumes:
  ci-postgres-data:

and get this error:

---> Running in c5add5098b70 ERROR: Unable to lock database: Permission denied ERROR: Failed to open apk database: Permission denied ERROR: Service 'ci-api' failed to build: The command '/bin/sh -c apk add --no-cache openssl' returned a non-zero code: 99

You are getting the error because the node user you are trying to use does not have permissions to run the command. Move the user definition to after the commands, something like this:

  #building code
    FROM node:lts-alpine

    RUN mkdir -p /home/node/api && chown -R node:node /home/node/api
    WORKDIR /home/node/api
    COPY ormconfig.json .env package.json yarn.* ./

    RUN yarn
    COPY --chown=node:node . .
    RUN apk add --no-cache openssl
    COPY wait-pg.sh .
    RUN chmod +x ./wait-pg.sh

    USER node



    ENTRYPOINT ["./wait-pg.sh"]
    EXPOSE 4000
    CMD ["yarn", "dev"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM