|
1 |
| -# CircleCI (Cloud, Server v2.x, and Server v3.x) |
| 1 | +# CircleCI |
2 | 2 |
|
3 |
| -Your CircleCI configuration should use a dedicated VM for Testcontainers to work. You can achieve this by specifying the |
4 |
| -executor type in your `.circleci/config.yml` to be `machine` instead of the default `docker` executor (see [Choosing an Executor Type](https://circleci.com/docs/2.0/executor-types/) for more info). |
| 3 | +## CircleCI Cloud |
| 4 | + |
| 5 | +Testcontainers can be used on CircleCI with either the Docker executor or the Machine executor. |
| 6 | + |
| 7 | +When using the Docker executor, you must include the `setup_remote_docker` step to enable Docker support. |
| 8 | + |
| 9 | +Testcontainers will run containers using the Docker daemon provided by `setup_remote_docker`. Exposed container ports are automatically forwarded to the job container. |
| 10 | + |
| 11 | +Here’s a minimal CircleCI configuration using the Docker executor: |
| 12 | + |
| 13 | + |
| 14 | +```yaml |
| 15 | +version: 2.1 |
| 16 | +jobs: |
| 17 | + build: |
| 18 | + docker: |
| 19 | + - image: cimg/openjdk:23.0 |
| 20 | + steps: |
| 21 | + - checkout |
| 22 | + - setup_remote_docker |
| 23 | + - run: mvn -B clean install |
| 24 | +``` |
| 25 | +
|
| 26 | +!!! warning |
| 27 | + Testcontainers will map exposed container ports to the job container's network. However, due to the remote Docker architecture, **you should not assume containers are accessible via `localhost`**. Always use `.getHost()` and `.getMappedPort()` from Testcontainers to retrieve the correct hostname and port as described [here](/features/networking/#getting-the-container-host). |
| 28 | + |
| 29 | +Alternatively, you can use the Machine executor, which provides native Docker support without `setup_remote_docker`: |
| 30 | + |
| 31 | +```yaml |
| 32 | +version: 2.1 |
| 33 | +jobs: |
| 34 | + build: |
| 35 | + machine: |
| 36 | + image: ubuntu-2204:current |
| 37 | + steps: |
| 38 | + - checkout |
| 39 | + - run: mvn -B clean install |
| 40 | +``` |
| 41 | + |
| 42 | +## CircleCI Server |
| 43 | + |
| 44 | +On CircleCI Server, `setup_remote_docker` is not supported. To use Testcontainers, you must run your jobs on a dedicated virtual machine using the machine executor. You can configure this by specifying the executor type in your `.circleci/config.yml` to be `machine`. For more information see [Choosing an Executor Type](https://circleci.com/docs/executor-intro/). |
5 | 45 |
|
6 | 46 | Here is a sample CircleCI configuration that does a checkout of a project and runs Maven:
|
7 | 47 |
|
8 |
| -```yml |
| 48 | +```yaml |
9 | 49 | jobs:
|
10 | 50 | build:
|
11 | 51 | # Check https://circleci.com/docs/executor-intro#linux-vm for more details
|
|
15 | 55 | - run: mvn -B clean install
|
16 | 56 | ```
|
17 | 57 |
|
18 |
| -You can learn more about the best practices of using Testcontainers together with CircleCI in [this article](https://www.atomicjar.com/2022/12/testcontainers-with-circleci/). |
|
0 commit comments