Skip to content

Commit

Permalink
Merge pull request #19 from conda-incubator/feat-configure-registry
Browse files Browse the repository at this point in the history
Make docker registry configurable to be able to pull from private registries
  • Loading branch information
costrouc authored Aug 5, 2022
2 parents 2818753 + 4192e06 commit 735743a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
71 changes: 40 additions & 31 deletions conda_docker/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import tempfile
import subprocess
from typing import List

from python_docker.base import Image
from python_docker.registry import Registry
Expand Down Expand Up @@ -595,19 +596,44 @@ def add_conda_layers(
LOGGER.info(f"docker image {image.name}:{image.tag} has {len(image.layers)} layers")


def parse_image_name(name):
parts = name.split(":")
if len(parts) == 1:
return parts[0], "latest"
return parts


def pull_container_image(base_image: str):
base_image_name, base_image_tag = parse_image_name(base_image)
if base_image == "scratch":
image = Image(name=base_image_name, tag=base_image_tag)
else:
LOGGER.info(f"pulling base image {base_image_name}:{base_image_tag}")
with timer(LOGGER, "pulling base image"):
registry = Registry(
hostname=os.environ.get(
"CONDA_DOCKER_REGISTRY_URL", "https://registry-1.docker.io"
),
username=os.environ.get("CONDA_DOCKER_REGISTRY_USERNAME"),
password=os.environ.get("CONDA_DOCKER_REGISTRY_PASSWORD"),
)
image = registry.pull_image(base_image_name, base_image_tag)
return image


def build_docker_environment(
base_image,
output_image,
base_image: str,
output_image: str,
records,
output_filename,
default_prefix,
download_dir,
user_conda,
channels_remap,
layering_strategy="layered",
output_filename: str,
default_prefix: str,
download_dir: str,
user_conda: str,
channels_remap: List,
layering_strategy: str = "layered",
):
image = build_docker_environment_image(
base_image,
pull_container_image(base_image),
output_image,
records,
default_prefix,
Expand All @@ -623,7 +649,7 @@ def build_docker_environment(


def build_docker_environment_image(
base_image,
base_image: Image,
output_image,
records,
default_prefix,
Expand All @@ -632,28 +658,11 @@ def build_docker_environment_image(
channels_remap,
layering_strategy="layered",
):
def parse_image_name(name):
parts = name.split(":")
if len(parts) == 1:
return parts[0], "latest"
return parts

base_image_name, base_image_tag = parse_image_name(base_image)
output_image_name, output_image_tag = parse_image_name(output_image)
base_image.name = output_image_name
base_image.tag = output_image_tag

with tempfile.TemporaryDirectory() as tmpdir:
if base_image == "scratch":
image = Image(name=output_image_name, tag=output_image_tag)
else:
LOGGER.info(f"pulling base image {base_image_name}:{base_image_tag}")
with timer(LOGGER, "pulling base image"):
registry = Registry(
hostname="https://registry-1.docker.io"
) # using dockerhub only at the moment
image = registry.pull_image(base_image_name, base_image_tag)
image.name = output_image_name
image.tag = output_image_tag

LOGGER.info("building conda environment")
with timer(LOGGER, "building conda environment"):
chroot_install(
Expand All @@ -666,12 +675,12 @@ def parse_image_name(name):
)

add_conda_layers(
image,
base_image,
str(tmpdir),
arcpath="/",
filter=conda_file_filter(),
records=records,
layering_strategy=layering_strategy,
)

return image
return base_image
4 changes: 3 additions & 1 deletion tests/test_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from conda_docker.conda import (
build_docker_environment_image,
pull_container_image,
find_user_conda,
conda_info,
find_precs,
Expand All @@ -21,6 +22,7 @@ class CondaMakeData:

user_conda = default_prefix = None
download_dir = precs = records = None
base_image = None


@skip_if_conda_build
Expand Down Expand Up @@ -51,7 +53,7 @@ def test_fetch_precs(self):

def test_build_docker_environment(self, class_tmpdir):
image = build_docker_environment_image(
"library/debian:sid-slim",
pull_container_image("library/debian:sid-slim"),
"example:test",
CondaMakeData.records,
CondaMakeData.default_prefix,
Expand Down

0 comments on commit 735743a

Please sign in to comment.