1

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
4
  • 3
    You can add lib requests to your requirements.txt before build the image using docker, are there any reason for not doing that? Commented Jan 10, 2021 at 13:40
  • Actually, I'm mounting host requirements.txt to the /app/requirements.txt so that any installation inside the container will be updated in the host. I actually want to remove the host dependency for the development environment due to a system dependency error in installing few requirements. I'm using Mac OS for development, but the production server is on linux. Commented Jan 10, 2021 at 13:42
  • Why not just add to requirements.txt or add a RUN to the dockerfile. RUN pip install requests Commented Jan 10, 2021 at 13:52
  • What does your entrypoint.sh script look like? Commented Jan 10, 2021 at 14:07

1 Answer 1

2

The problem is that you are trying to install packages, but:

  1. You are not root, so pip can't write to the systemd-wide locations, and
  2. Your anychat user has no home directory, so pip can'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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.