You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/modules/docker_compose.md
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,23 +13,24 @@ The [`ComposeContainer`](http://static.javadoc.io/org.testcontainers/testcontain
13
13
A single class `ComposeContainer`, defined based on a `docker-compose.yml` file, should be sufficient to launch any number of services required by our tests:
14
14
15
15
<!--codeinclude-->
16
-
[Create a ComposeContainer](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerConstructor
16
+
[Create a ComposeContainer](../../core/src/test/java/org/testcontainers/junit/ComposeContainerTest.java) inside_block:composeContainerConstructor
17
17
<!--/codeinclude-->
18
18
19
19
!!! note
20
20
Make sure the service names use a `-` rather than `_` as separator.
21
21
22
-
In this example, Docker Compose file should have content such as:
In this example, Docker Compose file should have content such as:
28
+
```yaml
26
29
services:
27
30
redis:
28
-
image: redis:7
29
-
postgres:
30
-
image: postgres:17-alpine
31
-
environment:
32
-
POSTGRES_USER: postgres
31
+
image: redis
32
+
db:
33
+
image: mysql:8.0.36
33
34
```
34
35
35
36
Note that it is not necessary to define ports to be exposed in the YAML file, as this would inhibit the reuse/inclusion of the file in other contexts.
@@ -55,7 +56,7 @@ ComposeContainer provides methods for discovering how your tests can interact wi
55
56
56
57
Let's use this API to create the URL that will enable our tests to access the Redis service:
57
58
<!--codeinclude-->
58
-
[Access a Service's host and port](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:getServiceHostAndPort
59
+
[Access a Service's host and port](../../core/src/test/java/org/testcontainers/junit/ComposeContainerTest.java) inside_block:getServiceHostAndPort
59
60
<!--/codeinclude-->
60
61
61
62
## Wait Strategies and Startup Timeouts
@@ -67,15 +68,15 @@ There are overloaded `withExposedService` methods that take a [`WaitStrategy`](h
67
68
68
69
For instance, we can wait for exposed port and set a custom timeout:
69
70
<!--codeinclude-->
70
-
[Wait for the exposed port and use a custom timeout](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWaitForPortWithTimeout
71
+
[Wait for the exposed port and use a custom timeout](../../core/src/test/java/org/testcontainers/junit/ComposeContainerWithWaitStrategies.java) inside_block:composeContainerWaitForPortWithTimeout
71
72
<!--/codeinclude-->
72
73
73
74
Needless to say, we can define different strategies for each service in our Docker Compose setup.
74
75
75
-
For example, our Redis container can wait for a successful redis-cli command, while Postgres waits for a specific log message:
76
+
For example, our Redis container can wait for a successful redis-cli command, while our db service waits for a specific log message:
76
77
77
78
<!--codeinclude-->
78
-
[Wait for a custom command and a log message](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWithCombinedWaitStrategies
79
+
[Wait for a custom command and a log message](../../core/src/test/java/org/testcontainers/junit/ComposeContainerWithWaitStrategies.java) inside_block:composeContainerWithCombinedWaitStrategies
79
80
<!--/codeinclude-->
80
81
81
82
!!! More on Wait Strategies
@@ -89,15 +90,15 @@ We can override Testcontainers' default behaviour and make it use a `docker-comp
89
90
This will generally yield an experience that is closer to running _docker compose_ locally, with the caveat that Docker Compose needs to be present on dev and CI machines.
90
91
91
92
<!--codeinclude-->
92
-
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWithLocalCompose
93
+
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/containers/ComposeProfilesOptionTest.java) inside_block:composeContainerWithLocalCompose
93
94
<!--/codeinclude-->
94
95
95
96
## Build Working Directory
96
97
97
98
We can select what files should be copied only via `withCopyFilesInContainer`:
98
99
99
100
<!--codeinclude-->
100
-
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWithCopyFiles
101
+
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/junit/ComposeContainerWithCopyFilesTest.java) inside_block:composeContainerWithCopyFiles
101
102
<!--/codeinclude-->
102
103
103
104
In this example, only docker compose and env files are copied over into the container that will run the Docker Compose file.
0 commit comments