Skip to content

feat(BA-858): Set Docker container timezone using TZ env #3892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/ai/backend/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ def update_additional_gids(environ: MutableMapping[str, str], gids: Iterable[int
environ["ADDITIONAL_GIDS"] = ",".join(map(str, additional_gids))


def adjust_zoneinfo_string(host_path: Path) -> str:
parts = list(host_path.parts)
if sys.platform == "darwin":
try:
idx = parts.index("zoneinfo") # /var/db/timezone/zoneinfo/Asia/Seoul
except ValueError:
idx = -3 # /usr/share/zoneinfo.default/Asia/Seoul
else:
idx = parts.index("zoneinfo")
Copy link
Preview

Copilot AI Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On non-darwin platforms, if 'zoneinfo' is not present in host_path parts, this code will raise a ValueError. Consider adding error handling or an alternative fallback path.

Suggested change
idx = parts.index("zoneinfo")
try:
idx = parts.index("zoneinfo")
except ValueError:
raise ValueError("The 'zoneinfo' directory is not present in the provided host_path.")

Copilot uses AI. Check for mistakes.

return "/".join(parts[idx + 1 :])


class AbstractKernelCreationContext(aobject, Generic[KernelObjectType]):
kspec_version: int
distro: str
Expand Down Expand Up @@ -1911,6 +1923,17 @@ async def create_kernel(
f" {agent_architecture} machine",
)

# inject timezone variable
if sys.platform.startswith("linux") or sys.platform.startswith("darwin"):
localtime_path = (
Path("/etc/localtime").resolve()
if sys.platform == "darwin"
else Path("/etc/localtime")
)
timezone_str = adjust_zoneinfo_string(localtime_path)
if timezone_str:
environ["TZ"] = timezone_str

# Check if we need to pull the container image
do_pull = (not ctx.image_ref.is_local) and await self.check_image(
ctx.image_ref,
Expand Down
22 changes: 0 additions & 22 deletions src/ai/backend/agent/docker/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,28 +431,6 @@ async def get_intrinsic_mounts(self) -> Sequence[Mount]:
MountPermission.READ_WRITE,
)
)
# /etc/localtime and /etc/timezone mounts
if sys.platform.startswith("linux"):
localtime_file = Path("/etc/localtime")
timezone_file = Path("/etc/timezone")
if localtime_file.exists():
mounts.append(
Mount(
type=MountTypes.BIND,
source=localtime_file,
target=localtime_file,
permission=MountPermission.READ_ONLY,
)
)
if timezone_file.exists():
mounts.append(
Mount(
type=MountTypes.BIND,
source=timezone_file,
target=timezone_file,
permission=MountPermission.READ_ONLY,
)
)
# lxcfs mounts
lxcfs_root = Path("/var/lib/lxcfs")
if lxcfs_root.is_dir():
Expand Down
15 changes: 15 additions & 0 deletions src/ai/backend/runner/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ if [ ! -f "/usr/bin/scp" ]; then
ln -s /opt/kernel/dropbearmulti /usr/bin/scp
fi

# set timezone configuration
TARGET_TZ=${TZ:-"Etc/UTC"}
ZONEINFO_DIR="/usr/share/zoneinfo"
DEFAULT_ZONEINFO="${ZONEINFO_DIR}/Etc/UTC"
TARGET_ZONEINFO="${ZONEINFO_DIR}/${TARGET_TZ}"
if [ -f "$TARGET_ZONEINFO" ]; then
ln -sf "$TARGET_ZONEINFO" /etc/localtime
echo "$TARGET_TZ" > /etc/timezone
echo "Timezone set to: $TARGET_TZ"
else
ln -sf "$DEFAULT_ZONEINFO" /etc/localtime
echo "$DEFAULT_TZ" > /etc/timezone
echo "Invalid TZ. Timezone set to default: Etc/UTC"
fi

if [ $USER_ID -eq 0 ]; then

echo "WARNING: Running the user codes as root is not recommended."
Expand Down
Loading