{"id":26626793,"url":"https://github.com/mrgkanev/tensorflow-gpu-docker-setup","last_synced_at":"2026-04-13T13:32:14.582Z","repository":{"id":282681662,"uuid":"949342959","full_name":"MrGKanev/TensorFlow-GPU-Docker-Setup","owner":"MrGKanev","description":"A Docker environment for TensorFlow GPU development with optimized configurations for WSL2, troubleshooting guides, and common error fixes","archived":false,"fork":false,"pushed_at":"2025-09-23T10:25:50.000Z","size":60,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-12T04:53:27.728Z","etag":null,"topics":["cuda","cuda-toolkit","deep-learning","dev-environment","development-tools","docker","gpu-acceleration","machine-learning","nvidia-docker","nvidia-docker-support","python","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MrGKanev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-03-16T08:31:55.000Z","updated_at":"2025-10-03T22:32:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"e3f1b454-e3bc-4eed-989f-7456e2019b63","html_url":"https://github.com/MrGKanev/TensorFlow-GPU-Docker-Setup","commit_stats":null,"previous_names":["mrgkanev/tensorflow-gpu-docker-setup"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MrGKanev/TensorFlow-GPU-Docker-Setup","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGKanev%2FTensorFlow-GPU-Docker-Setup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGKanev%2FTensorFlow-GPU-Docker-Setup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGKanev%2FTensorFlow-GPU-Docker-Setup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGKanev%2FTensorFlow-GPU-Docker-Setup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MrGKanev","download_url":"https://codeload.github.com/MrGKanev/TensorFlow-GPU-Docker-Setup/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGKanev%2FTensorFlow-GPU-Docker-Setup/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31754894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-13T13:27:56.013Z","status":"ssl_error","status_checked_at":"2026-04-13T13:21:23.512Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cuda","cuda-toolkit","deep-learning","dev-environment","development-tools","docker","gpu-acceleration","machine-learning","nvidia-docker","nvidia-docker-support","python","tensorflow"],"created_at":"2025-03-24T11:28:19.062Z","updated_at":"2026-04-13T13:32:14.570Z","avatar_url":"https://github.com/MrGKanev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TensorFlow GPU Docker Setup\n\n![TensorFlow Docker GPU Header](.github/img/docker-tensorflow-header.svg)\n\nThis repository contains Docker configuration for running TensorFlow with GPU support. The setup includes optimizations for WSL2 environments and includes all necessary packages for data science and deep learning tasks.\n\n## Prerequisites\n\n- [Docker](https://docs.docker.com/get-docker/) installed\n- [Docker Compose](https://docs.docker.com/compose/install/) installed\n- [NVIDIA GPU](https://www.nvidia.com/en-us/geforce/) with [compatible drivers](https://www.nvidia.com/Download/index.aspx)\n- [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html) installed\n- If using WSL2: [Properly configured GPU passthrough](https://docs.nvidia.com/cuda/wsl-user-guide/index.html)\n\n## Quick Start\n\n### Using Docker Compose (Recommended)\n\n```bash\n# Build and run with Docker Compose\ndocker-compose up --build\n\n# Run in detached mode\ndocker-compose up --build -d\n\n# Access the running container\ndocker exec -it tensorflow-gpu-custom bash\n\n# Stop the container\ndocker-compose down\n```\n\n### Using Docker Commands\n\n```bash\n# Build the image\ndocker build -t tensorflow-gpu-custom -f Dockerfile.gpu .\n\n# Run with GPU support\ndocker run --gpus all --name tensorflow-gpu-custom -it tensorflow-gpu-custom\n\n# Run the comprehensive GPU test\ndocker run --gpus all -it tensorflow-gpu-custom python /app/test_gpu.py\n```\n\n## Using Pre-built Container from GitHub\n\nThis repository publishes a pre-built container to GitHub Container Registry, which you can use directly:\n\n```bash\n# Pull the pre-built image\ndocker pull ghcr.io/mrgkanev/tensorflow-gpu-custom:latest\n\n# Run with GPU support\ndocker run --gpus all -it ghcr.io/mrgkanev/tensorflow-gpu-custom\n\n# Run the comprehensive GPU test\ndocker run --gpus all -it ghcr.io/mrgkanev/tensorflow-gpu-custom python /app/test_gpu.py\n```\n\n## Included Packages\n\nThe Docker image includes the following packages:\n- TensorFlow 2.11.0 with GPU support\n- NumPy\n- Pandas\n- scikit-learn\n- CUDA and cuDNN libraries\n\n## Performance Optimizations\n\nThe Docker image includes several performance optimizations:\n- GPU memory growth configuration\n- Thread management for TensorFlow\n- cuDNN optimizations\n- Memory allocation to prevent OOM errors\n\n## Benchmarks\n\n### Usage\n\n```bash\n# Simple run (all benchmarks, standard sizes)\npython /app/tf_benchmark.py\n\n# Quick run with smaller sizes\npython /app/tf_benchmark.py --small\n\n# Run specific benchmarks\npython /app/tf_benchmark.py --matrix  # Matrix multiplication only\npython /app/tf_benchmark.py --dense   # Dense layer only\npython /app/tf_benchmark.py --conv    # Convolution only\n```\n\n## Detailed Setup Instructions\n\n### 1. Using Docker Compose\n\nSave the `Dockerfile.gpu` and `docker-compose.yml` in your project directory:\n\n```bash\n# Build and start the container with GPU support\ndocker-compose up --build\n```\n\nThis will build the Docker image based on `Dockerfile.gpu` and start the container with proper GPU access.\n\n### 2. Building the Docker Image Manually\n\nSave the `Dockerfile.gpu` and `test_gpu.py` in your project directory, then build the image:\n\n```bash\ndocker build -t tensorflow-gpu-custom -f Dockerfile.gpu .\n```\n\nThis command creates a Docker image named `tensorflow-gpu-custom` based on the content of `Dockerfile.gpu`.\n\n### 3. Run the Container with GPU Access\n\nTo run the container with GPU support:\n\n```bash\ndocker run --gpus all -it tensorflow-gpu-custom\n```\n\nThe `--gpus all` flag is what enables GPU access from within the container.\n\n### 4. Verifying GPU Access\n\nOnce inside the container, you can verify GPU access with these methods:\n\n#### Option 1: Quick verification\n```python\nimport tensorflow as tf\nprint(tf.config.list_physical_devices('GPU'))\n```\n\n#### Option 2: Run the comprehensive test script\n```bash\npython /app/check_gpu.py\n```\n\nor\n\n```bash\npython /app/test_gpu.py\n```\n\nThese scripts perform:\n- GPU detection and environment checks\n- Matrix multiplication benchmarks comparing CPU vs GPU speed\n- Simple neural network training test\n\n## GPU Test Script\n\nThe included `test_gpu.py` script provides comprehensive testing of your GPU setup:\n\n```bash\n# Run directly from host\ndocker run --gpus all -it tensorflow-gpu-custom python /app/test_gpu.py\n\n# Or run from inside the container\npython /app/test_gpu.py\n```\n\nThe test script will:\n1. Check if TensorFlow can detect your GPU\n2. Run performance benchmarks using matrix multiplication\n3. Train a simple neural network to verify end-to-end functionality\n4. Provide detailed diagnostics if GPU is not detected\n\n## Using with PyCharm\n\nTo use this Docker container with PyCharm:\n\n1. **Configure Docker integration in PyCharm:**\n   - Go to Settings/Preferences → Build, Execution, Deployment → Docker\n   - Add your Docker connection\n\n2. **Configure Python Interpreter:**\n   - Go to Settings/Preferences → Project → Python Interpreter\n   - Click the gear icon → Add → Docker\n   - Select your Docker server and the `tensorflow-gpu-custom` image\n\n3. **Fix common TensorFlow errors:**\n   - If you encounter optimizer errors with LSTM models (like `KeyError: 'The optimizer cannot recognize variable lstm/lstm_cell/kernel:0'`), use the legacy optimizers:\n     ```python\n     # Change this:\n     from tensorflow.keras.optimizers import Adam\n     \n     # To this:\n     from tensorflow.keras.optimizers.legacy import Adam\n     ```\n   - Configure GPU memory growth at the start of your script:\n     ```python\n     gpus = tf.config.list_physical_devices('GPU')\n     for gpu in gpus:\n         tf.config.experimental.set_memory_growth(gpu, True)\n     ```\n   - See the included `tensorflow_pycharm_fix.py` script for a complete example\n\n4. **Configure Run Configuration:**\n   - Create a new Run Configuration\n   - Set the Docker container as the target environment\n   - Set the working directory to your project folder\n   - Map your local project directory to a directory in the container\n\n## Common Issues and Troubleshooting\n\n### GPU Not Detected\n\nIf you see `GPUs available: []` or errors about CUDA, check:\n\n1. **NVIDIA Driver Installation**\n   ```bash\n   # On host machine\n   nvidia-smi\n   ```\n   \n2. **NVIDIA Container Toolkit Installation**\n   ```bash\n   # On Linux host\n   dpkg -l | grep nvidia-container-toolkit\n   \n   # Install if missing\n   sudo apt-get install -y nvidia-container-toolkit\n   sudo systemctl restart docker\n   ```\n\n3. **Test with NVIDIA's Base Container**\n   ```bash\n   docker run --gpus all --rm nvidia/cuda:11.6.2-base-ubuntu20.04 nvidia-smi\n   ```\n\n### WSL2-Specific Issues\n\n1. **Install NVIDIA Drivers for WSL**\n   - Download the [NVIDIA CUDA driver for WSL](https://developer.nvidia.com/cuda/wsl) \n   - Install on your Windows host (not inside WSL)\n\n2. **Enable GPU in Docker Desktop**\n   - Open Docker Desktop Settings\n   - Go to Resources → WSL Integration\n   - Check \"Enable NVIDIA GPU support in WSL 2\"\n\n3. **Configure WSL**\n   - Create or edit `%USERPROFILE%\\.wslconfig` file in Windows with:\n     ```\n     [wsl2]\n     kernelCommandLine = systemd.unified_cgroup_hierarchy=0\n     ```\n\n4. **Restart WSL**\n   ```\n   wsl --shutdown\n   ```\n\n### Performance Optimization\n\n1. **Configure Memory Growth**\n   ```python\n   gpus = tf.config.list_physical_devices('GPU')\n   for gpu in gpus:\n     tf.config.experimental.set_memory_growth(gpu, True)\n   ```\n\n2. **Monitor GPU Usage**\n   ```bash\n   # From host or another terminal\n   watch -n 0.5 nvidia-smi\n   ```\n\n## Docker Commands Quick Reference\n\n- Build with Docker Compose: `docker-compose up --build`\n- Stop Docker Compose container: `docker-compose down`\n- Build the image: `docker build -t tensorflow-gpu-custom -f Dockerfile.gpu .`\n- Run with GPU access: `docker run --gpus all -it tensorflow-gpu-custom`\n- Run with volume mount: `docker run --gpus all -v $(pwd):/app -it tensorflow-gpu-custom`\n- Check existing images: `docker images`\n- Check running containers: `docker ps`\n- Execute commands in running container: `docker exec -it tensorflow-gpu-custom bash`\n\n## Sample Training Code\n\n```python\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.optimizers.legacy import Adam\n\n# Check for GPU\ngpus = tf.config.list_physical_devices('GPU')\nprint(f\"GPUs available: {gpus}\")\n\n# Optional: Enable memory growth\nfor gpu in gpus:\n    tf.config.experimental.set_memory_growth(gpu, True)\n\n# Create a simple model\nmodel = tf.keras.Sequential([\n    layers.Dense(128, activation='relu', input_shape=(10,)),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(1, activation='sigmoid')\n])\n\n# Compile the model\nmodel.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])\n\n# Generate some fake data\nX = np.random.random((1000, 10))\ny = np.random.randint(2, size=(1000, 1))\n\n# Train the model\nmodel.fit(X, y, epochs=5, batch_size=32)\n```\n\n## Notes\n\n- The Docker image is based on TensorFlow 2.11.0-gpu for optimal compatibility\n- The container includes proper environment setup for CUDA paths\n- If you encounter memory issues, adjust batch sizes or enable memory growth\n- Environment variables are set in the Dockerfile for optimal TensorFlow performance\n\n## Fixing Common TensorFlow Errors\n\n### KeyError with Optimizers\n\nIf you see this error:\n```\nKeyError: 'The optimizer cannot recognize variable lstm/lstm_cell/kernel:0'\n```\n\nThis happens because TensorFlow 2.11.0 has both experimental and legacy optimizers. The experimental optimizers have compatibility issues with certain layer types (especially LSTM).\n\n**Solution:**\n1. Use legacy optimizers:\n```python\n# Change this\nfrom tensorflow.keras.optimizers import Adam\n\n# To this\nfrom tensorflow.keras.optimizers.legacy import Adam\n```\n\n2. Make sure your input shapes are correct:\n```python\n# Check input shape expectations\nprint(model.input_shape)\n```\n\n3. Fix shape mismatches in your data. If your error mentions:\n```\nModel was constructed with shape (None, 30, 1) but was called on input with incompatible shape (64, 1, 1)\n```\n\nEnsure your data matches the expected dimensions:\n```python\n# Reshape your data to match expected input\nX = X.reshape(batch_size, sequence_length, features)\n```\n\n4. See the provided `tensorflow_pycharm_fix.py` script for complete examples","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrgkanev%2Ftensorflow-gpu-docker-setup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrgkanev%2Ftensorflow-gpu-docker-setup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrgkanev%2Ftensorflow-gpu-docker-setup/lists"}