I have a Flask Python application which creates an application.log file in the /opt/docker/logs folder. In my Dockerfile, I already do a chown -R but I still see that error.
Dockerfile:
FROM nexus.company.com/docker-private/company-base:1.4.0
LABEL team="Team"
LABEL maintainer=team@company.com
USER root
# Create directory for logs - kubernetes logging sidecar reads logs from this location
RUN mkdir -p /opt/docker/logs
# Grant write permission
RUN chown -R daemon:daemon /opt/docker
# Install required packages tools
RUN apt-get update -y && apt-get install -y python3-pip \
# wget and zlib1g-dev to help with python3.6 installation
&& apt-get install -y wget && apt-get install -y zlib1g-dev \
# The following line installs the ssl module required by pip3 to install requirements
&& apt-get install -y libssl-dev && apt-get install liblzma-dev \
# The following line installs the bz2 module required for pandas to install correctly
&& apt-get install -y libbz2-dev
WORKDIR /opt
# Download and install Python 3.6
RUN wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz && tar -xvf Python-3.6.3.tgz
RUN cd Python-3.6.3 && ./configure && make && make install
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install --upgrade pip && pip3 install -r requirements.txt
# Copy all files to /app folder - we will run our application from here
COPY . /app
# Permission for local application.log if running locally
RUN chown -R daemon:daemon /app
USER daemon
# Run flask app with uwsgi
ENTRYPOINT uwsgi --wsgi-file src/app.py --http-socket :9000 --callable app --ini app.ini
Code:
def setup_logging():
rotating_log_handler = RotatingFileHandler(app.container.config.get("app.LOG_FILE"), maxBytes=10000, backupCount=1) # app.LOG_FILE value is /opt/docker/logs/application.log
rotating_log_handler.addFilter(RequestIDLogFilter()) # << Add request id contextual filter
logging.getLogger().addHandler(rotating_log_handler)
logging.getLogger().setLevel(level="DEBUG")
Error Stacktrace:
File "./src/__init__.py", line 28, in create_app
setup_logging(app)
File "./src/__init__.py", line 54, in setup_logging
rotating_log_handler = RotatingFileHandler(app.container.config.get("app.LOG_FILE"), maxBytes=10000, backupCount=1)
File "/usr/local/lib/python3.6/logging/handlers.py", line 150, in __init__
BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
File "/usr/local/lib/python3.6/logging/handlers.py", line 57, in __init__
logging.FileHandler.__init__(self, filename, mode, encoding, delay)
File "/usr/local/lib/python3.6/logging/__init__.py", line 1030, in __init__
StreamHandler.__init__(self, self._open())
File "/usr/local/lib/python3.6/logging/__init__.py", line 1059, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: '/opt/docker/logs/application.log'
chown is run before everything else, immediately after the directory was created - what could be the issue? The base image is built on top of ubuntu 16.04 if I'm not mistaken.