import pulumi import pulumi_docker as docker import json import os # Load the configuration from JSON file try: with open("config.json", "r") as f: containers_data = json.load(f) print("Loaded configuration from config.json successfully.") except FileNotFoundError as e: print(f"Error: {e}. The file 'config.json' was not found.") raise except json.JSONDecodeError as e: print(f"Error: {e}. There was an issue parsing 'config.json'.") raise except Exception as e: print(f"Unexpected error while loading config.json: {e}") raise # Create the Docker network try: network = docker.Network("testNetwork") print("Docker network 'testNetwork' created successfully.") except Exception as e: print(f"Error creating Docker network: {e}") raise # Create volumes for containers volumes = {} for container in containers_data["containers"]: for volume in container.get("volumes", []): if "volume_name" in volume and volume["volume_name"] not in volumes: try: volumes[volume["volume_name"]] = docker.Volume(volume["volume_name"]) print(f"Volume {volume['volume_name']} created successfully.") except Exception as e: print(f"Error creating volume {volume['volume_name']}: {e}") raise # Create the containers for container in containers_data["containers"]: instances = container.get("instances", 1) for i in range(instances): container_name = f"{container['name']}-{i}" if instances > 1 else container["name"] # Configure volumes volumes_config = [] for volume in container.get("volumes", []): try: if "volume_name" in volume: volumes_config.append(docker.ContainerVolumeArgs( container_path=volume["container_path"], volume_name=volumes[volume["volume_name"]].name )) else: volumes_config.append(docker.ContainerVolumeArgs( container_path=volume["container_path"], host_path=os.path.abspath(volume["host_path"]) )) print(f"Volume for container {container_name} configured successfully.") except Exception as e: print(f"Error configuring volume for container {container_name}: {e}") raise # Set entrypoint script (if any) entrypoint_script = "/entrypoint.sh" # Path where entrypoint.sh will be mounted # Create the container try: docker.Container( container_name, image=container["image"], envs=[f"{key}={value}" for key, value in container.get("envs", {}).items()], ports=[docker.ContainerPortArgs( internal=port["internal"], external=port["external"] + i ) for port in container.get("ports", [])], volumes=volumes_config + [ # Mount the entrypoint.sh script into the container (if it exists) docker.ContainerVolumeArgs( container_path="/entrypoint.sh", # Path inside the container host_path=os.path.abspath("entrypoint.sh") # Path on the host machine ) ] if "entrypoint.sh" in container.get("volumes", []) else [], network_mode=network.name, command=container.get("command", []) # Set the command from the JSON (if exists) ) print(f"Container {container_name} created successfully.") except Exception as e: print(f"Error creating container {container_name}: {e}") raise # Export the container URLs urls = {} for container in containers_data["containers"]: for i in range(container.get("instances", 1)): container_name = f"{container['name']}-{i}" if container.get("instances", 1) > 1 else container["name"] for port in container.get("ports", []): if "external" in port: external_port = port["external"] + i urls[container_name] = f"http://localhost:{external_port}" pulumi.export("urls", urls)