Can't install any pip package within my docker environment, since it won't be recognized

I am running a Cookiecutter django project in a docker environment and I would like to add new packages via pip. Specifically I want to add: djangorestframework-jwt

When I do: docker-compose -f local.yml run --rm django pip install it seems like it would be perfectly working because I get: Successfully installed PyJWT-1.7.1 djangorestframework-jwt-1.11.0

Now the problem is that it doesn't install it. It doesn't appear when I run pip freeze, and also not in pip list

Then I tried to put it into my requirements.txt file and run it with: docker-compose -f local.yml run --rm django pip install -r requirements/base.txt

Same result. It says that it is successfully installed but it is not. I thought it might be a problem with my django version and the package, but the same happens when I try to update my pip. It says it updated, but when I run pip install -upgrade pip I get again: You should consider upgrading via the 'pip install --upgrade pip' command.

I'm running out of options.

My requirements:

-r ./base.txt

Werkzeug==0.14.1  #
ipdb==0.11  #
Sphinx==1.7.5  #
psycopg2==2.7.4 --no-binary psycopg2  #

# Testing
# ------------------------------------------------------------------------------
pytest==3.6.3  #
pytest-sugar==0.9.1  #

# Code quality
# ------------------------------------------------------------------------------
flake8==3.5.0  #
coverage==4.5.1  #

# Django
# ------------------------------------------------------------------------------
factory-boy==2.11.1  #

django-debug-toolbar==1.9.1  #
django-extensions==2.0.7  #
django-coverage-plugin==1.5.0  #
pytest-django==3.3.2  #
djangorestframework-jwt==1.11.0 #

Output of pip list:

Package                  Version 
------------------------ --------
alabaster                0.7.12  
argon2-cffi              18.1.0  
atomicwrites             1.3.0   
attrs                    19.1.0  
Babel                    2.6.0   
backcall                 0.1.0   
certifi                  2019.3.9
cffi                     1.12.2  
chardet                  3.0.4   
coreapi                  2.3.3   
coreschema               0.0.4   
coverage                 4.5.1   
decorator                4.4.0   
defusedxml               0.5.0   
Django                   2.0.7   
django-allauth           0.36.0  
django-coverage-plugin   1.5.0   
django-crispy-forms      1.7.2   
django-debug-toolbar     1.9.1   
django-environ           0.4.5   
django-extensions        2.0.7   
django-model-utils       3.1.2   
django-redis             4.9.0   
django-widget-tweaks     1.4.3   
djangorestframework      3.8.2   
docutils                 0.14    
factory-boy              2.11.1  
Faker                    1.0.4   
flake8                   3.5.0   
idna                     2.8     
imagesize                1.1.0   
ipdb                     0.11    
ipython                  7.4.0   
ipython-genutils         0.2.0   
itypes                   1.1.0   
jedi                     0.13.3  
Jinja2                   2.10    
MarkupSafe               1.1.1   
mccabe                   0.6.1   
more-itertools           6.0.0   
oauthlib                 3.0.1   
packaging                19.0    
parso                    0.3.4   
pexpect                  4.6.0   
pickleshare              0.7.5   
Pillow                   5.2.0   
pip                      19.0.3  
pluggy                   0.6.0   
prompt-toolkit           2.0.9   
psycopg2                 2.7.4   
ptyprocess               0.6.0   
py                       1.8.0   
pycodestyle              2.3.1   
pycparser                2.19    
pyflakes                 1.6.0   
Pygments                 2.3.1   
pyparsing                2.3.1   
pytest                   3.6.3   
pytest-django            3.3.2   
pytest-sugar             0.9.1   
python-dateutil          2.8.0   
python-slugify           1.2.5   
python3-openid           3.1.0   
pytz                     2018.5  
redis                    3.2.1   
requests                 2.21.0  
requests-oauthlib        1.2.0   
setuptools               40.8.0  
six                      1.12.0  
snowballstemmer          1.2.1   
Sphinx                   1.7.5   
sphinxcontrib-websupport 1.1.0   
sqlparse                 0.3.0   
termcolor                1.1.0   
text-unidecode           1.2     
traitlets                4.3.2   
Unidecode                1.0.23  
uritemplate              3.0.0   
urllib3                  1.24.1  
wcwidth                  0.1.7   
Werkzeug                 0.14.1  
wheel                    0.33.1  

Any help is highly appreciated! Thanks...


docker-compose run starts a new container and executes the command in it. When used with --rm flag the container gets removed after command completes.

What happens is you get a new container created, and packages installed, or pip upgraded, inside this container. Once the command completes the container is removed.

If later on you run something like docker-compose -f local.yml run --rm pip list a brand new container will get created and pip list executed inside it, showing no packages from previous run since they were installed in a different container, which is already removed.

A better way would be to create docker image that includes your application and install pip packages during docker build. You can check a sample in this question

This way any time you start a container from your image it will have all packages inside.

Posted on by Anonymous