Kirill
Kirill

Reputation: 8096

How to build an image from Dockerfile using Earthly target?

I would like to build and image from Dockerfile using Earthly. You might be wondering why do I want that, because I can describe images right inside of Earthfile, but I have 2 reasons for using external Dockerfile:

  1. ADD command (which I need to download file by URL) is not supported by Earthly yet
  2. I would like to use a heredoc syntax for embedding file's content into container right from Dockerfile. This requires # syntax=docker/dockerfile:1.4, which is again not available in Earthfile

So, here is what I tried to do.

My approximate Dockerfile looks like:

# syntax=docker/dockerfile:1.4
FROM gcr.io/distroless/java17:nonroot
WORKDIR /opt/app
ADD --chown=nonroot https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.4.7/applicationinsights-agent-3.4.7.jar agent.jar
COPY <<EOF /opt/app/applicationinsights.json
{
  "instrumentation": {}
}
EOF

And this is how I try to build it with Earthly:

base-image:
    FROM earthly/dind:alpine
    WORKDIR /build
    ENV DOCKER_BUILDKIT=1 # <---- required to support heredoc syntax
    COPY distroless-runtime-17.Dockerfile Dockerfile
    WITH DOCKER --allow-privileged
        RUN docker build . -t base-17-image
    END

While the WITH DOCKER RUN part gets executed successfully, I do not know how to use the result of base-image target in other targets to package my app using the resulting base image. The FROM base-17-image just fails as if it does not exist (and this tag really does not exist - docker run base-17-image fails with the same reason).

Upvotes: 1

Views: 897

Answers (1)

Kirill
Kirill

Reputation: 8096

It turned out to be very easy and natively supported:

The whole recipe is just 2 lines of code:

base-image:
    FROM DOCKERFILE -f distroless-runtime-17.Dockerfile .

and the result can of the above step can be reused to package your application as: FROM +base-image

Upvotes: 3

Related Questions