Images¶
Building an image¶
Build and start your own Docker image:
1 2 3 4 5 6 7 | |
Images are built by default with a randomly generated name and are deleted on exit. If you wish to keep the built images between test runs, you can provide a name and specify not to delete the image:
1 2 3 4 5 | |
With buildkit¶
1 2 3 4 5 6 | |
With a pull policy¶
Testcontainers will automatically pull an image if it doesn't exist. This is configurable:
1 2 3 4 5 6 | |
Create a custom pull policy:
1 2 3 4 5 6 7 8 9 10 11 12 | |
PullPolicy.neverPull() and custom never-pull policies throw an error for Dockerfile builds, with or without BuildKit: the Docker build API cannot forbid base image pulls (docker/buildx#1889).
For an already built image, use a container with a never-pull policy.
With build arguments¶
1 2 3 4 | |
With target¶
Stop the build at a specific stage by specifying a target:
1 2 3 4 | |
With custom Dockerfile¶
1 2 3 | |
Without cache¶
1 2 3 4 | |
With platform¶
1 2 3 4 | |
Image name substitution¶
Testcontainers supports automatic substitution of Docker image names.
This allows replacement of an image name specified in test code with an alternative name - for example, to replace the name of a Docker Hub image dependency with an alternative hosted on a private image registry.
This is advisable to avoid Docker Hub rate limiting, and some companies will prefer this for policy reasons.
You can then configure Testcontainers to apply the prefix registry.mycompany.com/mirror/ to every image that it tries to pull from Docker Hub. This can be done by setting the environment variable TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX=registry.mycompany.com/mirror/.