|
| 1 | +# Building Images from Dockerfiles |
| 2 | + |
| 3 | +Testcontainers-Python allows you to build Docker images from Dockerfiles during test execution. This is useful when you need to test custom images or when you want to ensure your Dockerfile builds correctly. |
| 4 | + |
| 5 | +## Basic Image Building |
| 6 | + |
| 7 | +The simplest way to build an image is using the `build_image` function: |
| 8 | + |
| 9 | +```python |
| 10 | +from testcontainers.core.container import build_image |
| 11 | + |
| 12 | +# Build an image from a Dockerfile |
| 13 | +image = build_image( |
| 14 | + path="path/to/dockerfile/directory", |
| 15 | + tag="myapp:test" |
| 16 | +) |
| 17 | + |
| 18 | +# Use the built image |
| 19 | +with GenericContainer(image) as container: |
| 20 | + # Your test code here |
| 21 | + pass |
| 22 | +``` |
| 23 | + |
| 24 | +## Building with Options |
| 25 | + |
| 26 | +You can customize the build process with various options: |
| 27 | + |
| 28 | +```python |
| 29 | +# Build with specific Dockerfile |
| 30 | +image = build_image( |
| 31 | + path="path/to/dockerfile/directory", |
| 32 | + dockerfile="Dockerfile.test", |
| 33 | + tag="myapp:test" |
| 34 | +) |
| 35 | + |
| 36 | +# Build with build arguments |
| 37 | +image = build_image( |
| 38 | + path="path/to/dockerfile/directory", |
| 39 | + buildargs={ |
| 40 | + "VERSION": "1.0.0", |
| 41 | + "ENVIRONMENT": "test" |
| 42 | + }, |
| 43 | + tag="myapp:test" |
| 44 | +) |
| 45 | + |
| 46 | +# Build with target stage |
| 47 | +image = build_image( |
| 48 | + path="path/to/dockerfile/directory", |
| 49 | + target="test", |
| 50 | + tag="myapp:test" |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +## Building with Context |
| 55 | + |
| 56 | +You can specify a build context: |
| 57 | + |
| 58 | +```python |
| 59 | +# Build with specific context |
| 60 | +image = build_image( |
| 61 | + path="path/to/dockerfile/directory", |
| 62 | + context="path/to/build/context", |
| 63 | + tag="myapp:test" |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +## Building with Cache |
| 68 | + |
| 69 | +You can control build caching: |
| 70 | + |
| 71 | +```python |
| 72 | +# Build without cache |
| 73 | +image = build_image( |
| 74 | + path="path/to/dockerfile/directory", |
| 75 | + nocache=True, |
| 76 | + tag="myapp:test" |
| 77 | +) |
| 78 | + |
| 79 | +# Build with specific cache from |
| 80 | +image = build_image( |
| 81 | + path="path/to/dockerfile/directory", |
| 82 | + cache_from=["myapp:latest"], |
| 83 | + tag="myapp:test" |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +## Building with Platform |
| 88 | + |
| 89 | +You can specify the target platform: |
| 90 | + |
| 91 | +```python |
| 92 | +# Build for specific platform |
| 93 | +image = build_image( |
| 94 | + path="path/to/dockerfile/directory", |
| 95 | + platform="linux/amd64", |
| 96 | + tag="myapp:test" |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +## Building with Labels |
| 101 | + |
| 102 | +You can add labels to the built image: |
| 103 | + |
| 104 | +```python |
| 105 | +# Build with labels |
| 106 | +image = build_image( |
| 107 | + path="path/to/dockerfile/directory", |
| 108 | + labels={ |
| 109 | + "test": "true", |
| 110 | + "environment": "test" |
| 111 | + }, |
| 112 | + tag="myapp:test" |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +## Best Practices |
| 117 | + |
| 118 | +1. Use appropriate tags |
| 119 | +2. Clean up built images |
| 120 | +3. Use build arguments for configuration |
| 121 | +4. Consider build context size |
| 122 | +5. Use appropriate build caching |
| 123 | +6. Handle build failures |
| 124 | +7. Use appropriate platforms |
| 125 | +8. Add meaningful labels |
| 126 | + |
| 127 | +## Common Use Cases |
| 128 | + |
| 129 | +### Building Test Images |
| 130 | + |
| 131 | +```python |
| 132 | +def test_custom_image(): |
| 133 | + # Build test image |
| 134 | + image = build_image( |
| 135 | + path="path/to/dockerfile/directory", |
| 136 | + buildargs={"TEST_MODE": "true"}, |
| 137 | + tag="myapp:test" |
| 138 | + ) |
| 139 | + |
| 140 | + # Use the test image |
| 141 | + with GenericContainer(image) as container: |
| 142 | + # Your test code here |
| 143 | + pass |
| 144 | +``` |
| 145 | + |
| 146 | +### Building with Dependencies |
| 147 | + |
| 148 | +```python |
| 149 | +def test_with_dependencies(): |
| 150 | + # Build base image |
| 151 | + base_image = build_image( |
| 152 | + path="path/to/base/dockerfile/directory", |
| 153 | + tag="myapp:base" |
| 154 | + ) |
| 155 | + |
| 156 | + # Build test image using base |
| 157 | + test_image = build_image( |
| 158 | + path="path/to/test/dockerfile/directory", |
| 159 | + cache_from=[base_image], |
| 160 | + tag="myapp:test" |
| 161 | + ) |
| 162 | +``` |
| 163 | + |
| 164 | +### Building for Different Environments |
| 165 | + |
| 166 | +```python |
| 167 | +def test_different_environments(): |
| 168 | + # Build for different environments |
| 169 | + environments = ["dev", "test", "staging"] |
| 170 | + |
| 171 | + for env in environments: |
| 172 | + image = build_image( |
| 173 | + path="path/to/dockerfile/directory", |
| 174 | + buildargs={"ENVIRONMENT": env}, |
| 175 | + tag=f"myapp:{env}" |
| 176 | + ) |
| 177 | +``` |
| 178 | + |
| 179 | +## Troubleshooting |
| 180 | + |
| 181 | +If you encounter issues with image building: |
| 182 | + |
| 183 | +1. Check Dockerfile syntax |
| 184 | +2. Verify build context |
| 185 | +3. Check for missing files |
| 186 | +4. Verify build arguments |
| 187 | +5. Check for platform compatibility |
| 188 | +6. Verify cache settings |
| 189 | +7. Check for resource limits |
| 190 | +8. Verify Docker daemon state |
0 commit comments