Skip to content

Commit

Permalink
Mount pip_cache_path in Docker container (#3556)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
humitos authored and safwanrahman committed Feb 15, 2018
1 parent e6f1c54 commit 1376860
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
52 changes: 38 additions & 14 deletions readthedocs/doc_builder/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from datetime import datetime

from readthedocs.core.utils import slugify
from django.conf import settings
from django.utils.translation import ugettext_lazy as _, ugettext_noop
from docker import Client
from docker.utils import create_host_config
Expand Down Expand Up @@ -662,6 +663,42 @@ def get_client(self):
)
)

def get_container_host_config(self):
"""
Create the ``host_config`` settings for the container.
It mainly generates the proper path bindings between the Docker
container and the Host by mounting them with the proper permissions.
Besides, it mounts the ``GLOBAL_PIP_CACHE`` if it's set and we are under
``DEBUG``.
The object returned is passed to Docker function
``client.create_container``.
"""
binds = {
SPHINX_TEMPLATE_DIR: {
'bind': SPHINX_TEMPLATE_DIR,
'mode': 'ro',
},
MKDOCS_TEMPLATE_DIR: {
'bind': MKDOCS_TEMPLATE_DIR,
'mode': 'ro',
},
self.project.doc_path: {
'bind': self.project.doc_path,
'mode': 'rw',
},
}

if getattr(settings, 'GLOBAL_PIP_CACHE', False) and settings.DEBUG:
binds.update({
self.project.pip_cache_path: {
'bind': self.project.pip_cache_path,
'mode': 'rw',
}
})
return create_host_config(binds=binds)

@property
def container_id(self):
"""Return id of container if it is valid."""
Expand Down Expand Up @@ -715,20 +752,7 @@ def create_container(self):
exit=DOCKER_TIMEOUT_EXIT_CODE)),
name=self.container_id,
hostname=self.container_id,
host_config=create_host_config(binds={
SPHINX_TEMPLATE_DIR: {
'bind': SPHINX_TEMPLATE_DIR,
'mode': 'ro'
},
MKDOCS_TEMPLATE_DIR: {
'bind': MKDOCS_TEMPLATE_DIR,
'mode': 'ro'
},
self.project.doc_path: {
'bind': self.project.doc_path,
'mode': 'rw'
},
}),
host_config=self.get_container_host_config(),
detach=True,
environment=self.environment,
mem_limit=self.container_mem_limit,
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def checkout_path(self, version=LATEST):
@property
def pip_cache_path(self):
"""Path to pip cache."""
if getattr(settings, 'GLOBAL_PIP_CACHE', False):
if getattr(settings, 'GLOBAL_PIP_CACHE', False) and settings.DEBUG:
return settings.GLOBAL_PIP_CACHE
return os.path.join(self.doc_path, '.cache', 'pip')

Expand Down

0 comments on commit 1376860

Please sign in to comment.