Skip to content

Commit 1376860

Browse files
humitossafwanrahman
authored andcommitted
Mount pip_cache_path in Docker container (#3556)
* Mount `pip_cache_path` in Docker container This allow us to have permanent pip cache between different builds / projects to work faster locally and under slow internet connections. * Mount the pip cache in Docker only if we are in DEBUG mode * Mount the GLOBAL_PIP_CACHE conditionally Refactor, by creating a new method, where the binds are created to be easier to read.
1 parent e6f1c54 commit 1376860

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

readthedocs/doc_builder/environments.py

+38-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from datetime import datetime
1414

1515
from readthedocs.core.utils import slugify
16+
from django.conf import settings
1617
from django.utils.translation import ugettext_lazy as _, ugettext_noop
1718
from docker import Client
1819
from docker.utils import create_host_config
@@ -662,6 +663,42 @@ def get_client(self):
662663
)
663664
)
664665

666+
def get_container_host_config(self):
667+
"""
668+
Create the ``host_config`` settings for the container.
669+
670+
It mainly generates the proper path bindings between the Docker
671+
container and the Host by mounting them with the proper permissions.
672+
Besides, it mounts the ``GLOBAL_PIP_CACHE`` if it's set and we are under
673+
``DEBUG``.
674+
675+
The object returned is passed to Docker function
676+
``client.create_container``.
677+
"""
678+
binds = {
679+
SPHINX_TEMPLATE_DIR: {
680+
'bind': SPHINX_TEMPLATE_DIR,
681+
'mode': 'ro',
682+
},
683+
MKDOCS_TEMPLATE_DIR: {
684+
'bind': MKDOCS_TEMPLATE_DIR,
685+
'mode': 'ro',
686+
},
687+
self.project.doc_path: {
688+
'bind': self.project.doc_path,
689+
'mode': 'rw',
690+
},
691+
}
692+
693+
if getattr(settings, 'GLOBAL_PIP_CACHE', False) and settings.DEBUG:
694+
binds.update({
695+
self.project.pip_cache_path: {
696+
'bind': self.project.pip_cache_path,
697+
'mode': 'rw',
698+
}
699+
})
700+
return create_host_config(binds=binds)
701+
665702
@property
666703
def container_id(self):
667704
"""Return id of container if it is valid."""
@@ -715,20 +752,7 @@ def create_container(self):
715752
exit=DOCKER_TIMEOUT_EXIT_CODE)),
716753
name=self.container_id,
717754
hostname=self.container_id,
718-
host_config=create_host_config(binds={
719-
SPHINX_TEMPLATE_DIR: {
720-
'bind': SPHINX_TEMPLATE_DIR,
721-
'mode': 'ro'
722-
},
723-
MKDOCS_TEMPLATE_DIR: {
724-
'bind': MKDOCS_TEMPLATE_DIR,
725-
'mode': 'ro'
726-
},
727-
self.project.doc_path: {
728-
'bind': self.project.doc_path,
729-
'mode': 'rw'
730-
},
731-
}),
755+
host_config=self.get_container_host_config(),
732756
detach=True,
733757
environment=self.environment,
734758
mem_limit=self.container_mem_limit,

readthedocs/projects/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def checkout_path(self, version=LATEST):
449449
@property
450450
def pip_cache_path(self):
451451
"""Path to pip cache."""
452-
if getattr(settings, 'GLOBAL_PIP_CACHE', False):
452+
if getattr(settings, 'GLOBAL_PIP_CACHE', False) and settings.DEBUG:
453453
return settings.GLOBAL_PIP_CACHE
454454
return os.path.join(self.doc_path, '.cache', 'pip')
455455

0 commit comments

Comments
 (0)