Fix container username and home mount
This commit is contained in:
parent
fc2e5b1bca
commit
c387a7a365
4 changed files with 35 additions and 11 deletions
|
|
@ -16,14 +16,35 @@ class OpenCodeContainer:
|
|||
def __init__(self):
|
||||
self.project_path = Path.cwd().resolve()
|
||||
self.project_id = self._project_id(self.project_path)
|
||||
|
||||
# Store host user info once
|
||||
self.host_username = os.environ.get('USER', 'dev')
|
||||
self.host_uid = os.getuid()
|
||||
self.host_gid = os.getgid()
|
||||
|
||||
self.container_name = f"oc-{self.project_path.name}-{self.project_id}"
|
||||
self.docker_context_dir = Path(__file__).resolve().parent
|
||||
|
||||
def _get_xdg_data_home(self) -> Path:
|
||||
"""Get XDG_DATA_HOME with fallback to ~/.local/share"""
|
||||
xdg_data = os.environ.get('XDG_DATA_HOME')
|
||||
if xdg_data:
|
||||
return Path(xdg_data)
|
||||
return Path.home() / '.local' / 'share'
|
||||
|
||||
@property
|
||||
def container_home_path(self) -> Path:
|
||||
"""Container home directory in XDG_DATA_HOME"""
|
||||
return self._get_xdg_data_home() / 'opencode-container' / 'container-home'
|
||||
|
||||
# =========================
|
||||
# Public entrypoint
|
||||
# =========================
|
||||
|
||||
def run(self, args: list[str]) -> None:
|
||||
# Ensure container home directory exists
|
||||
self.container_home_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if not self.image_exists():
|
||||
self.build_image()
|
||||
|
||||
|
|
@ -54,9 +75,12 @@ class OpenCodeContainer:
|
|||
).returncode == 0
|
||||
|
||||
def build_image(self) -> None:
|
||||
print(f"Building image '{self.IMAGE}'")
|
||||
print(f"Building image '{self.IMAGE}' with user {self.host_username} ({self.host_uid}:{self.host_gid})")
|
||||
self._run([
|
||||
"docker", "build",
|
||||
"--build-arg", f"USERNAME={self.host_username}",
|
||||
"--build-arg", f"UID={self.host_uid}",
|
||||
"--build-arg", f"GID={self.host_gid}",
|
||||
"-t", self.IMAGE,
|
||||
str(self.docker_context_dir),
|
||||
])
|
||||
|
|
@ -66,16 +90,14 @@ class OpenCodeContainer:
|
|||
# =========================
|
||||
|
||||
def create_container(self) -> None:
|
||||
uid = os.getuid()
|
||||
gid = os.getgid()
|
||||
|
||||
print(f"Creating container '{self.container_name}'")
|
||||
|
||||
self._run([
|
||||
"docker", "create",
|
||||
"--name", self.container_name,
|
||||
"--user", f"{uid}:{gid}",
|
||||
"--user", f"{self.host_uid}:{self.host_gid}",
|
||||
"--volume", f"{self.project_path}:{self.project_path}",
|
||||
"--volume", f"{self.container_home_path}:/home/{self.host_username}",
|
||||
self.IMAGE,
|
||||
"sh", "-c", "sleep infinity",
|
||||
])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue