@docker_app.command(
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
help="Run a Docker container for the FastAgency app",
)
def run(
image: Annotated[
str,
typer.Argument(
...,
help="The Docker image to run",
),
] = "deploy_fastagency",
*,
name: Annotated[
str,
typer.Option(
"--name",
help="Assign a name to the container",
),
] = "deploy_fastagency",
env: Annotated[
Optional[list[str]],
typer.Option(
"--env",
"-e",
help="Set environment variables",
show_default=False,
),
] = None,
publish: Annotated[
Optional[list[str]],
typer.Option(
"--publish",
"-p",
help="Publish a container's port(s) to the host",
show_default=False,
),
] = None,
remove: Annotated[
bool,
typer.Option(
"--rm",
help="Automatically remove the container and its associated anonymous volumes when it exits",
is_flag=True,
),
] = False,
detach: Annotated[
bool,
typer.Option(
"--detach",
"-d",
help="Run container in background and print container ID",
is_flag=True,
),
] = True,
network: Annotated[
Optional[str],
typer.Option(
"--network",
help="Connect a container to a network",
show_default=False,
),
] = None,
ctx: typer.Context,
) -> None:
# Construct the docker run command using the provided options
command = ["docker", "run", "--name", name]
if env:
for env_var in env:
command.extend(["--env", env_var])
if publish:
for port in publish:
command.extend(["--publish", port])
if "8888:8888" not in publish:
command.extend(["--publish", "8888:8888"])
else:
command.extend(["--publish", "8888:8888"])
if remove:
command.append("--rm")
if detach:
command.append("--detach")
if network:
command.extend(["--network", network])
command += ctx.args
command.append(image)
try:
typer.echo(
f"Running FastAgency Docker image with the command: {' '.join(command)}"
)
# Run the docker command
result = subprocess.run( # nosec B603
command, check=True, capture_output=True, text=True
)
typer.echo(result.stdout)
except subprocess.CalledProcessError as e:
typer.echo(f"Error: {e.stderr}", err=True)
raise typer.Exit(code=1) from e