Search code examples
earthly

How to avoid installing jq/docker into container running inside of WITH DOCKER block?


I build JVM-based project and would like to have an Earthly target with integration steps similar to this one using WITH DOCKER ... END syntax. WITH DOCKER command is needed to have a real database instance available in the context of integration tests execution.

Since I work on a JVM project, my base image for executing any commands related to build system is: FROM bellsoft/liberica-openjdk-alpine:17. The thing which I find suboptimal is that for any command running inside of WITH DOCKER ... END block Earthly check for presence of jq and docker/docker-compose projects. Each time I execute integration tests on CI node, jq and docker get installed, while they are completely useless in my WITH DOCKER usage scenario.

Is there a way to disable their installation? Right now, as a workaround, I consider adding jq and docker to my base bellsoft/liberica-openjdk-alpine:17 builder-image to make docker/jq installation *cached*


Solution

  • Alex Couture-Beil recently provided good answer to this question on Earthly Slack

    I think that's the best way to avoid installing dependencies on each build, just use the recommended earthly/dind:alpine for WITH DOCKER commands, it will only be downloaded once and run tests with docker run instead of RUN. Here is how I changed my pipeline now:

    test:
        FROM earthly/dind:alpine # <-- recommended image for 'WITH DOCKER' commands, docker and jq are already there
        COPY docker-compose.yaml ./
        WITH DOCKER --compose docker-compose.yaml --service=mongo --load build-image=+compile
            RUN docker run --name tests-container --network=host build-image \
            ./gradlew test \
            && \
            docker cp tests-container:/build/ build/ # <-- get test execution results
        END
        SAVE ARTIFACT build/test-results