I can not make clear where the RUN instruction in Dockerfile execute.
In the build context or in the running container?
The official document says:
The RUN instruction will execute any commands to create a new layer on top of the current image. The added layer is used in the next step in the Dockerfile.
So I think the RUN instruction is exeuted in the build context.
But let us think about the next examples:
RUN <<EOF
apt-get update
apt-get install -y curl
EOF
Shouldn’t the ‘apt-get update’ and ‘apt-get install -y curl’ run in the running container? Not in the build context?
We are going to update the underlying image/container and install curl on it, so the curl shouldn’t be installed on the hos, right?
FROM base AS build-client <<---- Stage 1
RUN go build -o /bin/client ./cmd/client
FROM base AS build-server <<---- Stage 2
RUN go build -o /bin/server ./cmd/server
Shouldn’t the ‘go build’ run in the running container? Not in the build context?
We are going to build an image that is applicable to compile go-lang programs, aren’t we?
An image is a blueprint for a container. An image should include everything required to run the core service|application|toolchain, all its dependencies, all required configuration, and usually a clever entrypoint script used to render environment variables into configurations, before it starts the main process.It should be self-contained.
The build context is a copy of the content from the host folder you use as context (excluding files defined in .dockerignore). Though, other container images or even git projects can be added as additional build contexts to the build process.
Every RUN instruction will create its temporary build container based on the latest image layer in the chain between the image layers from the base image, up to the image layers of the last RUN instruction. Modification made to the temporary build containers filesystem will be stored as image layer.
The COPY and ADD instructions can copy file from the build context into a new image (layer.
When it comes to your last question: only you know what a container created based on your image should do..