Install a Private Go Module in a Dockerfile Without Leaking Credentials
Your team's Go app depends on git.corp/mycompany/logger, a module hosted on the company's private Git server.
To download it, the build needs the credentials stored in ~/app/.netrc.
A teammate wrote a Dockerfile that copies the .netrc file into the image, downloads the dependencies, and then deletes the file with RUN rm -f /root/.netrc.
Does deleting the file actually protect the credentials?
Start by inspecting the project:
cat ~/app/Dockerfile
cat ~/app/go.mod
cat ~/app/.netrc
You'll also see corp-ca.crt in the project. It's the public certificate of the company's internal certificate authority, which the build needs to trust git.corp over HTTPS. It isn't a secret.
Step 1: Build and push the insecure image
Build the app using the original, unmodified Dockerfile, then push the image:
docker build -t registry.iximiuz.com/my-app:insecure ~/app/
docker push registry.iximiuz.com/my-app:insecure
Step 2: Fix the credential leak
The checker found the token in an image layer, even though .netrc is no longer visible in the container's filesystem. Anyone who can pull this image could recover the token and use it to access the company's private repositories.
Update ~/app/Dockerfile so that:
- The app still builds and runs, including downloading the private module during
docker build. - The credentials are never written to any image layer, including intermediate layers retained in the build cache.
Build the fixed image, tag it as registry.iximiuz.com/my-app:v1.0.0, and push it:
docker push registry.iximiuz.com/my-app:v1.0.0
Hint 1: Why doesn't RUN rm solve the problem?
Docker images consist of layers. A COPY instruction puts the file in one layer. A later RUN rm removes it from the visible filesystem, but it doesn't change that earlier layer.
Anyone with access to the image can extract the layer containing .netrc.
Hint 2: Would a multi-stage build be enough?
A multi-stage build can keep the credentials out of the final image by copying only the compiled binary from the build stage.
But the build-stage layers can remain in the build cache. They may be exported to a registry, for example when a CI pipeline uses --cache-to ... mode=max.
A multi-stage build is useful, but the credentials must not enter a layer in the first place.
Hint 3: How can the build use credentials without storing them?
Docker supports build secrets, which are mounted into a single build step and never written to an image layer.
Look up RUN --mount=type=secret in a Dockerfile and the matching docker build --secret flag.
If build secrets are new to you, solve the simpler npm version of this challenge first.