I’m a newbie to docker, sorry if my question is too basic. I saw dockerfile like this:
FROM diamol/maven AS builder
WORKDIR /usr/src/iotd
COPY pom.xml .
RUN mvn -B dependency:go-offline
COPY . .
RUN mvn package
FROM diamol/openjdk
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"]
I’m confused about COPY . .
instruction, what does the first period and second period COPY . .
mean?
Also, if I want to copy all files of the current working directory from my host machine into the image, then how can I modify COPY . .
so that the first period means currenty directory of my machine?
2
Answers
From my perspective, one of the best ways to know all the details for
COPY
andWORKDIR
docker commands is to go through following official documentation.You can either search for
COPY
andWORKDIR
keywords on the home page at below first link or please refer to last two links and find all the details including examples.https://docs.docker.com/engine/reference/builder/
https://docs.docker.com/engine/reference/builder/#copy
https://docs.docker.com/engine/reference/builder/#workdir
In the Dockerfile
COPY
directive, the last argument is the path inside the container, relative to the currentWORKDIR
. All of the preceding arguments are paths inside the build context, the host directory passed as an argument todocker build
.You probably don’t need to. So long as you
docker build .
naming the current directory.
as the last argument, that’s exactly whatCOPY . .
does. That instruction means to copy.
– the entirety of the build context, from the original host system – to.
– the current directory, inside the image.I’ve mentioned "build context" a couple of times. That is the directory argument to
docker build
or that you specify in a
docker-compose.yml
fileexcept that the files mentioned in a
.dockerignore
file are removed first.In the question title you also ask
They do. The newer BuildKit backend has some capability to execute build stages not necessarily in the order they’re written, but it ensures that you get the same results as if all of the
COPY
andRUN
instructions from a previous stage had run before aCOPY --from=...
in a later stage happens.