wiki:write_custom_host_path_docker_container
Table of Contents
Write to custom host path from Docker container
Say you have an app that generates some reports and its running in its own container. You want to serve the reports from this container via nginx on host machine.
In your Dockerfile create reports dir and user/group, example of Dockerfile utilizing supercronic to run some cronjob.
FROM python:3.10-alpine3.16 RUN apk add --no-cache libffi-dev build-base cmake openssl-dev curl tzdata gnupg unixodbc-dev RUN apk add --no-cache libffi-dev build-base cmake openssl-dev curl tzdata gnupg unixodbc-dev ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.2.1/supercronic-linux-amd64 \ SUPERCRONIC=supercronic-linux-amd64 \ SUPERCRONIC_SHA1SUM=d7f4c0886eb85249ad05ed592902fa6865bb9d70 RUN curl -fsSLO "$SUPERCRONIC_URL" \ && echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \ && chmod +x "$SUPERCRONIC" \ && mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \ && ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic RUN addgroup --gid 2222 --system user_app && adduser --disabled-password --uid 2222 --system user_app --ingroup user_app WORKDIR /app RUN mkdir -p /app/exports RUN mkdir -p /app/exports/public RUN chown -R user_app:user_app /app/exports COPY . . USER user_app CMD ["supercronic", "/app/python-docker-crontab"]
This user should exist on the host machine with some UID e.g.:
useradd --no-create-home --uid 2222 user_app
In nginx setup a location on the host to serve e.g.
location /reports/ { autoindex on; autoindex_localtime on; alias /var/www/app_reports/public/; }
The host path /var/www/app_reports/public/ needs to be mounted in container, similar to a volume, and needs to have the owner set to user_app
so that the containerized app can write to it and nginx can serve it. Permissions can be 0755
, so owner (user_app) can read/write/execute.
useradd --no-create-home --uid 2222 user_app chown -R user_app:user_app /var/www/app_reports
Build and run the docker container:
docker build -t python-reports . docker run --name reports -d -v /var/www/app_reports/:/app/exports/ python-reports
Tested on
- Debian 11
- Docker 20.10.21
See also
References
wiki/write_custom_host_path_docker_container.txt · Last modified: 2023/03/09 11:15 by antisa