Docker Community Forums

Share and learn in the Docker community.

Name 'docker build' container

(Mark Himsley) #1

Is it possible to name the container created by the docker build process?

I ask because, I have a Continuous Integration process which tries to build an image every night. Some times the build fails, but that leaves a stopped ‘build container’ and its associated images. I have to manually go to the Docker host and docker ps -a and pick the containers to rm - which is bad for a CI tool.


(Benjacho) #2

When you build an image, you can execute docker build -t name:tag .

(David Maze) #3

That only assigns a name to the final image, not to the intermediate containers; and in the case the OP is describing it also won’t assign names to intermediate layer images if there’s a crash in the middle of the build.

I just run a cron job to do the usual cleanup steps

docker ps -aq -f status=exited | xargs docker rm -v
docker images -q -f dangling=true | xargs docker rmi

but I can see how that wouldn’t be ideal.

(Mark Himsley) #4

Thangs @dmaze. You’ve got the problem exactly.

I am looking for a way that is better than running cron jobs, because cron jobs performing clean-up is a bit ugly and not determaistic - I always feel like I’ve failed when I have to resort to using clean-up cron jobs.

(Mukadobo) #5

@dmaze - Did you ever develop a better solution? I, too, am unhappy with the various suggestions - although I can’t think of better. But…

Maybe something like add a LABEL BuildStatus="Working" at the beginning of the Dockerfile, then at the end change the value with LABEL BuildStatus="Complete". Later, you can interrogate an exited container’s labels with something like docker inspect <NAMEorID> | jq -r '.[0].Config.Labels.BuildStatus' to see if still has the “Working” value and, if so, remove it.


Since I wrote this (mostly off the top of my head), I’ve tried out the basic mechanism and it works fine. Sadly, Docker doesn’t offer a mechanism to “un-define” a label so the final image must still carry the build status label (whatever name used). The label value can be empty, but the label name remains. There are worse thing in the world…

OR - just after the FROM ... do something like CMD ["{BUILD}"] that you will override at any time later in the Dockerfile. Then, when you find a “suspicious” (dead) container, look at the history of it’s image - which will show the CMD ... as a signal that this is leftover from a failed build. This won’t even leave a label as residue, but it’s a little more “obnoxious” in the Dockerfile (which could be softened with a comment line or two).