I’m new to Docker and trying to wrap my head around it thoroughly. I have an app using a multistage build, which may get published frequently, and I do not want to end up using too much drive space, or have to remember to come back to clean things up.
There is a similar question here, but there is no feedback regarding a convenient automated cleanup mechanism: stackoverflow.com/questions/48640478/do-we-need-to-clean-up-in-a-multi-stage-build
The builder image is created with a different ID each time, but no tag or name. I can call docker image prune, and it wipes them all out, which is nice, but I’m not the only one using this server, so that could be dangerous.
My multistage build looks like this:
# builder FROM node:14.17.0-alpine as build WORKDIR /app ENV PATH /app/node_modules/.bin:$PATH COPY package.json ./ COPY package-lock.json ./ RUN npm ci RUN npm install firstname.lastname@example.org -g COPY . ./ RUN npm run build # production environment FROM nginx:stable-alpine COPY --from=build /app/build /usr/share/nginx/html # new COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ("nginx", "-g", "daemon off;")
I can tag the production environment from the call to build this Dockerfile in a PowerShell script:
docker build -f ./Dockerfile.prod -t sbc.hackermon:$($DateTime) ./
(On a side note, this creates an image that I think is a replication every time, since though each one gets a new tag, their ID is always the same, every time I publish/build. If so, then there’s no reason for me to tag them distinctly, unless they do happen to change as some point, which makes me wonder, since I’m using ‘stable’ and not a version number, if they could change without any code changes on my end. But that’s a different question I may also ask about separately.)
Now the build/helper images get a new random ID every time, but get
<none> as a tag and
<none> as for their repo. Being random, I don’t have a direct way to reference the ID to remove them each time I build, so they just keep piling up.
Is there a direct way to tag these manually so I can know the ID, or is there a way to find out the ID after so I know which to remove? It does appear there is no way to tag from within the Dockerfile, so that’s not an option.