private void createBuckets() {
        logger().debug("Creating {} buckets (and corresponding indexes).", buckets.size());

        for (BucketDefinition bucket : buckets) {
            logger().debug("Creating bucket \"{}\"", bucket.getName());

            @Cleanup
            Response response = doHttpRequest(
                MGMT_PORT,
                "/pools/default/buckets",
                "POST",
                new FormBody.Builder()
                    .add("name", bucket.getName())
                    .add("ramQuotaMB", Integer.toString(bucket.getQuota()))
                    .add("flushEnabled", bucket.hasFlushEnabled() ? "1" : "0")
                    .add("replicaNumber", Integer.toString(bucket.getNumReplicas()))
                    .build(),
                true
            );

            checkSuccessfulResponse(response, "Could not create bucket " + bucket.getName());

            timePhase(
                "createBucket:" + bucket.getName() + ":waitForAllServicesEnabled",
                () -> {
                    new HttpWaitStrategy()
                        .forPath("/pools/default/b/" + bucket.getName())
                        .forPort(MGMT_PORT)
                        .withBasicCredentials(username, password)
                        .forStatusCode(200)
                        .forResponsePredicate(new AllServicesEnabledPredicate())
                        .waitUntilReady(this);
                }
            );

            if (enabledServices.contains(CouchbaseService.QUERY)) {
                // If the query service is enabled, make sure that we only proceed if the query engine also
                // knows about the bucket in its metadata configuration.
                timePhase(
                    "createBucket:" + bucket.getName() + ":queryKeyspacePresent",
                    () -> {
                        Unreliables.retryUntilTrue(
                            1,
                            TimeUnit.MINUTES,
                            () -> {
                                @Cleanup
                                Response queryResponse = doHttpRequest(
                                    QUERY_PORT,
                                    "/query/service",
                                    "POST",
                                    new FormBody.Builder()
                                        .add(
                                            "statement",
                                            "SELECT COUNT(*) > 0 as present FROM system:keyspaces WHERE name = \"" +
                                            bucket.getName() +
                                            "\""
                                        )
                                        .build(),
                                    true
                                );

                                String body = queryResponse.body() != null ? queryResponse.body().string() : null;
                                checkSuccessfulResponse(
                                    queryResponse,
                                    "Could not poll query service state for bucket: " + bucket.getName()
                                );

                                return Optional
                                    .of(MAPPER.readTree(body))
                                    .map(n -> n.at("/results/0/present"))
                                    .map(JsonNode::asBoolean)
                                    .orElse(false);
                            }
                        );
                    }
                );
            }

            if (bucket.hasPrimaryIndex()) {
                if (enabledServices.contains(CouchbaseService.QUERY)) {
                    @Cleanup
                    Response queryResponse = doHttpRequest(
                        QUERY_PORT,
                        "/query/service",
                        "POST",
                        new FormBody.Builder()
                            .add("statement", "CREATE PRIMARY INDEX on `" + bucket.getName() + "`")
                            .build(),
                        true
                    );

                    try {
                        checkSuccessfulResponse(
                            queryResponse,
                            "Could not create primary index for bucket " + bucket.getName()
                        );
                    } catch (IllegalStateException ex) {
                        // potentially ignore the error, the index will be eventually built.
                        if (!ex.getMessage().contains("Index creation will be retried in background")) {
                            throw ex;
                        }
                    }

                    timePhase(
                        "createBucket:" + bucket.getName() + ":primaryIndexOnline",
                        () -> {
                            Unreliables.retryUntilTrue(
                                1,
                                TimeUnit.MINUTES,
                                () -> {
                                    @Cleanup
                                    Response stateResponse = doHttpRequest(
                                        QUERY_PORT,
                                        "/query/service",
                                        "POST",
                                        new FormBody.Builder()
                                            .add(
                                                "statement",
                                                "SELECT count(*) > 0 AS online FROM system:indexes where keyspace_id = \"" +
                                                bucket.getName() +
                                                "\" and is_primary = true and state = \"online\""
                                            )
                                            .build(),
                                        true
                                    );

                                    String body = stateResponse.body() != null ? stateResponse.body().string() : null;
                                    checkSuccessfulResponse(
                                        stateResponse,
                                        "Could not poll primary index state for bucket: " + bucket.getName()
                                    );

                                    return Optional
                                        .of(MAPPER.readTree(body))
                                        .map(n -> n.at("/results/0/online"))
                                        .map(JsonNode::asBoolean)
                                        .orElse(false);
                                }
                            );
                        }
                    );
                } else {
                    logger()
                        .info(
                            "Primary index creation for bucket {} ignored, since QUERY service is not present.",
                            bucket.getName()
                        );
                }
            }
        }
    }
Based on the user-configured bucket definitions, creating buckets and corresponding indexes if needed.
modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java