0

An error occure while running a dockerfile non-root in compose. No issue in the dockerfile while runing CLI. here is the docker-compose.yaml

version: "3.9"
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5000:3000"
    networks:
      - my-networks      
    volumes:
      - backend-logs:/application-logs/
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/api/health_check"]
      interval: 60s
      timeout: 10s
      retries: 3
      start_period: 40s  
networks:
  my-networks:      
volumes:
  backend-logs:

Dockerfile

FROM node:10.18.1-alpine3.9

ENV PROJECT_DIR=backend

ENV USER_NAME=node

ENV APP_PORT=5000

RUN apk run update \
    && npm config set unsafe-perm true \
    && apk add --no-cache git \
    && apk add curl \
    && npm install -g ts-node@7.0.1 \
    && npm install -g typescript@4.2.4

RUN mkdir -p /home/$USER_NAME/$PROJECT_DIR/node_modules && chown -R $USER_NAME:$USER_NAME /home/$USER_NAME/$PROJECT_DIR \
    && mkdir /application-logs
RUN chown -R $USER_NAME:$USER_NAME /application-logs
RUN chmod -R 777 /application-logs
RUN chown -R $USER_NAME:$USER_NAME /usr/local/lib/node_modules && chown -R $USER_NAME:$USER_NAME /usr/local/bin

WORKDIR /home/$USER_NAME/$PROJECT_DIR

COPY package.json .

USER node

RUN npm install

COPY . .

EXPOSE ${APP_PORT}
VOLUME /application-logs

CMD ["sh", "deploy.sh"]

I have attched non-root dockerfile. while calling

docker run -it backend

works fine. while using with docker compose up issue triggers `

backend-1  | deploy.sh: line 4: can't create /application-logs/access.log: Permission denied
backend-1  | deploy.sh: line 5: can't create /application-logs/access.log: Permission denied
backend-1  | deploy.sh: line 6: can't create /application-logs/access.log: Permission denied
backend-1  | deploy.sh: line 7: can't create /application-logs/access.log: Permission denied
backend-1  | deploy.sh: line 8: can't create /application-logs/access.log: Permission denied
backend-1  | deploy.sh: line 9: can't create /application-logs/access.log: Permission denied
backend-1  | ========================================
backend-1  | Tue Apr 25 08:59:10 UTC 2023
backend-1  | ========================================
backend-1  | 
backend-1  | Listening at http://localhost:3000/
backend-1  | GET /api/health_check 200 5.761 ms - 15
backend-1  | GET /favicon.ico 404 5.036 ms - 150
backend-1  | GET /api/health_check 304 4.721 ms - -
backend-1  | GET / 404 0.548 ms - 139

Given access to /application-logs/ file in dockerfile

`    && mkdir /application-logs
RUN chown -R $USER_NAME:$USER_NAME /application-logs
RUN chmod -R 777 /application-logs`

While Removing the Volume from docker-compose.yaml it works fine. Attaching the Volume seems an issue

  • The volume mount hides every characteristic of the image directory; if a volume is going to be mounted, then you can't specify a user owner or (incredibly insecure) permissions in the Dockerfile. You need to make sure the container is running with the (numeric) user ID that owns the host files, as in [Docker-compose set user and group on mounted volume](https://stackoverflow.com/questions/40462189/docker-compose-set-user-and-group-on-mounted-volume) (and maybe more specifically [this answer](/a/56904335)). – David Maze Apr 28 '23 at 10:33

0 Answers0