I run a docker container in order to extract files from a source folder into a destination folder. The source folder resides in my user's home directory so there is no problem to read from it or write. The destination folder on the other hand, is accessed only by a nonrootuser.
When I ran the docker container with the nonrootuser, I cannot write in the container's folders (permission denied). On the other hand when I ran the container with my user, I cannot write to the destination folder.
Setup
I build the image like this
docker build -t lftp .
based on the following Dockerfile:
Dockerfile
FROM debian:10
RUN apt-get update && apt-get -y upgrade
RUN apt-get -y install lftp dos2unix man
# Adding the scripts
COPY scripts /scripts
WORKDIR /work
# Adding the nonrootuser and his uid (`id -u nonrootuser`)
RUN useradd -u 47001 nonrootuser && mkhomedir_helper nonrootuser
Then I ran the container while binding the following volumes :
- download_folder
- destination_folder <-> this folder need to be accessed by a nonrootuser
docker run -ti --rm --name=lftp_untar -u `id -u nonrootuser`:`id -g nonrootuser` -v ${download_folder}:/source -v ${destination_folder}:/target lftp bash /scripts/execute_untar.sh /source /target
Where:
execute_untar.sh
#!/bin/bash
source=$1
target=$2
if [ ! -d $source ]; then
echo Can\'t access $source
exit 1
fi
if [ ! -d $target ]; then
echo Can\'t access $target
exit 1
fi
if [ ! -w $target ]; then
echo Can\'t write to $target
exit 1
fi
# Then Read files from /scripts and /work folder
exclude_file=$(readlink -f /scripts/exclude.txt)
log_file=$(readlink -f untar.log)