1+ #! /bin/bash
2+ # Copyright 2024 The JAX Authors.
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # http://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+ # ==============================================================================
16+ # Sets up a Docker container for JAX CI.
17+ #
18+ # This script creates and starts a Docker container named "jax" for internal
19+ # JAX CI jobs.
20+ #
21+ # Note: While GitHub action workflows use the same Docker images, they do not
22+ # run this script as they leverage built-in containerization features to run
23+ # jobs within a container.
24+ # Usage:
25+ # ./ci/utilities/run_docker_container.sh
26+ # docker exec jax <build-script>
27+ # E.g: docker exec jax ./ci/build_artifacts.sh jaxlib
28+ #
29+ # -e: abort script if one command fails
30+ # -u: error if undefined variable used
31+ # -x: log all commands
32+ # -o history: record shell history
33+ # -o allexport: export all functions and variables to be available to subscripts
34+ set -exu -o history -o allexport
35+
36+ source ./ci/envs/docker.env
37+
38+ # Keep the existing "jax" container if it's already present.
39+ if ! docker container inspect jax > /dev/null 2>&1 ; then
40+ # Simple retry logic for docker-pull errors. Sleeps if a pull fails.
41+ # Pulling an already-pulled container image will finish instantly, so
42+ # repeating the command costs nothing.
43+ docker pull " $JAXCI_DOCKER_IMAGE " || sleep 15
44+ docker pull " $JAXCI_DOCKER_IMAGE "
45+
46+ # Docker on Windows doesn't support the `host` networking mode, and so
47+ # port-forwarding is required for the container to detect it's running on GCE.
48+ # This is needed for RBE configs to work.
49+ if [[ " $( uname -s) " =~ " MSYS_NT" ]]; then
50+ export IP_ADDR=$( powershell -command " (Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias 'vEthernet (nat)').IPAddress" )
51+ netsh interface portproxy add v4tov4 listenaddress=$IP_ADDR listenport=80 connectaddress=169.254.169.254 connectport=80
52+ JAXCI_DOCKER_ARGS=" $JAXCI_DOCKER_ARGS -e GCE_METADATA_HOST=$IP_ADDR "
53+ fi
54+
55+ # Create a temporary file to pass any user defined JAXCI_ variables to the
56+ # container.
57+ JAXCI_TEMP_ENVFILE_DIR=$( mktemp)
58+ env | grep " JAXCI_" > " $JAXCI_TEMP_ENVFILE_DIR "
59+
60+ # On Windows, convert MSYS Linux-like paths to Windows paths.
61+ if [[ " $( uname -s) " =~ " MSYS_NT" ]]; then
62+ echo ' Converting MSYS Linux-like paths to Windows paths for setting up the Docker container'
63+ # Convert all "JAXCI.*DIR" variables
64+ source <( python ./ci/utilities/convert_msys_paths_to_win_paths.py --convert $( env | grep " JAXCI.*DIR" | awk -F= ' {print $1}' ) )
65+ fi
66+
67+ # Start the container.
68+ docker run $JAXCI_DOCKER_ARGS --name jax \
69+ --env-file " $JAXCI_TEMP_ENVFILE_DIR " \
70+ -w " $JAXCI_DOCKER_WORK_DIR " -itd --rm \
71+ -v " $JAXCI_JAX_GIT_DIR :$JAXCI_DOCKER_WORK_DIR " \
72+ " $JAXCI_DOCKER_IMAGE " \
73+ bash
74+
75+ if [[ " $( uname -s) " =~ " MSYS_NT" ]]; then
76+ # Allow requests from the container.
77+ CONTAINER_IP_ADDR=$( docker inspect -f ' {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' jax)
78+ netsh advfirewall firewall add rule name=" Allow Metadata Proxy" dir=in action=allow protocol=TCP localport=80 remoteip=" $CONTAINER_IP_ADDR "
79+ fi
80+ fi
0 commit comments