I have the following Dockerfile
FROM python:3.9.1
ARG APP_USER=anychat
RUN groupadd -r ${APP_USER} && useradd --no-log-init -r -g ${APP_USER} ${APP_USER}
WORKDIR /app/
COPY requirements.txt /app/
RUN set -ex \
&& BUILD_DEPS=" \
build-essential \
libpcre3-dev \
libpq-dev \
vim \
" \
&& apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \
&& pip install -r requirements.txt
COPY ./src /app/
EXPOSE 8000
ENTRYPOINT ["/app/scripts/docker/entrypoint.sh"]
After running the container, I want to install the python library inside the running container
$ docker exec -it 1ab2a4b34 bash
anychat@947756b6ae96:/app pip install requests
But this gives the error
WARNING: The directory '/home/anychat/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Defaulting to user installation because normal site-packages is not writeable
Collecting requests
Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 3.0 MB/s
Collecting certifi>=2017年4月17日
Downloading certifi-2020年12月5日-py2.py3-none-any.whl (147 kB)
|████████████████████████████████| 147 kB 3.3 MB/s
Collecting chardet<5,>=3.0.2
Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
|████████████████████████████████| 178 kB 3.3 MB/s
Collecting idna<3,>=2.5
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 4.7 MB/s
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.2-py2.py3-none-any.whl (136 kB)
|████████████████████████████████| 136 kB 1.8 MB/s
Installing collected packages: urllib3, idna, chardet, certifi, requests
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/home/anychat/.local'
Check the permissions.
asked Jan 10, 2021 at 13:35
Anuj TBE
9,92235 gold badges151 silver badges311 bronze badges
1 Answer 1
The problem is that you are trying to install packages, but:
- You are not
root, sopipcan't write to the systemd-wide locations, and - Your
anychatuser has no home directory, sopipcan't write to the default user location.
There are a few ways of addressing this problem. The easiest is probably to ensure that your anychat user has a home directory. Instead of writing:
useradd --no-log-init -r -g ${APP_USER} ${APP_USER}
Use:
useradd --no-log-init -r -m -g ${APP_USER} ${APP_USER}
The -m flag asks useradd to create the corresponding home directory.
answered Jan 10, 2021 at 14:12
larsks
319k50 gold badges475 silver badges484 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
larsks
I'm really curious what the downvote was for. If you think this answer can be improved (or is incorrect in some fashion!) please let me know.
lang-py
RUN pip install requestsentrypoint.shscript look like?