testcontainers/lib.rs
1#![deny(missing_debug_implementations)]
2#![warn(rust_2018_idioms)]
3#![cfg_attr(docsrs, deny(rustdoc::broken_intra_doc_links))]
4#![cfg_attr(docsrs, feature(doc_cfg))]
5#![forbid(unsafe_code)]
6
7//! A library for integration testing against docker containers from within Rust.
8//!
9//! This crate is the official Rust language fork of [`Testcontainers`][tc_website].
10//!
11//! Tests should be self-contained and isolated. While this is usually easy for unit-tests, integration-tests typically require a more complex environment.
12//! The testcontainers ecosystem facilitates self-contained and isolated integration tests. It allows to easily spin up Docker containers from within your tests and removes them afterwards.
13//!
14//! A very typical usecase for testcontainers are integration-tests of persistence layers. These require an actual database to be present. Using testcontainers, your tests can spin up database containers themselves, without the need for any other setup.
15//!
16//! # Main benefits
17//!
18//! - Run integration tests in parallel (because each test sets up its own environment)
19//! - Run integration tests the same way you run unit tests (`cargo test` and you are fine)
20//!
21//! # Usage
22//!
23//! Unsurprisingly, working with testcontainers is very similar to working with Docker itself.
24//!
25//! If you need to build an image first, then you need to define the [`BuildableImage`] that specifies the build context
26//! and the Dockerfile, then call the `build_image` method on it from either the [`AsyncBuilder`] or [`SyncBuilder`] trait.
27//! This will yield an [`Image`] you could actually start.
28//!
29//! If you already have a Docker image you can just define your [`Image`] that you want to run, and then simply call the
30//! `start` method on it from either the [`AsyncRunner`] or [`SyncRunner`] trait.
31//!
32//! This will return you [`ContainerAsync`] or [`Container`] respectively.
33//! Containers implement `Drop`. As soon as they go out of scope, the underlying docker container is removed.
34//! To disable this behavior, you can set ENV variable `TESTCONTAINERS_COMMAND` to `keep`.
35//!
36//! See examples in the corresponding runner ([`AsyncRunner`] and [`SyncRunner`])
37//!
38//! ### Docker host resolution
39//!
40//! You can change the configuration of the Docker host used by the client in two ways:
41//! - environment variables
42//! - `~/.testcontainers.properties` file (a Java properties file, enabled by the `properties-config` feature)
43//!
44//! ##### The host is resolved in the following order:
45//!
46//! 1. Docker host from the `tc.host` property in the `~/.testcontainers.properties` file.
47//! 2. `DOCKER_HOST` environment variable.
48//! 3. Docker host from the "docker.host" property in the `~/.testcontainers.properties` file.
49//! 4. Read the default Docker socket path, without the unix schema. E.g. `/var/run/docker.sock`.
50//! 5. Read the rootless Docker socket path, checking in the following alternative locations:
51//! 1. `${XDG_RUNTIME_DIR}/.docker/run/docker.sock`.
52//! 2. `${HOME}/.docker/run/docker.sock`.
53//! 3. `${HOME}/.docker/desktop/docker.sock`.
54//! 6. The default Docker socket including schema will be returned if none of the above are set.
55//!
56//! ### Docker authentication
57//!
58//! Sometimes the Docker images you use live in a private Docker registry.
59//! For that reason, Testcontainers for Rust gives you the ability to read the Docker configuration and retrieve the authentication for a given registry.
60//! Configuration is fetched in the following order:
61//!
62//! 1. `DOCKER_AUTH_CONFIG` environment variable, unmarshalling the string value from its JSON representation and using it as the Docker config.
63//! 2. `DOCKER_CONFIG` environment variable, as an alternative path to the directory containing Docker `config.json` file.
64//! 3. else it will load the default Docker config file, which lives in the user's home, e.g. `~/.docker/config.json`.
65//!
66//! # Ecosystem
67//!
68//! `testcontainers` is the core crate that provides an API for working with containers in a test environment.
69//! The only buildable image and image implementations that are provided by the core crate are the [`GenericBuildableImage`]
70//! and [`GenericImage`], respectively.
71//!
72//! However, it does not provide ready-to-use modules, you can implement your [`Image`]s using the library directly or use community supported [`testcontainers-modules`].
73//!
74//! # Usage in production code
75//!
76//! Although nothing inherently prevents testcontainers from being used in production code, the library itself was not designed with that in mind.
77//!
78//! [tc_website]: https://testcontainers.org
79//! [`Docker`]: https://docker.com
80//! [`AsyncBuilder`]: runners::AsyncBuilder
81//! [`SyncBuilder`]: runners::SyncBuilder
82//! [`AsyncRunner`]: runners::AsyncRunner
83//! [`SyncRunner`]: runners::SyncRunner
84//! [`testcontainers-modules`]: https://crates.io/crates/testcontainers-modules
85
86pub mod core;
87#[cfg(feature = "blocking")]
88#[cfg_attr(docsrs, doc(cfg(feature = "blocking")))]
89pub use crate::core::Container;
90#[cfg(feature = "reusable-containers")]
91pub use crate::core::ReuseDirective;
92pub use crate::core::{
93 copy::{CopyDataSource, CopyTargetOptions, CopyToContainer, CopyToContainerError},
94 error::TestcontainersError,
95 BuildableImage, ContainerAsync, ContainerRequest, Healthcheck, Image, ImageExt,
96};
97
98#[cfg(feature = "watchdog")]
99#[cfg_attr(docsrs, doc(cfg(feature = "watchdog")))]
100pub(crate) mod watchdog;
101
102mod buildables;
103pub use buildables::generic::GenericBuildableImage;
104
105/// All available Docker images.
106mod images;
107pub use images::generic::GenericImage;
108
109#[cfg(feature = "docker-compose")]
110#[cfg_attr(docsrs, doc(cfg(feature = "docker-compose")))]
111pub mod compose;
112
113pub mod runners;
114
115/// Re-export of the `bollard` crate to allow direct interaction with the Docker API.
116/// This also solves potential version conflicts between `testcontainers` and `bollard` deps.
117pub use bollard;