Use COPY instead of ADD

Use COPY for local files and verify remote artifacts separately.

Description

ADD can copy a local file, unpack a local tar archive, or fetch a remote artifact.

Potential impact

A local tar archive can end up in the image as extracted files when the application expects the archive itself.

Remediation

Use COPY to keep a local file intact. Use ADD when you need its extra behavior. For remote files, verify the expected digest with a supported ADD --checksum or a separate verification step.

Examples

Set JAR_FILE to a JAR in the build context. Use a supported Java base image for production.

Before

dockerfile
FROM openjdk:10-jdk
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

After

dockerfile
FROM openjdk:10-jdk
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Both examples place the local JAR at the same destination. COPY makes that intent explicit. Verify the JAR’s origin and integrity separately.

References