The Ultimate Guide to Running Stable Diffusion in Docker: GPU Passthrough, Containerization, and Deployment Strategies

The integration of artificial intelligence into creative workflows has reached a tipping point, with text-to-image generation becoming a standard tool for designers, artists, and engineers. At the forefront of this revolution is Stable Diffusion, an open-source text-to-image AI model capable of generating high-fidelity images from natural language prompts. While the raw power of the model is impressive, the operational complexity of running it on local hardware presents significant challenges for many users. The traditional method of installing Python environments, managing dependencies, and configuring graphics drivers directly on the host operating system often leads to what is known as "dependency hell." This state of affairs is characterized by conflicting library versions, broken system updates, and a cluttered host environment that can destabilize other critical applications. To address these issues, the industry has increasingly turned to containerization technologies, specifically Docker, to isolate the Stable Diffusion environment. Running Stable Diffusion in Docker ensures that the host system remains clean and stable, provides a reproducible setup that can be easily replicated across different machines, and allows administrators to manage different model versions without the risk of dependency conflicts. However, the transition to a containerized workflow is not without its technical hurdles. The most significant challenge lies in configuring GPU passthrough correctly, as Stable Diffusion relies heavily on graphical processing units to generate images in a reasonable amount of time. Without proper hardware acceleration, the generation process becomes prohibitively slow, rendering the tool impractical for most use cases. This comprehensive guide explores the technical intricacies of setting up Stable Diffusion in Docker, covering everything from NVIDIA GPU support and the Container Toolkit to interactive web interfaces and API-based generation methods. It examines various deployment strategies, including Docker Compose configurations, volume management for persistent data, and the nuances of running prebuilt images versus building custom environments for development and testing.

Prerequisites and Environment Verification

Before attempting to deploy a Stable Diffusion container, it is imperative to ensure that the host system meets the necessary hardware and software requirements. The foundation of any successful deployment is the presence of a compatible NVIDIA GPU. The minimum requirement for running Stable Diffusion effectively is a GPU with at least 4GB of VRAM. While this baseline allows for the generation of lower-resolution images with simpler models, it is strongly recommended to utilize a GPU with 8GB or more of VRAM to handle larger batch sizes, higher resolutions, and more complex models without running out of memory. In addition to the hardware, the host system must have the appropriate NVIDIA drivers installed. These drivers serve as the bridge between the operating system and the GPU hardware, enabling the execution of compute-intensive tasks. However, the installation of host drivers is only the first step. To allow Docker containers to access the GPU, the NVIDIA Container Toolkit must be installed and configured. This toolkit extends the standard Docker functionality, enabling containers to see and use the host's NVIDIA GPUs.

Verifying the GPU setup is a critical step that should not be skipped. The first check involves confirming that the NVIDIA driver is correctly installed and recognized by the host operating system. This can be achieved by executing the nvidia-smi command in the terminal. This command queries the NVIDIA management interface and displays information about the GPU, including the driver version, GPU utilization, memory usage, and temperature. If this command fails or returns an error, it indicates that the host drivers are not properly installed or configured, and no amount of Docker configuration will resolve the issue until this foundational layer is fixed. Once the host driver is confirmed to be working, the next step is to verify that Docker can see the GPU. This is done by running a temporary NVIDIA CUDA container and executing the nvidia-smi command within that container. The command for this verification is docker run --rm --gpus all nvidia/cuda:12.2.0-base-ubuntu22.04 nvidia-smi. This command pulls the nvidia/cuda:12.2.0-base-ubuntu22.04 image, runs it with GPU access enabled (--gpus all), and immediately removes the container after execution (--rm). If this command fails, it indicates that the NVIDIA Container Toolkit is either not installed or not configured correctly. To resolve this, one must add the NVIDIA container toolkit repository to the system's package manager and install the necessary packages. This ensures that the Docker daemon can communicate with the NVIDIA drivers and expose the GPU resources to the containers.

Project Structure and Volume Management

One of the most common pitfalls in containerized deployments is the loss of data when the container is stopped or removed. By default, any files created inside a container are deleted when the container is removed. This is problematic for Stable Diffusion, which generates large model files, user-specific configurations, extensions, and output images that need to be preserved across sessions. To mitigate this, it is recommended to create a data directory on the host system, outside of the container, and mount this directory to a specific path inside the container. This approach places all data files, including models, extensions, outputs, and localizations, in a known location on the host system. This makes it easy for tools and applications on the host system to access the files directly, without needing to execute commands inside the container. Furthermore, it ensures that the data persists even if the container is destroyed and recreated. However, this method requires the user to ensure that the directory exists on the host and that the necessary permissions and security mechanisms are set up correctly. If the host directory does not exist or if the container does not have the correct permissions to write to it, the deployment will fail or result in data corruption.

To establish a robust project structure, users should create a dedicated directory for their Stable Diffusion deployment. This can be achieved by executing a series of commands to create the necessary subdirectories for models, outputs, and extensions. The command mkdir -p stable-diffusion-docker/{models,outputs,extensions} creates a main directory named stable-diffusion-docker and three subdirectories within it: models, outputs, and extensions. The -p flag ensures that parent directories are created as needed, and the curly braces allow for the creation of multiple directories in a single command. After creating the directory structure, the user should navigate into the new directory using the cd stable-diffusion-docker command. This directory will serve as the root for the Docker Compose configuration and the mounted volumes. The models directory will store the downloaded model weights, which can be several gigabytes in size. The outputs directory will store the generated images, allowing for easy access and organization. The extensions directory will store any additional plugins or extensions that enhance the functionality of the WebUI. By separating these components into distinct directories, users can manage their data more effectively and ensure that each type of file is stored in a logical location.

Docker Compose Configuration for Production

Docker Compose provides a powerful way to define and run multi-container Docker applications. It allows users to specify all the services, networks, and volumes required for their application in a single YAML file. This makes it easy to manage the deployment and ensures that all components are configured correctly. For a Stable Diffusion WebUI deployment, a Docker Compose file can be created to define the service, specify the image, configure the ports, mount the volumes, and enable GPU support. The following is an example of a Docker Compose configuration for a Stable Diffusion WebUI with NVIDIA GPU support. The service is named stable-diffusion and uses the ghcr.io/neggles/sd-webui-docker:latest image. This image is a popular choice for deploying Stable Diffusion in Docker, as it is well-maintained and supports a wide range of features. The container name is set to stable-diffusion for easy identification. The ports are mapped such that port 7860 on the host is forwarded to port 7860 in the container, allowing access to the WebUI via a web browser. The volumes are mounted to persist the models, outputs, and extensions, ensuring that they are not lost when the container is restarted. The deployment section specifies the resources required, including the NVIDIA GPU. The driver: nvidia setting ensures that the NVIDIA driver is used, and the count: 1 setting requests one GPU. The capabilities: [gpu] setting ensures that the GPU is available for compute tasks. The environment section includes the CLI_ARGS variable, which is used to pass command-line arguments to the WebUI. The --xformers argument enables the use of the xformers library, which can significantly speed up image generation. The --api argument enables the REST API, allowing other applications to interact with the WebUI programmatically. The --listen argument allows the WebUI to be accessed from other devices on the network. The restart: unless-stopped policy ensures that the container is automatically restarted if it crashes, unless it is explicitly stopped by the user.

yaml services: stable-diffusion: image: ghcr.io/neggles/sd-webui-docker:latest container_name: stable-diffusion ports: - "7860:7860" volumes: - ./models:/app/models - ./outputs:/app/outputs - ./extensions:/app/extensions deploy: resources: reservations: devices: - driver: nvidia count: 1 capabilities: [gpu] environment: - CLI_ARGS=--xformers --api --listen restart: unless-stopped

After creating the Docker Compose file, the service can be started by executing the docker compose up -d command. This command pulls the image if it is not already present and starts the container in detached mode, allowing it to run in the background. The first startup may take some time, as the container needs to download the Stable Diffusion model weights, which can be several gigabytes in size. During this process, it is helpful to monitor the logs to ensure that the download is progressing correctly. This can be done by executing the docker compose logs -f stable-diffusion command. The -f flag follows the logs in real-time, allowing the user to see any errors or warnings that may occur. Once the model is downloaded and the WebUI is fully initialized, it will be accessible via the web browser at http://localhost:7860. The user can then begin generating images using the interactive interface or the REST API.

Alternative Docker Images and Minimal Configurations

