liveBook · Manning

Copy instruction

As explained in Learn Docker in a Month of Lunches, Second Edition

A COPY instruction is a Dockerfile directive that copies files or directories into the image filesystem.

A Dockerfile needs a way to put application files into an image. `COPY` does that: `COPY [source path] [target path]` copies files or directories from the build context into the image filesystem. In `WORKDIR /web-ping` followed by `COPY app.js .`, the dot targets the working directory, so the application file is available there when the container starts. section 3.2

Choose what to copy at each build step. A Java build can copy `pom.xml` first, download Maven dependencies, and then use `COPY . .` to bring in the remaining source before packaging it. If `pom.xml` has not changed, Docker can reuse the dependency step from its layer cache. section 4.2

Copying from another build stage

In a multi-stage build, `COPY --from=builder /usr/src/iotd/target/iotd-service-0.1.0.jar .` takes the compiled JAR from the builder stage and places it in the final stage’s `/app` working directory. The final image starts from an image with the Java runtime rather than the Maven build tools. section 4.2

Avoid `COPY . .` when the image does not need everything in the build context. Copying an unused directory and deleting it with a later `RUN` instruction hides its files from the resulting container filesystem, but the earlier COPY layer still adds to image size. `COPY ./app ./app` selects just the application folder. A `.dockerignore` file is another way to exclude unwanted content from the build context, especially when precise COPY patterns become awkward. section 17.1

Listing 4.2 Dockerfile for building a Java app with Maven
FROM diamol/maven:2e AS builder

WORKDIR /usr/src/iotd
COPY pom.xml .
RUN mvn -B dependency:go-offline

COPY . .
RUN mvn package

# app
FROM diamol/openjdk:2e

WORKDIR /app
COPY --from=builder /usr/src/iotd/target/iotd-service-0.1.0.jar .

EXPOSE 80
ENTRYPOINT ["java", "-jar", "/app/iotd-service-0.1.0.jar"]

Follow the three COPY instructions to see how a Maven build separates dependency input, source code, and the finished JAR. section 4.2

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Defines source and target paths, then works through copy ordering, multi-stage copying, and image-size consequences.
Examples
Uses a Node.js application file, a Maven-built JAR, and a comparison between copying an entire directory and copying only its app folder.
What it assumes
Introduces Dockerfile COPY syntax from a first-image example, then assumes familiarity with builds when discussing caching and multi-stage images.
What you can do after
Build it from scratch: write COPY instructions that place needed files in an image while avoiding unnecessary copied content.
How it connects
Connects COPY to WORKDIR, build contexts, layer caching, multi-stage builds, and .dockerignore.

Also explained in

Compare how 2 books explain copy instruction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.