In addition to layering concerns, it’s also important to understand that each intermediary layer is normally (i.e. by most builders AFAIK) created by running a dedicated intermediary container and committing its topmost layer.
The context of those intermediary layers matters; for instance and relevant to your example, the working directory is never configured to be changed on creation of these intermediary containers, meaning it is not “retained” across multiple
Tested with both the Docker builder and podman/buildah:
$ podman build .
STEP 1: FROM alpine
STEP 2: RUN pwd
STEP 3: RUN cd /tmp
STEP 4: RUN pwd
STEP 5: COMMIT
$ docker build .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM alpine
Step 2/4 : RUN pwd
---> Running in 08d24b65bffd
Removing intermediate container 08d24b65bffd
Step 3/4 : RUN cd /tmp
---> Running in 463d071f12a4
Removing intermediate container 463d071f12a4
Step 4/4 : RUN pwd
---> Running in 52339084ede3
Removing intermediate container 52339084ede3
Successfully built 612d915ffdb2
So, I don’t believe your example Dockerfile yields a build procedure as you intend (i.e. you’re not actually running build processes with
samtools as the working directory).