Home > Enterprise >  Makefile get last docker build
Makefile get last docker build

Time:01-04

I am trying to get latest docker build ID from the Makefile, I have created some Make as follow

service-develop:    
    $(MAKE) build
    $(eval IMAGE=$(shell docker images --format='{{.ID}}' | head -1))
    docker network inspect $(DOCKER_NETWORK) >/dev/null 2>&1 || docker network create $(DOCKER_NETWORK)
    docker run -td --name $(PROJECT_NAME)-rfs --network-alias rfs --network=$(DOCKER_NETWORK) \
                    -v ${PWD}/packages/${NAME}/python:/var/opt/ncs/packages/${NAME}/python \
                    -v ${PWD}/packages/${NAME}/templates:/var/opt/ncs/packages/${NAME}/templates \
                    $(IMAGE)

During the $(MAKE) build some docker image is created, then I am trying to get the latest build ID and assign it into IMAGE variable $(eval IMAGE=$(shell docker images --format='{{.ID}}' | head -1)) however what happens is following, latest docker image - dcd6599dee08

Step 12/12 : COPY extra-files /
 ---> dcd6599dee08
Successfully built dcd6599dee08
Successfully tagged package-1/nso:5.4.1.1-ubuntu

I can see it also in my output

ubuntu@mgmt:~$ docker images | head -5
REPOSITORY                                                                                                        TAG              IMAGE ID       CREATED          SIZE
package-1/nso                                                                                                     5.4.1.1-ubuntu   dcd6599dee08   7 minutes ago    567MB
package-1/build                                                                                                   5.4.1.1-ubuntu   df61a9396715   7 minutes ago    1.36GB
<none>                                                                                                            <none>           ace2ec32bd29   10 minutes ago   567MB
<none>                                                                                                            <none>           4ca01c690857   10 minutes ago   1.36GB


ubuntu@mgmt:~$ docker images --format='{{.ID}}' | head -1
dcd6599dee08

Unfortunatelly IMAGE variable got not the first but third entry from the docker images output

docker run -td --name package-1-rfs --network-alias rfs --network=bridge-net-ubuntu \
            -v /home/ubuntu/package-1/packages/underlay/python:/var/opt/ncs/packages/underlay/python \
            -v /home/ubuntu/package-1/packages/underlay/templates:/var/opt/ncs/packages/underlay/templates \
            **ace2ec32bd29**

Any thoughts?

CodePudding user response:

You can use the Docker image name (docker build -t option) directly in pretty much every docker command. You never need to look up the ID if you know the full image name. I'd specify that name in a Make variable if I were using Make here.

REPOSITORY := docker.io
IMAGE := package-1/nso
TAG := nso:5.4.1.1-ubuntu
DOCKER_IMAGE := $(REPOSITORY)/$(IMAGE):$(TAG)

DOCKER_NETWORK := some-network

build:
        docker build -t $(DOCKER_IMAGE) .

network:
        docker network inspect $(DOCKER_NETWORK) >/dev/null 2>&1 || docker network create $(DOCKER_NETWORK)

service: build network
        docker run -d \
          --name $(PROJECT_NAME)-rfs \
          --network-alias rfs \
          --network=$(DOCKER_NETWORK) \
          $(DOCKER_IMAGE)

You should rarely need the GNU Make $(shell ...) extension, and I know some of the Make functions get expanded at times other than when you expect (I can't immediately find a statement if $(eval $(shell ...)) will get run before or during rule execution). You don't need it here, since you already know the image name and tag.

  • Related