While the ghcr.io/neggles/sd-webui-docker image is a popular choice, there are other options available on Docker Hub that may better suit specific needs. One such option is the universonic/stable-diffusion-webui image, which is updated regularly and supports a wide range of features. This image is designed for NVIDIA GPUs and includes a browser interface based on the Gradio library. The basic example for running this image is docker run --gpus all --restart unless-stopped -p 8080:8080 --name stable-diffusion-webui -d universonic/stable-diffusion-webui. This command runs the container with GPU access, sets the restart policy to unless-stopped, maps port 8080 on the host to port 8080 in the container, names the container stable-diffusion-webui, and runs it in detached mode. It is recommended to create a data directory on the host system and mount it to a directory visible from inside the container. This places the data files in a known location on the host system, making it easy for tools and applications on the host system to access the files. The downside of this approach is that the user needs to ensure that the directory exists and that the necessary permissions are set up correctly.

bash docker run --gpus all --restart unless-stopped -p 8080:8080 \ --name stable-diffusion-webui -d universonic/stable-diffusion-webui

For more advanced users, a more complex Docker Compose configuration can be used to fine-tune the deployment. This configuration includes additional volumes for inputs, textual inversion templates, embeddings, localizations, and other data files. It also includes security settings such as cap_drop: ALL and cap_add: NET_BIND_SERVICE, which drop all capabilities and add back only the one required for binding to network ports. This enhances the security of the container by minimizing the privileges it has on the host system. The deploy section includes placement constraints, such as node.labels.iface !=, which can be used to ensure that the container is only deployed on nodes with specific labels. This is useful in multi-node Kubernetes or Docker Swarm deployments, where different nodes may have different hardware configurations.

yaml version: "3.2" services: stable-diffusion-webui: image: universonic/stable-diffusion-webui:minimal command: --no-half --no-half-vae --precision full runtime: nvidia restart: unless-stopped ports: - "8080:8080/tcp" volumes: - /my/own/datadir/inputs:/app/stable-diffusion-webui/inputs - /my/own/datadir/textual_inversion_templates:/app/stable-diffusion-webui/textual_inversion_templates - /my/own/datadir/embeddings:/app/stable-diffusion-webui/embeddings - /my/own/datadir/extensions:/app/stable-diffusion-webui/extensions - /my/own/datadir/models:/app/stable-diffusion-webui/models - /my/own/datadir/localizations:/app/stable-diffusion-webui/localizations - /my/own/datadir/outputs:/app/stable-diffusion-webui/outputs cap_drop: - ALL cap_add: - NET_BIND_SERVICE deploy: mode: global placement: constraints: - "node.labels.iface != "

Development and Testing with Sygil WebUI

For developers and testers, the Sygil WebUI provides a specialized Docker environment that is optimized for speed and flexibility. This environment is intended to speed up the development and testing of Stable Diffusion WebUI features. The use of a container image format allows for the packaging and isolation of Stable Diffusion and WebUI dependencies separate from the host environment. This ensures that the development environment is consistent and reproducible, regardless of the host system's configuration. The easiest way to run the Sygil WebUI is to use the prebuilt image from Docker Hub. The command docker pull tukirito/sygil-webui:latest pulls the latest version of the image. This image includes the barebones environment to run the Web UI. The models will be downloaded during the installation process, so users must ensure that they have sufficient storage space and a stable internet connection. Users must also take care of the volume for the sd/models directory, as the image does not include any preloaded models.

The image can be run using the following command: docker container run --rm -d -p 8501:8501 -e STREAMLIT_SERVER_HEADLESS=true -e "WEBUI_SCRIPT=webui_streamlit.py" -e "VALIDATE_MODELS=false" -v "${PWD}/outputs:/sd/outputs" --gpus all tukirito/sygil-webui:latest. This command runs the container in detached mode, maps port 8501 on the host to port 8501 in the container, sets several environment variables to configure the WebUI, mounts the current directory's outputs folder to the /sd/outputs directory in the container, and enables GPU access. The --rm flag ensures that the container is removed after it stops, preventing clutter. The STREAMLIT_SERVER_HEADLESS=true environment variable allows the Streamlit server to run in headless mode, which is necessary for remote access. The WEBUI_SCRIPT=webui_streamlit.py environment variable specifies the script to run, and the VALIDATE_MODELS=false environment variable disables model validation, which can speed up startup time. Note that if the container is run on a local machine, the output directory will be created in the current directory from which the command is executed.

