简体   繁体   中英

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM