Skip to content

Commit 44ab326

Browse files
author
Chris Culhane
committed
Add docker compose V2 compatibility, see aws#3659 (comment)
1 parent e2624af commit 44ab326

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/sagemaker/local/image.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import sys
3030
import tarfile
3131
import tempfile
32+
from functools import cache
3233

3334
from distutils.spawn import find_executable
3435
from threading import Thread
@@ -58,6 +59,32 @@
5859

5960
logger = logging.getLogger(__name__)
6061

62+
@cache
63+
def get_docker_compose_cmd(*args):
64+
cmd = []
65+
# check docker compose v2 first
66+
try:
67+
output = subprocess.check_output(
68+
['docker', 'compose', 'version'],
69+
universal_newlines=True,
70+
)
71+
if 'v2' in output.strip():
72+
cmd.extend(['docker', 'compose'])
73+
74+
except subprocess.CalledProcessError:
75+
# check if docker-compose is installed
76+
if find_executable("docker-compose") is None:
77+
raise ImportError(
78+
"'docker-compose' is not installed. "
79+
"Local Mode features will not work without docker-compose. "
80+
"For more information on how to install 'docker-compose', please, see "
81+
"https://docs.docker.com/compose/install/"
82+
)
83+
cmd.append('docker-compose')
84+
85+
cmd.extend(args)
86+
return cmd
87+
6188

6289
class _SageMakerContainer(object):
6390
"""Handle the lifecycle and configuration of a local container execution.
@@ -93,15 +120,6 @@ def __init__(
93120
"""
94121
from sagemaker.local.local_session import LocalSession
95122

96-
# check if docker-compose is installed
97-
if find_executable("docker-compose") is None:
98-
raise ImportError(
99-
"'docker-compose' is not installed. "
100-
"Local Mode features will not work without docker-compose. "
101-
"For more information on how to install 'docker-compose', please, see "
102-
"https://docs.docker.com/compose/install/"
103-
)
104-
105123
self.sagemaker_session = sagemaker_session or LocalSession()
106124
self.instance_type = instance_type
107125
self.instance_count = instance_count
@@ -715,17 +733,15 @@ def _compose(self, detached=False):
715733
Args:
716734
detached:
717735
"""
718-
compose_cmd = "docker-compose"
719-
720-
command = [
736+
compose_args = [
721737
compose_cmd,
722738
"-f",
723739
os.path.join(self.container_root, DOCKER_COMPOSE_FILENAME),
724740
"up",
725741
"--build",
726742
"--abort-on-container-exit" if not detached else "--detach", # mutually exclusive
727743
]
728-
744+
command = get_docker_compose_cmd(*compose_args)
729745
logger.info("docker command: %s", " ".join(command))
730746
return command
731747

@@ -768,7 +784,7 @@ def _create_docker_host(self, host, environment, optml_subdirs, command, volumes
768784
# to setting --runtime=nvidia in the docker commandline.
769785
if self.instance_type == "local_gpu":
770786
host_config["deploy"] = {
771-
"resources": {"reservations": {"devices": [{"capabilities": ["gpu"]}]}}
787+
"resources": {"reservations": {"devices": [{"driver": "nvidia", "count": "all", "capabilities": ["gpu"]}]}}
772788
}
773789

774790
if command == "serve":

0 commit comments

Comments
 (0)