Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Add --no-cache option to build.py when building containers (#13182)
Browse files Browse the repository at this point in the history
Add functionality to build.py to disable caching
  • Loading branch information
larroy authored and marcoabreu committed Nov 13, 2018
1 parent 1822cef commit 8b38528
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Docker containers

You need docker and nvidia docker if you have a GPU.

Also you need to run `pip3 install docker` as it uses the [docker python module](https://docker-py.readthedocs.io/en/stable/containers.html#)

If you are in ubuntu an easy way to install Docker CE is executing the
following script:

Expand Down
30 changes: 14 additions & 16 deletions ci/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def get_platforms(path: str = get_dockerfiles_path()) -> List[str]:

def get_docker_tag(platform: str, registry: str) -> str:
""":return: docker tag to be used for the container"""
if not registry:
registry = "mxnet_local"
return "{0}/build.{1}".format(registry, platform)


Expand All @@ -112,14 +114,14 @@ def get_docker_binary(use_nvidia_docker: bool) -> str:
return "nvidia-docker" if use_nvidia_docker else "docker"


def build_docker(platform: str, docker_binary: str, registry: str, num_retries: int, use_cache: bool) -> str:
def build_docker(platform: str, docker_binary: str, registry: str, num_retries: int, no_cache: bool) -> str:
"""
Build a container for the given platform
:param platform: Platform
:param docker_binary: docker binary to use (docker/nvidia-docker)
:param registry: Dockerhub registry name
:param num_retries: Number of retries to build the docker image
:param use_cache: will pass cache_from to docker to use the previously pulled tag
:param no_cache: pass no-cache to docker to rebuild the images
:return: Id of the top level image
"""
tag = get_docker_tag(platform=platform, registry=registry)
Expand All @@ -144,7 +146,9 @@ def build_docker(platform: str, docker_binary: str, registry: str, num_retries:
"-f", get_dockerfile(platform),
"--build-arg", "USER_ID={}".format(os.getuid()),
"--build-arg", "GROUP_ID={}".format(os.getgid())]
if use_cache:
if no_cache:
cmd.append("--no-cache")
elif registry:
cmd.extend(["--cache-from", tag])
cmd.extend(["-t", tag, get_dockerfiles_path()])

Expand Down Expand Up @@ -422,7 +426,7 @@ def main() -> int:
action='store_true')

parser.add_argument("-d", "--docker-registry",
help="Dockerhub registry name to retrieve cache from. Default is 'mxnetci'",
help="Dockerhub registry name to retrieve cache from.",
default='mxnetci',
type=str)

Expand All @@ -431,10 +435,8 @@ def main() -> int:
default=1,
type=int)

parser.add_argument("-c", "--no-dockerhub-cache", action="store_true",
help="Disables use of --cache-from option on docker build, allowing docker"
" to use local layers for caching. If absent, we use the cache from dockerhub"
" which is the default.")
parser.add_argument("--no-cache", action="store_true",
help="passes --no-cache to docker build")

parser.add_argument("command",
help="command to run in the container",
Expand All @@ -447,9 +449,6 @@ def main() -> int:

args = parser.parse_args()

def use_cache():
return not args.no_dockerhub_cache or under_ci()

command = list(chain(*args.command))
docker_binary = get_docker_binary(args.nvidiadocker)

Expand All @@ -472,10 +471,10 @@ def signal_handler(signum, _):
elif args.platform:
platform = args.platform
tag = get_docker_tag(platform=platform, registry=args.docker_registry)
if use_cache():
if args.docker_registry:
load_docker_cache(tag=tag, docker_registry=args.docker_registry)
build_docker(platform=platform, docker_binary=docker_binary, registry=args.docker_registry,
num_retries=args.docker_build_retries, use_cache=use_cache())
num_retries=args.docker_build_retries, no_cache=args.no_cache)
if args.build_only:
logging.warning("Container was just built. Exiting due to build-only.")
return 0
Expand Down Expand Up @@ -512,10 +511,9 @@ def signal_handler(signum, _):
logging.info("Artifacts will be produced in the build/ directory.")
for platform in platforms:
tag = get_docker_tag(platform=platform, registry=args.docker_registry)
if use_cache():
load_docker_cache(tag=tag, docker_registry=args.docker_registry)
load_docker_cache(tag=tag, docker_registry=args.docker_registry)
build_docker(platform, docker_binary=docker_binary, registry=args.docker_registry,
num_retries=args.docker_build_retries, use_cache=use_cache())
num_retries=args.docker_build_retries, no_cache=args.no_cache)
if args.build_only:
continue
shutil.rmtree(buildir(), ignore_errors=True)
Expand Down
4 changes: 4 additions & 0 deletions ci/docker_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def load_docker_cache(registry, docker_tag) -> None:
:return: None
"""
# We don't have to retag the image since it's already in the right format
if not registry:
return
assert docker_tag

logging.info('Loading Docker cache for %s from %s', docker_tag, registry)
pull_cmd = ['docker', 'pull', docker_tag]
subprocess.call(pull_cmd) # Don't throw an error if the image does not exist
Expand Down
1 change: 1 addition & 0 deletions ci/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker==3.5.0

0 comments on commit 8b38528

Please sign in to comment.