bash docker container run --rm -d -p 8501:8501 -e STREAMLIT_SERVER_HEADLESS=true -e "WEBUI_SCRIPT=webui_streamlit.py" -e "VALIDATE_MODELS=false" -v "${PWD}/outputs:/sd/outputs" --gpus all tukirito/sygil-webui:latest

For those who wish to build their own image, the Sygil WebUI provides a Dockerfile that can be used to build a custom Docker image. This allows for greater control over the environment and enables the inclusion of specific libraries or configurations that may not be present in the prebuilt image. The build process requires a host computer with AMD64 architecture and will take several minutes to complete. After the image build has completed, the user will have a Docker image for running the Stable Diffusion WebUI tagged sygil-webui:dev. This image can then be run using the same commands as the prebuilt image, with the appropriate tag. Users can also start the container in "daemon" mode by applying the -d option to the docker compose up command. This will run the server in the background, allowing the user to close the console window without losing their work. When running in daemon mode, the logging output from the container can be viewed by running docker logs sd-webui. Note that depending on the version of Docker or Docker Compose installed, the command may be docker-compose (older versions) or docker compose (newer versions). The container may take several minutes to start up if model weights or checkpoints need to be downloaded, so users should be patient during this process.

Cross-Platform Deployment and Troubleshooting

The deployment of Stable Diffusion in Docker is not limited to Linux systems. The approach with Docker Compose is recommended for ease of management on Windows with WSL2 and NVIDIA GPUs, as well as Apple Silicon Macs with MPS. The AbdBarho/stable-diffusion-webui-docker repository provides docker-compose.yml files that define how to build and run the Automatic1111 Web UI within this container. This method uses Docker Desktop, which is available for both Windows and macOS. On Windows, Docker Desktop integrates with WSL2, allowing for a seamless Linux-like environment within the Windows OS. On macOS, Docker Desktop provides a virtual machine that runs Linux, allowing Docker containers to run on Apple Silicon Macs using Metal Performance Shaders (MPS) for GPU acceleration. This provides a high degree of isolation, as all the necessary Python versions, libraries (like torch and gradio), and dependencies are packaged inside the container, separate from the main Windows or WSL2 Ubuntu environment. This isolation prevents dependency conflicts and ensures that the host system remains stable and clean. Additionally, the local files, such as models and outputs, are "mounted" into the container, allowing users to access them with their usual file manager. This provides a convenient way to manage data without needing to execute commands inside the container.

Despite the benefits of containerization, users may encounter errors during the setup process. Common issues include GPU passthrough failures, permission errors, and network connectivity problems. It is important to carefully follow the installation instructions and verify each step to ensure that the environment is configured correctly. If errors occur, the logs should be examined to identify the root cause. For example, if the container fails to start, the logs may indicate that the NVIDIA driver is not installed or that the NVIDIA Container Toolkit is not configured correctly. If the WebUI is not accessible, the logs may indicate that the port is already in use or that there is a firewall blocking the connection. By systematically troubleshooting these issues, users can ensure a smooth and successful deployment of Stable Diffusion in Docker. The process may take some time, but the result is a robust and scalable environment that is easy to manage and maintain.

Conclusion

The deployment of Stable Diffusion in Docker represents a significant advancement in the accessibility and manageability of AI-powered image generation. By leveraging containerization technologies, users can isolate the complex dependencies of the Stable Diffusion environment, ensuring that their host systems remain clean and stable. This approach not only simplifies the setup process but also facilitates reproducible deployments and easy management of different model versions. The use of Docker Compose further enhances this by allowing for the definition of multi-container applications, including GPU passthrough, volume management, and network configuration. While the initial setup may present challenges, particularly in configuring GPU passthrough and managing volume permissions, the long-term benefits of a containerized workflow are substantial. Users gain the ability to run Stable Diffusion in a variety of environments, from local development machines to cloud-based infrastructure, with minimal friction. The availability of prebuilt images and community-maintained repositories further lowers the barrier to entry, making it possible for even those with limited technical expertise to deploy and utilize this powerful tool. As the field of AI continues to evolve, the ability to quickly and easily deploy and update models will become increasingly important, and Docker provides a robust and flexible foundation for meeting this need.

Sources

  1. How to Run Stable Diffusion in Docker
  2. Universonic/Stable-Diffusion-Webui
  3. Sygil WebUI Docker Guide
  4. Roger Frost Stable Diffusion

Related Posts